You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让卡片在页面缩小时保持固定宽高比?

问题求助

我知道这个问题的解决方案应该很简单,但我一直卡在这个问题上,无法解决。

  • 全屏时卡片宽高比正常
  • 页面缩小后卡片宽高比变形

能否有人帮我分析代码,指出问题所在或提供修复方法?提前万分感谢。


现有代码

CSS 代码

.book {
  position: relative;
  border-radius: 10px;
  max-width: 250px;
  max-height: 380px;
  background-color: whitesmoke;
  box-shadow: 1px 1px 12px rgb(0, 0, 0, .7);
  border: 1px solid rgb(0, 0, 0, .2);
  transform: preserve-3d;
  perspective: 2000px;
  display: flex;
  align-items: left;
  justify-content: center;
  color: #000;
  flex-direction: column;
  margin: 0px;
}

.cover {
  top: 0;
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.5s;
  transform-origin: 0;
  box-shadow: 1px 1px 12px rgb(0, 0, 0, .1);
  display: flex;
  align-items: center;
  justify-content: center;
}

.cover img {
  width: 100%;
  height: 100%;
  border-radius: 10px;
}

.book:hover .cover {
  transition: all 0.5s;
  transform: rotatey(-80deg);
}

.book-content {
  margin-left: 45px;
  margin-right: 10px;
  padding-top: 10px;
}

.book p {
  font-size: 15px;
}

.book p strong {
  font-size: 16px;
}

.book h3 {
  line-height: 15px;
  margin-left: 0;
}

.book-section {
  display: flex;
  justify-content: center;
  align-items: center;
  background: white;
  padding: 30px;
  border-radius: 5px;
  margin-bottom: 10px;
  height: 100%;
}

HTML 代码

等待封面图片加载完成后,将鼠标悬停在其上。

<div class="book-section">
  <div class="book">
    <div class="book-content">
      <h3>The Favourite</h3>
      <p><strong>Director:</strong> Yorgos Lanthimos
        <br><strong>Writers:</strong> Tony McNamara & Deborah Davis
        <br><strong>Cast:</strong> Emma Stone, Olivia Colman, Rachel Weisz, Nicholas Hoult...</p>
      <p><strong>Synopsis:</strong> England, early 18th century. The close relationship between Queen Anne and Sarah Churchill is threatened by the arrival of Sarah's cousin, Abigail Hill, resulting in a bitter rivalry between the two cousins to be the Queen's
        favourite.
      </p>
    </div>
    <div class="cover">
      <img src="https://picsum.photos/250/380">
    </div>
  </div>
</div>

问题分析与修复

问题根源

  1. .book 仅用 max-width 和 max-height 限制尺寸,没有固定宽高比约束,页面缩小时内部文本会撑开容器,导致比例变形。
  2. align-items: left 是无效CSS属性,正确写法应为 align-items: flex-start。
  3. transform: preserve-3d 写法错误,需改为 transform-style: preserve-3d 才能开启3D空间效果。

修复代码

修改后的核心CSS:

.book {
  position: relative;
  border-radius: 10px;
  max-width: 250px;
  /* 固定宽高比,保持250:380的比例 */
  aspect-ratio: 250/380;
  background-color: whitesmoke;
  box-shadow: 1px 1px 12px rgb(0, 0, 0, .7);
  border: 1px solid rgb(0, 0, 0, .2);
  /* 修正3D空间属性 */
  transform-style: preserve-3d;
  perspective: 2000px;
  display: flex;
  /* 修正对齐属性 */
  align-items: flex-start;
  justify-content: center;
  color: #000;
  flex-direction: column;
  margin: 0px;
  /* 防止内容溢出破坏布局 */
  overflow: hidden;
}

.book-content {
  margin-left: 45px;
  margin-right: 10px;
  padding-top: 10px;
  /* 内容过多时允许垂直滚动 */
  overflow-y: auto;
  /* 让内容区域占满卡片剩余高度 */
  flex: 1;
}

效果说明

  • aspect-ratio 强制卡片保持固定宽高比,无论页面如何缩放都不会变形。
  • overflow-y: auto 确保文本内容过多时,内部可滚动,不会撑爆卡片高度。
  • 修正后的3D属性和对齐属性让交互效果和布局更符合预期。

内容的提问来源于stack exchange,提问作者André

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 18:17:03