简体   繁体   English

CSS中心对齐ul列表

[英]CSS center align ul list

I have a <ul> list on my website that contains some sub menus. 我的网站上有一个<ul>列表,其中包含一些子菜单。 It is structured in this way: 它以这种方式构建:

<div id="cssmenu">
<ul>
   <li class='active'><a href='index.html'><span>Home</span></a></li>
   <li class='has-sub'><a href='#'><span>RNG</span></a>
      <ul>
         <li><a href='#'><span>menu 1</span></a></li>
         <li><a href='#'><span>menu 1</span></a></li>
         <li class='last'><a href='#'><span>menu 1</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Altro</span></a>
      <ul>
         <li><a href='#'><span>kldajofa</span></a></li>
         <li class='last'><a href='#'><span>Altro12</span></a></li>
      </ul>
   </li>
   <li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</div>

I have 4 <li> as you can see and I'd like to see them center-aligned with the width of the screen, but I don't know how to do it. 我有4 <li>你可以看到,我希望看到它们与屏幕的宽度居中对齐,但我不知道该怎么做。

You can see a fiddle here that contains the CSS too. 你可以在这里看到一个包含CSS的小提琴 How can center align the names I have on my menu? 如何将我在菜单上的名字对齐?

在此输入图像描述

Assuming there are only 4 li - as in the example, set width:25% . 假设只有4 li - 如示例中所示,设置width:25%

Working jsFiddle here 在这里工作jsFiddle

#cssmenu li {
    float: left;
    padding: 0px;
    width: 25%;
}

在此输入图像描述

I made a minor update so that the sub-nav menu items are also 25%.. 我做了一个小的更新,以便子导航菜单项也是25%..

jsFiddle here jsFiddle在这里

There were set to width:225px; 设定width:225px; .

#cssmenu li ul {
    width: 25%;
}

Considering you have only 4 menu items, you just need to add : 考虑到您只有4个菜单项,您只需添加:

#cssmenu > ul > li { width:25%; }

This will set the width of the list items to 25%. 这会将列表项的宽度设置为25%。 And since the a are set to be displayed as blocks, they will fit the whole width of the li . 并且由于a被设置为显示为块,因此它们将适合li的整个宽度。

Note that we are using the direct child selector > because we don't want this effect to be applied to the menu li s inside the sub-menu. 请注意 ,我们使用的是直接子选择器> ,因为我们不希望这样的效果施加到菜单都li子菜单里面秒。 We only need that effect on the root level. 我们只需要在根级别上有这种效果。

Other possible solution is to use flexbox which isn't well supported yet and cannot be used without polyfill. 其他可能的解决方案是使用flexbox ,该flexbox尚未得到良好支撑,并且不能在没有flexbox物的情况下使用。

If the li are display:inline-block you can apply text-align:center to the ul and the list items will center regardless of the number (provided there is room on the line). 如果li display:inline-block ,则可以将text-align:center应用于ul ,列表项将居中,而不管数字是多少(前提是线路上有空间)。

The good news is that there are no clearing issues. 好消息是没有清算问题。

JSFiddle 的jsfiddle

I've just added width to li and it works now. 我刚刚为li添加了宽度,现在可以使用了。

Check JsFiddle 检查JsFiddle

#cssmenu li a {
  background: #0D0D0D bottom right no-repeat;
  display: block;
  font-weight: normal;
  line-height: 35px;
  margin: 0px;
  padding: 0px 25px;
  text-align: center;
  text-decoration: none;
    width:70px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM