我有一个带有4个孩子的Flex容器,具体取决于屏幕的宽度,我希望每行显示1,2或3个帖子。我试图将帖子的宽度设置为100%,50%和33.3%,具体取决于视图,但我的问题是 - 如果帖子总计高达100%,那么我该如何在帖子之间添加间距?目前,他们溢出了下一行。
我已经在下面包含我的代码,但是我希望它看起来与此处的帖子部分相似:https://carney.co/daily-carnage/。
我尝试添加边距和填充,但这会导致帖子溢出到下一行。
.container {
max-width: 1140px;
padding-left: 2rem;
padding-right: 2rem;
margin-right: auto;
margin-left: auto;
box-sizing: border-box;
display: flex;
flex-wrap: wrap;
}
article {
box-shadow: 0 0 0.6rem 0.25rem rgba(0, 0, 0, .1);
margin: 2rem 0;
width: 100%;
}
.post-meta-and-title {
padding: 2rem 1.5rem 0;
}
.post-meta {
font-size: .7rem;
}
.post-title {
font-size: 1.25rem;
}
.post-description {
padding: 0 1.5rem;
}
footer {
position: relative;
padding: 0 1.5rem 2rem;
}
.btn {
background-image: linear-gradient(150deg, #ffb064 -1%, #ff6496 101%);
color: white;
position: absolute;
right: -1rem;
bottom: -1rem;
padding: .5rem 2rem;
line-height: 1.5;
}
.author-name {
display: inline;
}
span {
padding: 0 .4rem;
}
@media (min-width: 810px) {
.container {
padding: 0;
}
article {
width: 50%;
}
@media (min-width: 1200px) {
article {
width: 33.3%;
margin-right: 3rem;
}
}
<section class="container">
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">Sky High WiFi</a></h2>
</header>
<p class="post-description">plus – better landing pages, heatmaps, and Starbucks.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">Are you afraid of clowns?</a></h2>
</header>
<p class="post-description">plus – tech overload, productivity, and Tom Brady.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">It's time to get real folks</a></h2>
</header>
<p class="post-description">plus – sell more event tickets, and faster feedback.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">Burger King goes plant-based</a></h2>
</header>
<p class="post-description">plus – how to create content for boring industries.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
</section>
我想要1,2或根据屏幕尺寸显示每行3个帖子。这些帖子的宽度应设置为100%,50%和33.3%,但在其中也应该有空间,例如https://carney.co/daily-carnage/。
您可以在.container
上使用合理性,并计算要减去所需间距的宽度。
.container {
max-width: 1140px;
padding-left: 2rem;
padding-right: 2rem;
margin-right: auto;
margin-left: auto;
box-sizing: border-box;
display: flex;
flex-wrap: wrap;
justify-content: space-between; /* ADDED */
}
article {
box-shadow: 0 0 0.6rem 0.25rem rgba(0, 0, 0, .1);
margin: 2rem 0;
width: 100%;
}
.post-meta-and-title {
padding: 2rem 1.5rem 0;
}
.post-meta {
font-size: .7rem;
}
.post-title {
font-size: 1.25rem;
}
.post-description {
padding: 0 1.5rem;
}
footer {
position: relative;
padding: 0 1.5rem 2rem;
}
.btn {
background-image: linear-gradient(150deg, #ffb064 -1%, #ff6496 101%);
color: white;
position: absolute;
right: -1rem;
bottom: -1rem;
padding: .5rem 2rem;
line-height: 1.5;
}
.author-name {
display: inline;
}
span {
padding: 0 .4rem;
}
@media (min-width: 810px) {
.container {
padding: 0;
}
article {
width: calc(50% - 4rem); /* USE CALC */
}
@media (min-width: 1200px) {
article {
width: calc(33.3% - 4rem); /* USE CALC */
}
}
<section class="container">
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">Sky High WiFi</a></h2>
</header>
<p class="post-description">plus – better landing pages, heatmaps, and Starbucks.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">Are you afraid of clowns?</a></h2>
</header>
<p class="post-description">plus – tech overload, productivity, and Tom Brady.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">It's time to get real folks</a></h2>
</header>
<p class="post-description">plus – sell more event tickets, and faster feedback.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
<article>
<header class="post-meta-and-title">
<div class="post-meta">
<time datetime="2019-05-09 20:00">May 9, 2019</time>
<p class="author-name"><span>|</span>
<a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
</p>
</div>
<h2><a class="post-title" href="#">Burger King goes plant-based</a></h2>
</header>
<p class="post-description">plus – how to create content for boring industries.</p>
<footer>
<a href="#" class="btn">READ MORE</a>
</footer>
</article>
</section>
我过去所做的是将您的孩子元素包裹在自己的容器中,然后将其宽度为33.3%或其他任何东西,然后给它填充以在实际孩子之间增加空间。除了宽度外,还添加了边距(因此其实际上是33.33% 20px(。填充不会增加宽度。所以这样的东西也许
另一种可能性是使用宽度计算。所以
width: calc(33.333% - 20px);
只是不要将计算与flex基础使用。IE中很奇怪。