I have an unordered list and each list element contains a photo and a headline. My CSS sets up the grid to be a grid, where each row contains three photos. Sometimes the headline (or photo caption) is longer than the width of the photo and has to span two lines. In some situations, when the text spans 2+ lines, the list element below it gets pushed to the right and there is a big gap in the list. The only fix that works for me is adding the following HTML <div style="clear:both"></div> after every three <li></li> elements. The issue can be seen in the third row of the list elements. I’ve tried researching this issue, but have not found a CSS only method. In my example code, I applied the CSS clearfix class, but it doesn’t seem to have any effect.
I’m using the latest version of Google Chrome.
Here is my code: http://jsfiddle.net/NVveP/1/
Having both
float: leftanddisplay: inline-blockwill in effect nullifydisplay: inline-block, becausefloat: leftforcesdisplay: block.Hence, removing
float: leftallowsdisplay: inline-blockto work, which combined withvertical-align: topis how you can achieve your desired layout.See: http://jsfiddle.net/thirtydot/NVveP/3/
I also added a hack to make
display: inline-blockwork in IE7, if that matters.It would be more difficult to do this with floats. You’d need something to the effect of:
Which has the problem of not working in older browsers such as IE7/8. Fortunately, there’s no need to worry about this because
display: inline-blockis the solution here, not floats.