Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to justify content with space-between AND have everything centered?

Tags:

html

css

flexbox

I'm running into a problem with flexbox.

I have a div that has a max-width of 920px. I want the boxes of content to fill up the div from left to right to the max possible width, with everything having equal margins. When the screen-size goes down to one box per row, I want that box to be centered on the screen.

Here is the site in action: http://javinladish.com/code/index.html

If I use:

justify-content: center; 

Then the boxes don't fill up the max width.

If I use:

justify-content: space-between; 

Then the boxes don't stay centered when I go down to one box per row.

How can I achieve a happy balance between the two? I.e filling up the max width of the container, and keeping all content centered?

like image 730
javinladish Avatar asked Mar 05 '14 18:03

javinladish


People also ask

How do you center flex items and give them space between each item at the same time?

To set space between the flexbox you can use the flexbox property justify-content you can also visit all the property in that link. We can use the justify-content property of a flex container to set space between the flexbox.

What is justify content space evenly?

The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.

Can I use justify content and align items together?

The justify-content property accepts the same values as align-content . In the example below, the value of justify-content is space-between . The available space after displaying the items is distributed between the items. The left and right item line up flush with the start and end.


2 Answers

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.

like image 152
martin schwartz Avatar answered Sep 21 '22 10:09

martin schwartz


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; }  
like image 26
Isaac F Avatar answered Sep 22 '22 10:09

Isaac F