通过在flexbox中使用aptive-ratio的奇怪布局

发布于 2025-01-28 17:39:06 字数 1332 浏览 3 评论 0原文

dl 是flexbox, dd 是一些内容的盒子。

dt dd 相同,其内容宽度等于IT高度。


一个奇怪的结果是,当页面首次加载时, dt 的宽度不正确。

但是在回流后运行良好。发生在Chrome和Firefox中。

我不知道这是浏览器错误还是使用不正确。

section {
 display: inline-block;
 max-width: 100%;
 margin-right: 10px;
}

dl {
 display: flex;
 gap: 0 5px;
 background: #ddd;
 border-radius: 0 10px 10px 0;
 padding-right: 10px;
 margin: 0;
}
dt {
 flex: 0 0 auto;
 width: fit-content;
}
dd {
 margin: 0;
 flex: 1 1 auto;
}
.tag {
 height: 100%;
 background: #0ff;
 aspect-ratio: 1;
}
<section>
 <dl>
  <dt>
   <div class="tag">Test1</div>
  </dt>
  <dd>
   <p>Description 11111</p>
   <p>Description 22222</p>
  </dd>
 </dl>
</section>

<section>
 <dl>
  <dt>
   <div class="tag">Test2</div>
  </dt>
  <dd>
   <p>Description 11111</p>
  </dd>
 </dl>
</section>

codepen

The dl is flexbox, dd is a box of some content.

The dt is expected the same height with dd, and its content width is equal to it height.


A strange result is when the page first loaded, the width of dt isn't correct.

But it works well after a reflow. Happened both in Chrome and Firefox.

I don't know whether it's a browser bug or usage incorrect.

section {
 display: inline-block;
 max-width: 100%;
 margin-right: 10px;
}

dl {
 display: flex;
 gap: 0 5px;
 background: #ddd;
 border-radius: 0 10px 10px 0;
 padding-right: 10px;
 margin: 0;
}
dt {
 flex: 0 0 auto;
 width: fit-content;
}
dd {
 margin: 0;
 flex: 1 1 auto;
}
.tag {
 height: 100%;
 background: #0ff;
 aspect-ratio: 1;
}
<section>
 <dl>
  <dt>
   <div class="tag">Test1</div>
  </dt>
  <dd>
   <p>Description 11111</p>
   <p>Description 22222</p>
  </dd>
 </dl>
</section>

<section>
 <dl>
  <dt>
   <div class="tag">Test2</div>
  </dt>
  <dd>
   <p>Description 11111</p>
  </dd>
 </dl>
</section>

CodePen

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文