【发布时间】:2019-08-11 20:52:34
【问题描述】:
我在一个弹性容器中有 4 个项目。我想要每行 3 个。我希望容器居中——通过将容器与 justify-content 居中,第二行中的第 4 项也居中。所以我使用 ::after 技巧来尝试纠正它,但现在第二行与第一行不对齐。
在我看来,问题在于 flex 容器占用的宽度大于内容项,这迫使我将其与 justify-content 居中,这导致了这个问题。
使用网格会更容易吗?如果是这样怎么办?我想要一个简单的 iframe youtube 视频居中网格,每行 3 个,最后一行左对齐。
https://codepen.io/k4ir0s/pen/RdeejK
.section-vids ul {
list-style: none;
margin:0;
padding:0;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.section-vids ul li {
padding: 20px;
}
.section-vids ul:after {
content: "";
flex: auto;
}
<div class="container" id="media">
<section class="section-vids">
<ul>
<li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
<li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
<li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
<li><iframe width="560" height="315" src="https://www.youtube.com/embed/G1FFWgzk4vI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></li>
</ul>
</section>
</div>
【问题讨论】:
-
每个弹性项目都有一个宽度为 560 像素的 iframe - 这使得它在较小的屏幕宽度中不可能连续 3 个...