CSS - HTML Lists - center UL in page & left align LI items

流过昼夜 提交于 2019-12-11 13:20:02

问题


I have this code:

.menu {
    margin: auto;
    padding: 0;
    list-style-type: none;
}
.item {
    width: 60px;
    text-align: center;
    font-size: 4em;
    background-color:red;
    display: inline-block;
    margin: 5px;
    padding: 25px;
}
<ul class="menu">
  <li class="item">1</li>
  <li class="item">2</li>
  <li class="item">3</li>
  <li class="item">4</li>
  <li class="item">5</li>
  <li class="item">6</li>
  <li class="item">7</li>
  <li class="item">8</li>
  <li class="item">9</li>
  <li class="item">10</li>
</ul>

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 seem to make it work..

Any help would be appreciated.

Thanks in advance!


回答1:


You can also try the following flex box solution:

* {
  margin: 0;
  padding: 0;
}

body {
  width: 100vw;
  height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.menu {
  position: relative;
  list-style: none;
  width: 80vw;
  display: flex;
  flex-flow: row wrap;
  justify-content: flex-end; 
  background: green;
}

.item {
  width: 15vw;
  height: 15vw;
  background: red;
  margin: 1vw;
  /*the following is only for styling the numbers*/
  font-size: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
}
<ul class="menu">
  <li class="item">1</li>
  <li class="item">2</li>
  <li class="item">3</li>
  <li class="item">4</li>
  <li class="item">5</li>
  <li class="item">6</li>
  <li class="item">7</li>
  <li class="item">8</li>
  <li class="item">9</li>
  <li class="item">10</li>
</ul>



回答2:


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;
}



回答3:


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!




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/47492844/css-html-lists-center-ul-in-page-left-align-li-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!