I am trying to do a grid of products using list items and inline-block
. The problem is: the content of the blocks have variable heights and the inline-block
doesn't keep the heights equal.
The code:
#blocks ul{
list-style-type:none;
margin:0px;
padding:0px;
}
#blocks li {
display:inline-block;
vertical-align:top;
width:280px;
background-color:#ff9933;
padding:13px;
margin: 0px 0px 20px 19px;
}
<div id="blocks">
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
Here's an image to illustrate the issue.
I need the blocks to keep the same height of the larger blocks, independently of its content. Is it possible to make someting like this?
And finally: Sorry, english is not my mother language :)
1. Adding the following to the
li
CSS will mimic the image example you provided.2. Also, here are some other approaches:
There's a W3C candidate layout called "flexbox" that takes care of this problem and many others. To achieve the equal height boxes you would simply assign the property
display: flex
to the UL anddisplay: block
to the LIs inside it.Example (CodePen)
It's not going to get you very far if you need to support older browsers :) but if you can get around that this method is easy and super cool.
Reference: A Complete Guide to Flexbox
First, if you adjust your margin to be on all 4 sides it will space out a little better on the flow to new line.
Second, you can either specify a min-height that is closer to something common for all areas, or run JavaScript to set them to the same on a line given a particular width.
I don't think there is a way, barring javascript, to do this; my recommendation would be to set a defined height and maybe an
overflow:auto
so in the case that content does overflow it does not cripple your site and your readers can still see your content.When you have more divs and thus more rows, without row-divs (container divs that mark a row), then the following example from CSS Tricks does what you need:
Equal Height Blocks in Rows
The following code has eight list items. When only three or four items are displayed per role, then the given example will make all divs equal in height per row.