Flexbox容器中的CSS纵横比

发布于 2025-01-19 06:08:45 字数 708 浏览 4 评论 0原文

我正在尝试制作div,以填充其父级的所有可用高度,同时保持16/9的长宽比。但是,它填充了宽度,因此无法保持我设置的纵横比。关于如何解决此问题有什么建议吗?

<div class="outer">
  <div class="problem"></div>
  <div class="inner"></div>
</div>
.outer {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.problem {
  height: 200px;
}
.inner {
  aspect-ratio: 16/9;
  background-color: blue;
}

html,body {
  margin: 0;
  font-family: sans-serif;
}

jsfiddle: https://jsfiddle.net/cqhtb8sw/

I'm trying to make a div that fills all available height of its parent, while maintaining a 16/9 aspect ratio. However, it's filling the width and thus not keeping the aspect ratio I've set. Any advice on how to fix this?

<div class="outer">
  <div class="problem"></div>
  <div class="inner"></div>
</div>
.outer {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.problem {
  height: 200px;
}
.inner {
  aspect-ratio: 16/9;
  background-color: blue;
}

html,body {
  margin: 0;
  font-family: sans-serif;
}

JSFiddle: https://jsfiddle.net/cqhtb8sw/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2025-01-26 06:08:45

这是由于Flexbox所致,您可以在内部样式中添加它:

  height:100%;
  width: min-content;

It's due to flexbox, you can add this in your inner styles:

  height:100%;
  width: min-content;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文