How to make a <ul> display in a horizontal row
Asked Answered
D

9

135

How can I make my list items appear horizontally in a row using CSS?

#div_top_hypers {
    background-color:#eeeeee;
    display:inline;      
}
#ul_top_hypers {
    display: inline;
}
<div id="div_top_hypers">
    <ul id="ul_top_hypers">
        <li>&#8227; <a href="" class="a_top_hypers"> Inbox</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Compose</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Reports</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Preferences</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> logout</a></li>
    </ul>
</div>
Deadhead answered 20/5, 2009 at 0:36 Comment(0)
S
183

List items are normally block elements. Turn them into inline elements via the display property.

In the code you gave, you need to use a context selector to make the display: inline property apply to the list items, instead of the list itself (applying display: inline to the overall list will have no effect):

#ul_top_hypers li {
    display: inline;
}

Here is the working example:

#div_top_hypers {
    background-color:#eeeeee;
    display:inline;      
}
#ul_top_hypers li{
    display: inline;
}
<div id="div_top_hypers">
    <ul id="ul_top_hypers">
        <li>&#8227; <a href="" class="a_top_hypers"> Inbox</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Compose</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Reports</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Preferences</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> logout</a></li>
    </ul>
</div>
Suellen answered 20/5, 2009 at 0:37 Comment(5)
I've also achieved this effect by using float, thereby keeping the block nature of the list items.Newel
That's an interesting approach, I have to say—however, I think that would create some unnecessary hassles with margins and such, since you're effectively lifting the list items out of the box model.Suellen
@htw: You could kick it back into gear with any of the clearfix solutions.Fumble
You could always use display:inline-block if you wanted to retain the block nature... though it's not completely supported at this stage (I believe it's Fx2 that's the major culprit).Schadenfreude
where is flexbox of doing that ? like >> flex-direction: row;Keeney
F
21

You could also set them to float to the right.

#ul_top_hypers li {
    float: right;
}

This allows them to still be block level, but will appear on the same line.

Fumble answered 20/5, 2009 at 1:1 Comment(2)
Floating them right will have an extra affect: it will swap the order of them so from left to right they will be last to first.Barony
Ah yes, they'll need to be reversed in the markup (so much for separation of layout/markup!)Fumble
A
17

As others have mentioned, you can set the li to display:inline;, or float the li left or right. Additionally, you can also use display:flex; on the ul. In the snippet below I also added justify-content:space-around to give it more spacing.

For more information on flexbox, checkout this complete guide.

#ul_top_hypers {
    display: flex;
    justify-content:space-around;
    list-style-type:none;
}
<div id="div_top_hypers">
    <ul id="ul_top_hypers">
        <li>&#8227; <a href="" class="a_top_hypers"> Inbox</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Compose</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Reports</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> Preferences</a></li>
        <li>&#8227; <a href="" class="a_top_hypers"> logout</a></li>
    </ul>
</div>
Acree answered 2/9, 2016 at 19:23 Comment(0)
T
12

Set the display property to inline for the list you want this to apply to. There's a good explanation of displaying lists on A List Apart.

Tape answered 20/5, 2009 at 0:40 Comment(0)
W
10

As @alex said, you could float it right, but if you wanted to keep the markup the same, float it to the left!

#ul_top_hypers li {
    float: left;
}
Wendall answered 18/6, 2013 at 14:41 Comment(0)
C
10

It will work for you:

#ul_top_hypers li {
    display: inline-block;
}
Collectivism answered 21/10, 2016 at 12:11 Comment(0)
A
5
#ul_top_hypers li {
  display: flex;
}
Allude answered 14/6, 2017 at 5:42 Comment(0)
P
1

If you want to align list items(li) horizontally without affecting list style use below mentioned properties. ul{ display:flex; gap:30px; } gap:30px this used to set the gap between the list items.

Phrase answered 27/2, 2022 at 17:23 Comment(0)
Y
0

To be more specific use this:

#div_top_hypers ul#ul_top_hypers li{
display: inline;
}
Yeorgi answered 18/7, 2022 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.