gpt4 book ai didi

html - flex-start 和 baseline 有什么区别?

转载 作者:行者123 更新时间:2023-11-27 23:53:32 26 4
gpt4 key购买 nike

在使用 flex align-* 属性时,flex-startbaseline 有什么区别?

下面的代码片段为 align-self: flex-startalign-self: baseline 提供相同的输出。

align-self: flex-startalign-self: baseline 在哪些情况下会有不同的行为?

.flex-container {
color: white;
display: -webkit-flex;
display: flex;
width: 350px;
height: 200px;
background-color: yellow;
}
.flex-item {
background-color: green;
width: 50px;
min-height: 100px;
margin: 10px;
}
.item1 {
-webkit-align-self: flex-start;
align-self: flex-start;
}
.item2 {
-webkit-align-self: flex-end;
align-self: flex-end;
}
.item3 {
-webkit-align-self: center;
align-self: center;
}
.item4 {
-webkit-align-self: baseline;
align-self: baseline;
}
.item5 {
-webkit-align-self: stretch;
align-self: stretch;
}
<div class="flex-container">
<div class="flex-item item1">flex-start</div>
<div class="flex-item item4">baseline</div>
<div class="flex-item item2">flex-end</div>
<div class="flex-item item3">center</div>
<div class="flex-item item5">stretch</div>
</div>

最佳答案

请看下面两张图片。除了 align-items 规则外,两者的代码是相同的。

对齐元素:flex-start

enter image description here

对齐元素:基线

enter image description here

当使用 align-itemsalign-self 时,flex-start 值将在 cross-axis 的起始边缘对齐 flex 元素 flex 容器。

baseline 值将使 flex 元素沿其内容的baseline 对齐。

baseline

The line upon which most letters "sit" and below which descenders extend.

enter image description here

Source: Wikipedia.org

在许多情况下,当元素之间的字体大小相同(如问题中),或者内容在其他方面相同时,flex-startbaseline 将无法区分。

但是如果 flex 元素的内容大小不同,那么 baseline 会产生明显的差异。

就基线对齐发生的位置而言,这由行中最高的元素决定。

来自规范:

8.3. Cross-axis Alignment: the align-items and align-self properties

baseline

The flex item participates in baseline alignment: all participating flex items on the line are aligned such that their baselines align, and the item with the largest distance between its baseline and its cross-start margin edge is placed flush against the cross-start edge of the line.

.flex-container {
color: white;
display: flex;
height: 300px;
background-color: yellow;
justify-content: space-between;
align-items: baseline;
}
.flex-item {
background-color: green;
width: 110px;
min-height: 100px;
margin: 10px;
box-sizing: border-box;
padding: 5px;
text-align: center;
}
.item1 { font-size: 2em; }
.item2 { font-size: 7em; }
.item3 { font-size: .5em; }
.item4 { font-size: 3em; }
.item5 { font-size: 10em; }

/*
.item1 { align-self: flex-start; }
.item2 { align-self: flex-end; }
.item3 { align-self: center; }
.item4 { align-self: baseline; }
.item5 { align-self: stretch; }
*/

#dashed-line {
border: 1px dashed red;
position: absolute;
width: 100%;
top: 170px;
}
<div class="flex-container">
<div class="flex-item item1">A</div>
<div class="flex-item item2">B</div>
<div class="flex-item item3">C</div>
<div class="flex-item item4">D</div>
<div class="flex-item item5">E</div>
</div>

<div id="dashed-line"></div>

jsFiddle version

关于html - flex-start 和 baseline 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56391343/

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