Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lay-out list items like a grid with CSS and HTML?

Tags:

html

css

I have a div block which does not have a fixed width.

Inside, I have an <ul> <li>..</li> block with 11 items. I would like these <li> items to be listed inside the div, all with equal widths like this:

##item##  ##item##  ##item##
##item##  ##item##  ##item##
##item##  ##item##  ##item##
##item##            ##item##

However, I can't sort it out at all.

I tried float left and right but the central 3 elements will not be centered.

What can I do to get this working?

Thanks!

like image 335
Phil Avatar asked Feb 14 '13 22:02

Phil


People also ask

How do you arrange a grid element in CSS?

With CSS grid layout, the grid itself within its container as well as grid items can be positioned with the following 6 properties: justify-items , align-items , justify-content , align-content , justify-self , and align-self .

Which CSS rule will you use to place a grid item into the grid so that it starts from the second column and second row and spans three columns and two rows?

Using the property grid-auto-flow with a value of column . In this case grid will add items in rows that you have defined using grid-template-rows . When it fills up a column it will move onto the next explicit column, or create a new column track in the implicit grid.


1 Answers

The "Inline Blocks" link that Jordan posted is a great resource, particularly when it comes to older browser support. For quick reference for others landing on this page off of google, the basic css for creating a centered, inline-block grid is:

ul {
    margin: 0 auto;
    text-align: center;
}

li {
    display: inline-block;
    vertical-align: top;
}
like image 69
FreedomMan Avatar answered Nov 10 '22 01:11

FreedomMan