Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make <li> elements flow left-to-right instead of top-to-bottom?

Tags:

html

css

Here are my <li> elements. I want them to appear left-to-right instead of top-to-bottom.

<div class="nav">
  <ul>
    <li><a href="test">test</a></li>
    <li><a href="test">test</a></li>
    <li><a href="test">test</a></li>
    <li><a href="test">test</a></li>
    <li><a href="test">test</a></li>
  </ul>
</div>

How can I do this?

EDIT:how to control spacing between each li element and its bachground color and spacing between a element and border of li element?

like image 440
omg Avatar asked Jun 24 '09 17:06

omg


People also ask

How do I move UL to left in HTML?

You can add padding: 0 to the ul element to force it to stick to the left border of the parent nav element.


4 Answers

i think you are looking for

li {
  display:inline;
}
like image 192
mkoryak Avatar answered Oct 01 '22 11:10

mkoryak


There's a few ways.

One way is display:inline as the other posts mention

Another way is float:left;

edit: more detail! try this out in a page

<style>
/* style 1 */
div.nav1 ul li
{
  display:inline;

  border: solid 1px black;
  padding: 10px;
  margin: 10px;

  list-style-type: none;
  line-height: 4em;
}

/* style 2 */
div.nav2 ul li
{
  float: left;
  border: solid 1px black;
  padding: 10px;
  margin: 10px;

  list-style-type: none;
}
</style>

<h1>using display: inline;</h1>
<div class="nav1">
<ul>
<li><a href="#">inline</a></li>
<li><a href="#">inline blah bla</a></li>
<li><a href="#">i am not so neat on</a></li>
<li><a href="#">inline</a></li>
<li><a href="#">wrapping and I probably</a></li>
<li><a href="#">inline</a></li>
<li><a href="#">need a bit of work and tweaking</a></li>
<li><a href="#">inline</a></li>
<li><a href="#">the "line-height" css property</a></li>
<li><a href="#">inline</a></li>
<li><a href="#">to make me wrap better</a></li>
<li><a href="#">inline</a></li>
<li><a href="#">also notice how i tend to break a line up into .. two.  see?  make your browser narrow.</a></li>
<li><a href="#">inline</a></li>
</ul>
</div>

<hr />

<h1>using float:left;</h1>
<div class="nav2">
<ul>
<li><a href="#">floated left</a></li>
<li><a href="#">i tend to behave</a></li>
<li><a href="#">floated left</a></li>
<li><a href="#">better for wrapping</a></li>
<li><a href="#">floated left</a></li>
<li><a href="#">when the list</a></li>
<li><a href="#">floated left</a></li>
<li><a href="#">reaches the edge of the page</a></li>
<li><a href="#">floated left</a></li>
</ul>
</div>
like image 21
bobobobo Avatar answered Oct 01 '22 11:10

bobobobo


Try:

<style>
.nav li {display:inline;}
</style>
<div class="nav">
        <ul>
                <li><a href="test">test</a></li>
                <li><a href="test">test</a></li>
                <li><a href="test">test</a></li>
                <li><a href="test">test</a></li>
                <li><a href="test">test</a></li>
        </ul>
</div>

The styles should really be in an external style sheet, I just put them on the page for simplicity.

like image 23
D'Arcy Rittich Avatar answered Oct 01 '22 11:10

D'Arcy Rittich


You could use the float attribute:

.nav ul li{float:left;}
like image 34
James Conigliaro Avatar answered Oct 01 '22 09:10

James Conigliaro