tags:

views:

704

answers:

3

How can I make a ul list's item appears horizontally in a row using CSS?

here is code

<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">  Perefrences</a></li>
                <li>&#8227; <a href="" class="a_top_hypers">  logout</a></li>
            </ul>
        </div>

--

#div_top_hypers{
    background-color:#eeeeee;
        display:inline;

}
#ul_top_hypers{

  display: inline;




}
+5  A: 

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;
}
htw
I've also achieved this effect by using float, thereby keeping the block nature of the list items.
Joel Potter
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.
htw
@htw: You could kick it back into gear with any of the clearfix solutions.
alex
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).
James Burgess
+3  A: 

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.

Paul Morie
+1 for the link.
musicfreak
+2  A: 

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.

alex
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.
Matthew James Taylor
Ah yes, they'll need to be reversed in the markup (so much for separation of layout/markup!)
alex