I have a div within a div within a div:
<div id="wrapper" class="center">
<div id="content" class="center">
<div id="listDiv" class="center">
<ul>
<li><a href='#' id="1" >Link one</a> </li>
<li><a href='#' id="2" >Link 2</a> </li>
<li><a href='#' id="3" >Link three</a> </li>
</ul>
</div>
</div>
</div>
In my style sheet, I have a class that centers each div within the page, and centers things within the div:
div.center{
margin-left: auto;
margin-right: auto;
text-align: center;
}
I would like my unordered list to remain centered in the div, but for each list item to be left justified below the other. Everything I have tried centers the list items to each other. What am I missing?
To center align an unordered list, you need to use the CSS text align property. In addition to this, you also need to put the unordered list inside the div element. Now, add the style to the div class and use the text-align property with center as its value.
You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
May be, you want this. SEE THE DEMO
To achieve what you want, you have to give display: table;
to your div.
div.center {
margin-left: auto;
margin-right: auto;
display: table;
}
ul {
text-align: left;
}
Just add
ul {
text-align: left;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With