我正在学习 CSS 和 flexbox,并且了解到 margin collapsing。我知道像 Bootstrap 这样的框架避免了它(我理解并同意),但我想在特定情况下让边距折叠(您可以在上面的 CodePen 中看到)。这是一行带有顶部和底部 20 像素边距的标签。当该行断开时,边距不会折叠,因此在两行标签之间我得到 40px 的空白,但我希望与顶部或底部相同。
为什么边距不会折叠,最佳解决方法是什么?谢谢。
这是CodePen。
这是 HTML:
为什么边距不会折叠,最佳解决方法是什么?谢谢。
这是CodePen。
这是 HTML:
<main>
<div class="main-wrapper">
<div class="tags">
<ul>
<li>
<a href="#">ELEMENT 1</a>
</li>
<li>
<a href="#">ELEMENT 2</a>
</li>
<li>
<a href="#">ELEMENT 3</a>
</li>
<li>
<a href="#">ELEMENT 4</a>
</li>
<li>
<a href="#">ELEMENT 5</a>
</li>
</ul>
</div>
</div>
</main>
这是 CSS 代码:
$red: #FC575E;
$dark-grey: #3A4250;
$medium-grey: #e6e6e6;
$white: #FFFFFF;
$light-grey: #F1F4F5;
$width: 800px;
ul {
list-style-type: none;
padding-left: 0px;
}
a {
text-decoration: none;
}
main {
background-color: $light-grey;
padding: 30px 0px;
}
.main-wrapper {
max-width: $width;
margin: auto;
}
.tags {
ul {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
margin: 0;
}
li {
margin: 20px 10px;
}
a {
display: block;
justify-content: center;
color: $dark-grey;
background-color: $medium-grey;
border-radius: 20px;
padding: 7px 20px;
transition: background-color 0.3s ease, color 0.3s ease;
}
a:hover {
color: $white;
background-color: $red;
transition: background-color 0.3s ease, color 0.3s ease;
}
}