简体   繁体   English

如何在ul元素中将第一个li左对齐和第二个li中心对齐

[英]how to align first li left and second li center in the ul element

Html: HTML:

<ul id="wr">
    <li id="l1"></li> // align left
    <li id="l2"></li> //align center
</ul>

Css: CSS:

ul {background-color:yellow;widht:100%; height:25px; margin:0px; padding:0px;}
ul li {
  display: inline-block;
  list-style: none;
  width:200px;height:20px;
}
#l1 {background-color:red;}
#l2 {background-color:blue; margin:auto;}

http://jsfiddle.net/edo0ujnb/1/ http://jsfiddle.net/edo0ujnb/1/

Tried adding margin:auto on #l2 , but it doesn't work. 尝试在#l2上添加margin:auto ,但是不起作用。 Also tried text-align:center on ul, and on first li float:left , but second li ends up not in the center, but a bit to the right. 还尝试了ul上的text-align:center ,以及第一个li float:left ,但是第二个li结尾不是在中心,而是在右边。

How can align second li (blue in my example) center, and first li (red in my example) left? 如何对齐第二个li (在我的示例中为蓝色)中心和第一个li (在我的示例中为红色)中心?

You can use positioning to move the red list item to the left, and text-align:center on the <ul> to center the blue list item: 您可以使用定位将红色列表项向左移动,并在<ul>上将text-align:center置于蓝色列表项的中心:

 ul { background-color:yellow; width:100%; height:25px; margin:0px; padding:0px; text-align:center; position:relative; } ul li { display: inline-block; list-style: none; width:200px; height:20px; } #l1 { background-color:red; position:absolute; left:0; } #l2 { background-color:blue; margin:auto; } 
 <ul> <li id="l1"></li> <li id="l2"></li> </ul> 

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

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