gpt4 book ai didi

html - 对齐元素 : what is the difference between flex-start and Stretch?

转载 作者:行者123 更新时间:2023-11-28 16:58:44 27 4
gpt4 key购买 nike

你能告诉我 align-items :flex-start 和 Stretch 有什么区别吗?

.container {
height: 500px;
padding: 10px;
margin: 50px auto;
background: #eee;
display: flex;
align-items: flex-end
}

.container .box {
padding: 10px;
width: 200px;
height: 200px;
background-color: #fff;
}
<div class="container">
<div class="box"></div>
</div>

最佳答案

如果您为您的元素设置高度,您将看不到任何区别。

stretch

Flex items are stretched such that the cross-size of the item's margin box is the same as the line while respecting width and height constraints.ref

在您的情况下,由于您设置的高度,拉伸(stretch)不会发生任何事情。

flex-start

The cross-start margin edges of the flex items are flushed with the cross-start edge of the line. ref

这只是将元素对齐到顶部。同样,视觉上不会发生任何事情,因为它在某种程度上是默认行为(不是默认值)

.container {
display:inline-flex;
width:200px;
height:200px;
border:2px solid;
}

.container > span {
width:100px;
height:100px;
background:red;
}
<div class="container" style="align-items:flex-start">
<span></span>
</div>
<div class="container" style="align-items:stretch">
<span></span>
</div>

现在去掉高度限制,你会看到区别:

.container {
display:inline-flex;
width:200px;
height:200px;
border:2px solid;
vertical-align:top;
}

.container > span {
width:100px;
min-height:100px;
background:red;
}
<div class="container" style="align-items:flex-start">
<span></span>
</div>
<div class="container" style="align-items:stretch">
<span></span>
</div>

关于html - 对齐元素 : what is the difference between flex-start and Stretch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54754161/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com