Center
  • into div

2019-01-02 17:40发布

After some search, I did not find a proper way to center a list of <li> into a fixed width div.

Take a look at the page.. It doesn't work either!

16条回答
梦寄多情
2楼-- · 2019-01-02 18:23

Steps :

  1. Write style="text-align:center;" to parent div of ul
  2. Write style="display:inline-table;" to ul
  3. Write style="display:inline;" to li

or use

<div class="menu">
 <ul>
   <li>item 1 </li>
   <li>item 2 </li>
   <li>item 3 </li>
 </ul>
</div>

<style>
 .menu { text-align: center; }
 .menu ul { display:inline-table; }
 .menu li { display:inline; }
</style>
查看更多
裙下三千臣
3楼-- · 2019-01-02 18:23

Just add text-align: center; to your <ul>. Problem solved.

查看更多
呛了眼睛熬了心
4楼-- · 2019-01-02 18:27

Since ul and li elements are display: block by default — give them auto margins and a width that is smaller than their container.

ul {
    width: 70%;
    margin: auto;
}

If you've changed their display property, or done something that overrides normal alignment rules (such as floating them) then this won't work.

查看更多
孤独寂梦人
5楼-- · 2019-01-02 18:28

Another option is:

HTML

<nav>
  <ul class = "main-nav"> 
   <li> Productos </li>
   <li> Catalogo </li>
   <li> Contact </li>  
   <li> Us </li>
  </ul>
</nav>    

CSS:

nav {
  text-align: center;
}

nav .main-nav li {
  float: left;
  width: 20%;
  margin-right: 5%;
  font-size: 36px;
  text-align: center;
}
查看更多
登录 后发表回答