Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Keep all flexbox children elements the same size

Tags:

css

flexbox

My code is shown in this JSFiddle:

#container {     align-content: flex-start;     background-color: red;     display: flex;     flex-flow: row wrap;     height: 200px;     justify-content: space-around;     padding: 10px;     width: 400px; }  .child-item {     background-color: yellow;     flex-basis: 75px;     flex-grow: 1;     height: 20px;     margin: 10px;     width: 75px; }
<div id="container">     <div class="child-item"></div>     <div class="child-item"></div>     <div class="child-item"></div>     <div class="child-item"></div>     <div class="child-item"></div>     <div class="child-item"></div> </div>

flexbox grid

Although, I would like the two items across the bottom to have the same with as the items on the top.

I don't want to float them left and fix their width, this will not work for my arrangement in other resolutions.

like image 580
TaylorMac Avatar asked Aug 26 '14 20:08

TaylorMac


People also ask

How do you make flexed items the same size?

you can try set flex-basis: 50%; and flex-grow: 1 . Show activity on this post. You can get the items to stop expanding with max-width on the elements (fiddle).

How do you make flexbox children 100% height of their parents using CSS?

Getting the child of a flex-item to fill height 100%Set position: absolute; on the child. You can then set width/height as required (100% in my sample).

How do I fix my flexbox size?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.


1 Answers

You have 2 ways of doing this:

flex-grow: 0 (fiddle)

If you set flex-grow to 0 you are telling the items to keep their width and expand the empty space. Using flex-grow: 1 the items will expand the width to fit the space.

invisible items (fiddle)

Add some invisible items after the normal items with the same flex properties. You will get a left aligned look.

In this solution there will be the same number of items in all visible rows so make sure to add enough invisible items to work properly on larger screens.

like image 138
Igor Avatar answered Oct 03 '22 18:10

Igor