Reputation: 9
I have a flex container with 4 children and depending on the width of the screen, I want 1,2, or 3 posts to be displayed per row. I'm trying to set the widths of the posts at 100%, 50%, and 33.3% depending on the view but my question is - if the posts add up to 100% then how can I add spacing between the posts? Currently they're overflowing into the next row.
I've include my code below, but I want it to look similar to the posts section here: https://carney.co/daily-carnage/.
I've tried to add margin and padding but this causes the posts to overflow into the next row.
.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>
I want 1,2, or 3 posts to be displayed per row depending on the screen size. The widths of the posts should be set to 100%, 50%, and 33.3% but there should also be space in between them like on https://carney.co/daily-carnage/.
Upvotes: 0
Views: 768
Reputation: 148
What I've done in the past is wrap your child elements each in their own container then have that be 33.3% width or whatever then give it padding to add space between the actual children. Margin is added in addition to the width (so its actually 33.33% + 20px). Padding doesn't add to width. So something like this maybe
Another possibility is using width calc. so
width: calc(33.333% - 20px);
Just don't use calc with flex basis. It gets weird in IE.
Upvotes: 0
Reputation: 1395
You can use justify-content on the .container
and calculate the width you want minus the spacing you want.
.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>
Upvotes: 1