Skip to content Skip to sidebar Skip to footer

Css - Html Lists - Center Ul In Page & Left Align Li Items

I have this code: I want to center align the entire ul container, while keeping the li items aligned to left. So instead of this: I want to have something like this: I can't see

Solution 1:

Try this:

.menu {
padding: 0;
list-style-type: none;
width: 500px; /* Change this value is you need more li in the same line. You can also use percents *//*width: 90% */display: block;
margin-right: auto;
margin-left: auto;
}
.item {
width: 60px;
text-align: center;
font-size: 4em;
background-color:red;
display: inline-block;
margin: 5px;
padding: 25px;
}

Solution 2:

Since you are using pixels with .item, I think this will suites best:

.menu {
  min-width: 300px;
  max-width: 90%;
  margin: auto;
  padding: auto;
  list-style-type: none;
}
.item {
  width: 60px;
  text-align: center;
  font-size: 4em;
  background-color: red;
  display: inline-block;
  margin: 5px;
  padding: 25px;
}

Basically i just added measurements for the UL element. Hope i helped and Good Luck!

Solution 3:

The only solution I found, which still allowed the entire UL to take as much as possible from the browser width (for responsiveness) while still centered, was with:

ul.menu {
   margin: 0 auto;
   width: {x}px
}

and using @media queries to set the perfect width for different screen sizes.

Solution 4:

Try surounding you element in div (or whatever else) with display: flex. Margin: auto property only works on flex.

Post a Comment for "Css - Html Lists - Center Ul In Page & Left Align Li Items"