垂直对齐和 CSS 网格关系
vertical align and CSS grid relationship
垂直对齐和 CSS 网格如何协同工作?我有一个元素紧挨着需要在中间精确对齐的元素。见下文:
.alphabet {
font-size: 80px;
/*Below is not working...why?*/
vertical-align: middle;
}
.letter-grid {
display: flex;
}
.filter-item-grid {
display: grid;
display: -ms-grid;
grid-template-columns: auto auto;
justify-content: space-between;
align-content: space-between;
}
.letter-grid__filter-item {
display: grid;
display: -ms-grid;
grid-template-rows: repeat(3,auto);
grid-auto-flow: column;
margin-left: 24px;
}
<section class="letter-grid">
<span class="alphabet">a</span>
<div class="filter-item-grid">
<div class="letter-grid__filter-item">
<h3 class="letter-grid__filter-title">
<a href="#">Example 1</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 2</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 3</a>
</h3>
</div>
</div>
</section>
在示例中,我将 alphabet
设置为 vertical-align: middle;
以尝试将 A 字符对齐到三个列表项的中间。 I referred to the vertical align documentation 上面写着:
The vertical-align property can be used in two contexts:
To vertically align an inline element's box inside its containing line
box. For example, it could be used to vertically position an img in
a line of text...
因为我使用的是 <span>
标签,是因为我只能在内联元素容器中使用垂直对齐,而不是 <section>
?
垂直对齐不是将中间的A字符与列表元素对齐的正确方式吗?
不,vertical-align
在没有行内行框时将不起作用。
在这种情况下,您已将 span 设为 flex-child,并有效地删除了 span 的 inline 性质。
我建议在 alphabet
跨度上使用 flexbox。
.alphabet {
font-size: 80px;
display: flex;
align-items: center;
}
.letter-grid {
display: flex;
}
.filter-item-grid {
display: grid;
display: -ms-grid;
grid-template-columns: auto auto;
justify-content: space-between;
align-content: space-between;
}
.letter-grid__filter-item {
display: grid;
display: -ms-grid;
grid-template-rows: repeat(3, auto);
grid-auto-flow: column;
margin-left: 24px;
}
section * {
border: 1px solid lightgrey;
}
<section class="letter-grid">
<span class="alphabet">a</span>
<div class="filter-item-grid">
<div class="letter-grid__filter-item">
<h3 class="letter-grid__filter-title">
<a href="#">Example 1</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 2</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 3</a>
</h3>
</div>
</div>
</section>
垂直对齐和 CSS 网格如何协同工作?我有一个元素紧挨着需要在中间精确对齐的元素。见下文:
.alphabet {
font-size: 80px;
/*Below is not working...why?*/
vertical-align: middle;
}
.letter-grid {
display: flex;
}
.filter-item-grid {
display: grid;
display: -ms-grid;
grid-template-columns: auto auto;
justify-content: space-between;
align-content: space-between;
}
.letter-grid__filter-item {
display: grid;
display: -ms-grid;
grid-template-rows: repeat(3,auto);
grid-auto-flow: column;
margin-left: 24px;
}
<section class="letter-grid">
<span class="alphabet">a</span>
<div class="filter-item-grid">
<div class="letter-grid__filter-item">
<h3 class="letter-grid__filter-title">
<a href="#">Example 1</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 2</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 3</a>
</h3>
</div>
</div>
</section>
在示例中,我将 alphabet
设置为 vertical-align: middle;
以尝试将 A 字符对齐到三个列表项的中间。 I referred to the vertical align documentation 上面写着:
The vertical-align property can be used in two contexts:
To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an img in a line of text...
因为我使用的是 <span>
标签,是因为我只能在内联元素容器中使用垂直对齐,而不是 <section>
?
垂直对齐不是将中间的A字符与列表元素对齐的正确方式吗?
不,vertical-align
在没有行内行框时将不起作用。
在这种情况下,您已将 span 设为 flex-child,并有效地删除了 span 的 inline 性质。
我建议在 alphabet
跨度上使用 flexbox。
.alphabet {
font-size: 80px;
display: flex;
align-items: center;
}
.letter-grid {
display: flex;
}
.filter-item-grid {
display: grid;
display: -ms-grid;
grid-template-columns: auto auto;
justify-content: space-between;
align-content: space-between;
}
.letter-grid__filter-item {
display: grid;
display: -ms-grid;
grid-template-rows: repeat(3, auto);
grid-auto-flow: column;
margin-left: 24px;
}
section * {
border: 1px solid lightgrey;
}
<section class="letter-grid">
<span class="alphabet">a</span>
<div class="filter-item-grid">
<div class="letter-grid__filter-item">
<h3 class="letter-grid__filter-title">
<a href="#">Example 1</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 2</a>
</h3>
<h3 class="letter-grid__filter-title">
<a href="#">Example 3</a>
</h3>
</div>
</div>
</section>