Mobile app version of vmapp.org
Login or Join
Cooney921

: Can't center a ul with css Guys I'm trying to center this simple ul here. Any idea on whats wrong ? Css: ul { margin-left: auto; margin-right: auto; } li { display: inline;

@Cooney921

Posted in: #Css

Guys I'm trying to center this simple ul here. Any idea on whats wrong ?

Css:

ul {
margin-left: auto;
margin-right: auto;
}

li {
display: inline;
background-color: gray;
color: white;
list-style: none;
margin: 5px;
padding: 5px;
}


JSP/HTML:


<ul>
<li>Login</li>
<li>Option2</li>
<li>Optino3</li>
</ul>

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Cooney921

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

Because even though the ul centers (with the margin: auto) it's a block element so it spans the entire width of the element. There is no room for margin on either side of it, you'll have to specify a width for the ul element for this to work.

10% popularity Vote Up Vote Down


 

@YK1175434

Check matthewjamestaylor.com/blog/2 it has many HTML/CSS articles.

EDIT

Again, nothing against being downvoted. But knowing why downvotes (as well as upvotes, unless it is really really obvious) is important indeed to everyone, including me.

Despite the question best fits in other forum (yeah, I commented on question first), I tried a nice reply as per QA seems to be a beginner. And for beginners, I think the best is not just solve the problem, but learn the basics.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme