why is my justify-content: space-between not working on flex items?
css - Flexbox: justify-content: space-between? - Stack Overflow
html - How to justify content with space-between AND have everything centered? - Stack Overflow
Justify content: space-between; is not working. please explain
Videos
>> https://codepen.io/coderr11/pen/rNRwVmy
My code pen is above.
header is the parent, where i have placed: display: flex, align-items: center, and justify-content: space-evenly on it.
The child elements of it are .logo-image, .navbar, .btn and .fas - However, .logo-image takes up most of the width as seen by the red background and other flex items are squished to the left and are spaced-evenly. I can define a width on the .logo-image, but why is the default nature of it takes so much space compared to the other flex items?
Thank you
justify-content: space-between will automatically fill the space between the elements on the flex-axis. This means that 1. you have no control over the amount of space between the elements, the browser will calculate and fill as it sees fit; 2. only the space on the flex-axis (default: row; x-axis) is filled so the space below your first row is not filled automatically.
The solution is to go back to good ol' margins. Do note that margin behaves slightly different on flex items, i.e. margin: auto will fill the available space with a margin.
/* === RESERVATION === */
.reservation {
flex-direction: row-reverse;
}
/* === FLEXCONTAINER === */
.flexcontainer {
display: flex;
flex-direction: column;
align-items: center;
flex-wrap: wrap;
}
.flexcontainer p {
width: 500px;
}
.flexcontainer article {
display: flex;
}
.flexcontainer article img {
width: 500px;
margin: 24px;
margin-left: 0;
}
.flexcontainer article:nth-child(even) img {
margin: 24px;
margin-right: 0;
}
<section class="flexcontainer">
<article class="beliefs">
<img src="https://via.placeholder.com/500" alt="Our beliefs image" title="Our beliefs">
<div>
<h3>Our beliefs</h3>
<p>When eating...</p>
<p>We know...</p>
</div>
</article>
<article class="reservation">
<img src="https://via.placeholder.com/500" alt="reservation image" title="Reservation">
<div>
<h3>Reservation</h3>
<p>To fully...</p>
<p>This way...</p>
</div>
</article>
</section>
I would just add some padding.
/* === BELIEFS === */
.beliefs {
display: flex;
flex-direction: row;
justify-content: space-between;
}
/* === RESERVATION === */
.reservation {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
}
/* ==== SLOGAN === */
.slogan {
font-size: 1.4rem;
margin-bottom: 55px;
}
/* === FLEXCONTAINER === */
.flexcontainer {
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
}
section.flexcontainer p {
width: 500px;
}
section.flexcontainer img {
width: 500px;
height:375px;
}
section.flexcontainer article:nth-child(even) img {
padding-left:25px;
padding-bottom:25px;
}
section.flexcontainer article:nth-child(odd) img {
padding-right:25px;
padding-bottom:25px;
}
<section class="flexcontainer">
<article class="beliefs">
<img src="https://media.wired.com/photos/5926db217034dc5f91becd6b/master/w_1904,c_limit/so-logo-s.jpg" alt="Our beliefs image" title="Our beliefs">
<div>
<h3>Our beliefs</h3>
<p>When eating is motivated by pleasure, rather than hunger. A bit of italian tradition in the middle of
the
modern
world. A combination of traditional craftsmanship and the quality of “made in italy”.
</p>
<p>
We know your time is money. The spaces are reduced in this modern world. To meet your desires, in
every
time and
place, there we are that bring you a little moment of pleasure through spaces of your life.
</p>
</div>
</article>
<article class="reservation">
<img src="https://media.wired.com/photos/5926db217034dc5f91becd6b/master/w_1904,c_limit/so-logo-s.jpg" alt="reservation image" title="Reservation">
<div>
<h3>Reservation</h3>
<p>
To fully enjoy this experience you can call us on 646-755-8939 to book you table between 5.00
pm-11.30
pm
(between
11.30 am-11.30 pm on weekend).
</p>
<p>
This way we can reserve you a special spot in our warm italian atmosphere. We advise to call upfront
for
any large
group
</p>
</div>
</article>
</section>
Edit: I changed the css so it's more dynamic if another article is added.
The best solution would be to use margin: auto on the flex items horizontal margins as follows:
.flexbox {
background-color: pink;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.flex-item {
width: 175px;
height: 175px;
background-color: grey;
margin: 10px auto;
}
<div class="flexbox">
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
The only problem might be if the last row would have more than one item but not enough to fill up the whole row, there will be a lot of space between the last row items.
Seems you're after two behaviours at once. I've had success with justify-content: center and flex-wrap: wrap on the container, and setting an appropriate left and right margin on the children. Then apply that same amount as a negative margin on the container, and the edges of the children will line up with the container.
.parent {
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
margin: 0 -3rem;
}
.child {
align-self: center;
margin: 0 3rem 6rem 3rem;
}
The justify-content property uses the available space on the line to position flex items.
With justify-content: space-between, all available space is placed between the first and last items, pushing both items to opposite edges of the container.
You wrote:
I was wondering how to justify how much space is allowed in
justify-content: space-betweenfor flexbox.
With space-between in row-direction, you would have to control the width of the flex container. So if you want there to be less space between flex items, then you would need to shorten the width of the container.
Or you could use justify-content: space-around.
However, these solutions are suboptimal.
The right way to go about this would be to use margins.
You wrote:
Currently, my items are spaced but they have way too much space between them I want just a little space between them so they can settle somewhere in the middle in a row.
Use justify-content: center then use margins to space them apart.
My solution was :
- put dummy empty divs in between with a max-height specified
- change space-between to flex-start
- set the content blocks to nogrow
- set the dummy divs to grow
Then the spaces grow up to a max specified.