li元素填充ul宽度,水平菜单中元素之间的间距相同

时间:2018-03-05 08:32:02

标签: html css

我使用ul作为主菜单容器进行水平菜单导航,使用li作为菜单列表项,我使用ul的显示表和li的表格单元格{1}},所以当每个li适合ul宽度时,问题是每个单词菜单项之间的差距是不一样的,原因是因为我在li元素之间设置填充但是如果我摆脱了填充,它根本没有任何差距,我需要保持每个单词之间的差距菜单项是相同的,即使在li元素内有单词分隔符,并且我需要保持显示器的响应能力。

enter image description here

Demo

css代码:

body{
  background-color: #ff2;
  margin: 0px !important;
}
ul { 
    display:table;
    width:100%;
    box-sizing:border-box; 
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    background-color: #F25800;
    padding: 0 10px;
}
li { 
    display:table-cell; 
    text-align:center;
    padding: 10px 12px;
} 
li:hover{
  background-color: #64b448;
}
li a{
  text-decoration:none;
}
li a span{
  font-size: 13px; 
  color: #fff;
}

html代码:

<ul>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-rumah.html" class="level-top"><span>Kebutuhan Rumah</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-pribadi.html" class="level-top"><span>Kebutuhan Pribadi</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/makanan-minuman.html" class="level-top"><span>Makanan & Minuman</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-kantor.html" class="level-top"><span>Kebutuhan Kantor</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/otomotif.html" class="level-top"><span>Otomotif</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/listrik-teknik.html" class="level-top"><span>Listrik & Teknik</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/alat-bangunan.html" class="level-top"><span>Alat Bangunan</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/elektronik.html" class="level-top"><span>Elektronik</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/pakaian.html" class="level-top"><span>Pakaian</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/pecah-belah.html" class="level-top"><span>Pecah Belah</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kesehatan.html" class="level-top"><span>Kesehatan</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/mainan-hobi.html" class="level-top"><span>Mainan & Hobi</span></a>
   </li>
</ul>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
body{
  background-color: #ff2;
  margin: 0px !important;
}
ul { 
    display:flex;
    width:100%;
    box-sizing:border-box; 
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    background-color: #F25800;
    padding: 0 10px;
}
li { 
    display:inline-flex; 
    flex-wrap: wrap;
    flex: 1;
    padding: 10px 12px;
    text-align:center;
    align-items: center;
    padding: 10px 12px;
} 
li:hover{
  background-color: #64b448;
}
li a{
  text-decoration:none;
}
li a span{
  font-size: 13px; 
  color: #fff;
}
 
&#13;
<ul>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-rumah.html" class="level-top"><span>Kebutuhan Rumah</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-pribadi.html" class="level-top"><span>Kebutuhan Pribadi</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/makanan-minuman.html" class="level-top"><span>Makanan & Minuman</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kebutuhan-kantor.html" class="level-top"><span>Kebutuhan Kantor</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/otomotif.html" class="level-top"><span>Otomotif</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/listrik-teknik.html" class="level-top"><span>Listrik & Teknik</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/alat-bangunan.html" class="level-top"><span>Alat Bangunan</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/elektronik.html" class="level-top"><span>Elektronik</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/pakaian.html" class="level-top"><span>Pakaian</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/pecah-belah.html" class="level-top"><span>Pecah Belah</span></a>
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/kesehatan.html" class="level-top"><span>Kesehatan</span></a> 
   </li>
   <li class="ui-menu-item level0 classic parent ">
      <div class="open-children-toggle"></div>
      <a href="http://test.com/mainan-hobi.html" class="level-top"><span>Mainan & Hobi</span></a>
   </li>
</ul>
&#13;
&#13;
&#13;

以下是适用的代码。

  1. ul的显示更改为flex
  2. li的显示设置为inline-flex,将其配置为自动换行,并使用flex: 1调整大小调整。使用align-items: center将其垂直居中。