简体   繁体   English

ul的最后一个li成分在其余部分以下对齐

[英]Last li component of ul aligning below the rest

I'm having trouble getting the final LI element to align in my nav-container. 我在将最后一个LI元素对准我的导航容器时遇到麻烦。 I've got the other four spaced fine and there is enough space for the fifth element to display in the container but it's getting pushed down somehow. 我已经将其他四个隔开,并且有足够的空间可以在容器中显示第五个元素,但是它以某种方式被推下。 I'm pretty confused as to why this is happening. 我很困惑为什么会这样。 HTML and CSS below. 下面的HTML和CSS。 Any help greatly appreciated! 任何帮助,不胜感激!

<div class='top_menu_wrapper'>
    <div class='top_menu_center'>
        <ul>
            <li class='menu_link menu'>Who We Are</li>
            <li class='menu_link menu'>Family House</li>
            <li class='menu_link ypac_logo menu'> IMAGE PLACEHOLDER</li>
            <li class='menu_link menu'>Upcoming Events</li>
            <li class='menu_link menu'>Get Involved</li>
        </ul>
    </div>
</div>

CSS: CSS:

.top_menu_wrapper{
  min-height: 65px;
  border: 1px solid black;
  position: fixed;
  width:100%
  }
.top_menu_center{
 width:65%;
 border: 1px solid black;
 margin: 0 auto;
 height:100px;
 }
.top_menu_center ul{
 list-style-type:none;
 width:100%;
 margin:0 0 0 0;
 padding:0 0 0 0;
 height:100px;
}
.top_menu_center li{
 display:inline-block;
 height:100px;
 width:20%;
 margin:0 0 0 0;
 padding:0 0 0 0;
 text-align:center;
}

You need to change inline-block to inline 您需要将inline-block更改为inline

.top_menu_center li{
 display:block;
 float:left;
 height:100px;
 width:20%;
 margin:10px 0;
 padding:0 0;
 text-align:center;
}

You'll have some spacing issues though so I've set up a jsfiddle here: http://jsfiddle.net/rileychuggins/YF7DX/ 不过您会遇到一些间距问题,因此我在这里设置了jsfiddle: http : //jsfiddle.net/rileychuggins/YF7DX/

For some reason, the sum of your li 's width is more than 100%. 由于某种原因,您li的宽度之和超过100%。 Therefore the last element gets pushed out of the box. 因此,最后一个元素被开箱即用。 Anyway, if you change width of the li from 20% to 19% it starts fitting better in Chrome. 无论如何,如果将li宽度从20%更改为19%,它将开始更好地适合Chrome。 You can do the research on why 5 * 20% != 100% in this instance. 在这种情况下,您可以研究为什么5 * 20%!= 100%。

Fiddle: http://jsfiddle.net/WP6Lb/ 小提琴: http//jsfiddle.net/WP6Lb/

You should just float the li 's and set it up so that the ul clears the float. 您应该只浮动li并进行设置,以便ul清除浮动。

Here's a jsfiddle: http://jsfiddle.net/efgdY/ 这是一个jsfiddle: http : //jsfiddle.net/efgdY/

As a side note, if you intend to have them pushed down (by what looks like 10-15 pixels, as in the original CSS), you can set the padding-top value and add box-sizing: border-box; 附带说明一下,如果您打算将它们下推(与原始CSS一样降低10-15像素),则可以设置padding-top值并添加box-sizing: border-box; of the .top_menu_center li . .top_menu_center li

You can try using float: left instead of display: inline 您可以尝试使用float:left而不是display:inline

.top_menu_center li{
    float: left;
    height:100px;
    width:20%;
    margin:0 0 0 0;
    padding:0 0 0 0;
    text-align:center;
}

See: http://fiddle.jshell.net/RQQLJ/ 请参阅: http//fiddle.jshell.net/RQQLJ/

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

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