Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you left justify list items in a unordered list that is centered within a div?

Tags:

html

css

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?

like image 242
Lani1234 Avatar asked Mar 03 '14 18:03

Lani1234


People also ask

How do you make an unordered list centered?

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.

How do I keep the content in the middle of a div?

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.


2 Answers

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;
}
like image 158
Dhiraj Avatar answered Sep 18 '22 05:09

Dhiraj


Just add

ul {
  text-align: left;
}
like image 24
Gareth Avatar answered Sep 20 '22 05:09

Gareth