How to make a HTML list appear horizontally instead of vertically using CSS only?
Asked Answered
B

4

45

I need this because I want to make a menu (which is made from a HTML list) appear horizontally.

I prefer not to use absolute positioning since it might become messy when I start changing the layout of the page.

I would like also to remove the indenting of the sub-lists. Is it possible?

Beaut answered 27/1, 2010 at 7:30 Comment(0)
A
74

You will have to use something like below

#menu ul{
  list-style: none;
}
#menu li{
  display: inline;
}
	
<div id="menu">
  <ul>
    <li>First menu item</li>
    <li>Second menu item</li>
    <li>Third menu item</li>
  </ul>
</div>
Antinucleon answered 27/1, 2010 at 7:38 Comment(3)
Will (display:inline;) remove the indenting for the sub-lists?Beaut
No. For that, you want #menu li li{margin-left: 0; padding-left: 0;}Tessy
Like a charm had to tweak it a bit for my use-case but a solid answer.Distinctive
O
23

Using display: inline-flex

#menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: inline-flex
}
<div id="menu">
  <ul>
    <li>1 menu item</li>
    <li>2 menu item</li>
    <li>3 menu item</li>
  </ul>
</div>

Using display: inline-block

#menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

#menu li {
  display: inline-block;
}
<div id="menu">
  <ul>
    <li>1 menu item</li>
    <li>2 menu item</li>
    <li>3 menu item</li>
  </ul>
</div>
Occasionalism answered 23/6, 2014 at 7:13 Comment(0)
C
13

quite simple:

ul.yourlist li { float:left; }

or

ul.yourlist li { display:inline; }
Clitoris answered 27/1, 2010 at 7:31 Comment(0)
A
1

You can directly use inline styling something like this

<ul style="list-style: none; margin: 0; padding: 0;">
  <li style="display: inline">A</li>
  <li style="display: inline">B</li>
  <li style="display: inline">C</li>
  <li style="display: inline">D</li>
</ul>
Ashcroft answered 18/5, 2021 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.