TransWikia.com

Make list evenly spaced and inline

Stack Overflow Asked by Nobert on December 9, 2021

I have a html list and I want to make it inline, centered and evenly spaced. Here is the closest I’ve gotten to the desired result:

li {
  display: inline;
  font-size: 20px;
}

div {
   text-align: center;
   width: 30vw;
}
<ul>
<li><div><a href="#">Item 1 Lorem Ipsum</a></div></li>
<li><div><a href="#">Item 2 Lorem Ipsum</a></div></li>
<li><div><a href="#">Item 3 Lorem Ipsum</a></div></li>
</ul>

I just can’t get them inline

Here is what i want:

4 Answers

For a general approach not using width try

.foo li {
  float: left;
  padding-right: 10px;
}
.foo li a {
  display: block;
 }

<ul>
<li class = "foo"><a href="#">Item 1 Lorem Ipsum</a></li>
<li class = "foo"><a href="#">Item 2 Lorem Ipsum</a></li>
<li class = "foo"><a href="#">Item 3 Lorem Ipsum</a></li>
</ul>

Answered by MIKE on December 9, 2021

Try to use inline-block and the following is its implementation also there is space between them

li {
  display: inline-block;
  font-size: 20px;
}

div {
   text-align: center;
   width: 30vw;
}
<ul>
<li><div><a href="#">Item 1 Lorem Ipsum</a></div></li>
<li><div><a href="#">Item 2 Lorem Ipsum</a></div></li>
<li><div><a href="#">Item 3 Lorem Ipsum</a></div></li>
</ul>

Answered by Umutambyi Gad on December 9, 2021

I think this is what you're looking for when you said:

inline, centered and evenly spaced

Note: You'll probably need to open this full-screen.

li {
  display: inline-block;
  text-align: center;
  font-size: 20px;
  width: 33%;
}

li a {
   width: 30vw;
}
<ul>
<li><a href="#">Item 1 Lorem Ipsum</a></li>
<li><a href="#">Item 2 Lorem Ipsum</a></li>
<li><a href="#">Item 3 Lorem Ipsum</a></li>
</ul>

Answered by Nathan Champion on December 9, 2021

It is because you have a wrapping div and this is preventing the inline for the li to work properly. If you remove the div (ununsed in your example) it works properly:

li {
  display: inline;
  font-size: 20px;
}

div {
   text-align: center;
   width: 30vw;
}
<ul>
<li><a href="#">Item 1 Lorem Ipsum</a></li>
<li><a href="#">Item 2 Lorem Ipsum</a></li>
<li><a href="#">Item 3 Lorem Ipsum</a></li>
</ul>

Answered by Lelio Faieta on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP