Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS Making a list be in rows, 3 boxes per row?

Tags:

html

css

<ul>
    <li>item1</li>
    <li>item1</li>
    <li>item1</li>
    <li>item1</li>
    <li>item1</li>
    <li>item1</li>
</ul>

Let's say each box is 150px wide, so total width per row is 450px. What I am trying to do is, automatically make the list allow only 3 boxes (list elements) per row, and not make it go under each other.

I've tried something, and not just asking before trying!

There's my attempt:

<div class="container">
    <ul>
        <li>hreyyy</li>
        <li>hreyyy</li>
        <li>hreyyy</li>
        <li>hreyyy</li>
        <li>hreyyy</li>
        <li>hreyyy</li>
    </ul>
</div>

.container {
    width: 450px;
}

.container ul li {
    width: 150px;
    height: 100px;
    background-color: red;
    float: left;
}

.container ul {
    display: inline;
}

Result:

img
(source: gyazo.com)

It' doesn't function as I wanted, why?

Is there a plugin for it that makes life easier?

like image 675
Jony Kale Avatar asked Aug 21 '13 21:08

Jony Kale


People also ask

How do you put 3 boxes side by side in HTML?

Three or more different div can be put side-by-side using CSS in the same div. This can be achieved with flexbox – but note that you will need to use wrapper divs and apply different flex-directions to each in order to make the grid layout work. Use CSS property to set the height and width of div.

How do you make 3 divs in one row?

Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.

How do I put things in the same row in HTML?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.


1 Answers

You don't need to set the UL to display as an inline object. Instead, do something like this:

.container {
    width: 450px;
}
.container ul {
    padding: 0; /* remove default padding and all margins! */
    margin: 0;
    list-style-type: none; /* remove the • */
}
.container ul li {
    width: 150px;
    height: 100px;
    background-color: red;
    float: left;
}

Result: http://jsfiddle.net/f896G/

like image 156
Leonard Avatar answered Jan 02 '23 17:01

Leonard