简体   繁体   English

li不在ul的中心对齐

[英]li not aligning in center to ul

In the following code, I am struggling to align the li HELLO exactly in the center of the ul But it's getting shifted to the left side. 在下面的代码中,我正在努力将li HELLO精确地对齐到ul的中心,但是它已移至左侧。 Here, I am not looking for a solution (as I am trying to practice and understand the functionality through this code). 在这里,我不是在寻找解决方案(因为我正在尝试通过此代码练习和理解功能)。 But I am most importantly looking for a reason and explanation as to why is this happening. 但是,最重要的是,我正在寻找原因和解释,说明为什么会这样。

Even though I have set the left/right margin of the li to auto. 即使我已将li的左/右边距设置为auto。 Then why is it not coming in the center of the ul ? 那为什么它不在ul的中心呢? For visual aid, I have created border for li so that you can easily spot it's exact position. 为了提供视觉帮助,我为li创建了边框,以便您可以轻松找到它的确切位置。 I know that by decreasing/altering the width of the ul , this problem can be corrected. 我知道通过减小/更改ul的宽度,可以解决此问题。 But is there no other way so that the text is aligned in the center automatically (without manually adjusting width,padding etc)? 但是,还有没有其他方法可以使文本自动居中对齐(无需手动调整宽度,填充等)? Also if I increase the length of the text or change its font size I want the text to be aligned in the center. 另外,如果我增加文本的长度或更改其字体大小,我希望文本在中心对齐。

 html body { height: 100%; width: 200px; } ul { background-color: rgba(0,0,0,1); height: 100px; width: 150px; } li { color: rgba(255,255,255,1); margin-right: auto; margin-left: auto; list-style-type: none; width: 50px; text-align: center; border: thin solid rgba(255,255,255,1); float: left; } 
 <ul> <li>Hello</li> </ul> 

It is slightly to the right because the ul has a padding-left by default; 它稍微靠右,因为ul默认情况下padding-left setting padding-left: 0; 设置padding-left: 0; to the ul will make it go completely left. ul会使它完全离开。 Then the li isn't centered because you're floating it to the left with float: left; 然后li不会居中,因为您使用float: left;将其浮动到float: left; this will make it ignore the margin: auto; 这将使其忽略margin: auto; you have set. 你已经设定了。 If you remove the float the li will be at the center. 如果您删除floatli将位于中间。

I guess you used the float because there will be more li's in the list and you want them all on one line and centered in the ul . 我猜您使用了float因为列表中会有更多li's ,并且您希望它们全部在一行上并以ul为中心。 In these cases what I usually do is: 在这些情况下,我通常要做的是:

ul {
    text-align: center;
}

li {
    display: inline-block;
}

EXAMPLE: 例:

 ul { margin: 0; padding: 0; text-align: center; } li { display: inline-block; margin: 0 10px; border: 1px solid black; padding: 5px; } 
 <ul> <li>Example1</li> <li>Example2</li> <li>Example3</li> </ul> 

I'm seeing two issues here: 我在这里看到两个问题:

By default ul elements render with padding to the left. 默认情况下, ul元素在左侧填充。 This is what's given you that spacing and perhaps creating confusion around that element being pushed right. 这就是给您的间距,并可能导致向右推动该元素的混乱。

Additionally, you're adding the float property to your <li> element. 此外,您要将float属性添加到<li>元素中。 Remove that and you should see your <li> centered. 删除它,您应该看到<li>居中。

ul {
    padding: 0;
    background-color: rgba(0,0,0,1);
    height: 100px;
    width: 150px;
}

li {
    color: rgba(255,255,255,1);
    margin-right: auto;
    margin-left: auto;
    list-style-type: none;
    width: 50px;
    text-align: center;
    border: thin solid rgba(255,255,255,1);
}

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

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