简体   繁体   English

内联块最后一个项目包装,但有足够的空间

[英]inline-block last item wrapping, but there is enough space

I am trying to make my menu to the right side using text-align:right on the parent and display: inline-block on menu. 我试图使用父级上的text-align:right并使菜单上的inline-block变为右侧菜单。 (see example: http://codepen.io/sanns/pen/WQjRoj?editors=110 ) (请参见示例: http : //codepen.io/sanns/pen/WQjRoj?editors=110

I also want items in menu align left, so I used text-align:left on li . 我还希望菜单中的项目左对齐,所以我在li上使用了text-align:left But the problem is that the last item for some reason wraps despite there's enough width for all elements. 但是问题在于,尽管所有元素都有足够的宽度,但由于某种原因,最后一项还是会自动换行。

Why is it happening? 为什么会这样呢?

 .navg { display: inline-block; /* to aplly parents text-align*/ text-align: left; /* align children*/ } .navg__item { margin-right: 2.5%; display: inline-block; margin-bottom: 5px; } 
 <div class="col-sm-9 text-right"> <ul class="navg"> <li class="navg__item navg__item--active"><a href="#" class="navg__link">Главная</a></li> <li class="navg__item"><a href="#" class="navg__link">Услуги</a></li> <li class="navg__item"><a href="#" class="navg__link">On-line заказ</a></li> <li class="navg__item"><a href="#" class="navg__link">О компании</a></li> <li class="navg__item"><a href="#" class="navg__link">Контакты</a></li> </ul> </div> 

.navg__item {
    margin-right: 2.5%;
}

If you look at that 2.5% , that is the reason your item is going to new line. 如果您查看该2.5%,这就是您的商品要换行的原因。 % of margins are calculated against width of the element. 边距的百分比是针对元素的宽度计算的。

If you did something like 如果你做了类似的事情

.navg__item {
    margin-right: 10px;
}

It should work. 它应该工作。

Some more information here : http://www.w3.org/TR/CSS2/box.html#margin-properties 此处提供更多信息: http : //www.w3.org/TR/CSS2/box.html#margin-properties

Especially this part 特别是这部分

Percentage - The percentage is calculated with respect to the width of the generated box's containing block. 百分比 -相对于生成的盒子的包含块的宽度计算百分比。 Note that this is true for 'margin-top' and 'margin-bottom' as well. 请注意,“ margin-top”和“ margin-bottom”也是如此。 If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1. 如果包含块的宽度取决于此元素,则结果布局在CSS 2.1中未定义。

Just remove display: inline-block from .navg class 只需删除display: inline-block .navg类中的display: inline-block

 .navg { /* to aplly parents text-align*/ text-align: left; /* align children*/ } .navg__item { margin-right: 1%; display: inline-block; margin-bottom: 5px; } 
 <div class="col-sm-9 text-right" > <ul class="navg" > <li class="navg__item navg__item--active" ><a href="#" class="navg__link">Главная</a></li> <li class="navg__item"><a href="#" class="navg__link">Услуги</a></li> <li class="navg__item"><a href="#" class="navg__link">On-line заказ</a></li> <li class="navg__item"><a href="#" class="navg__link">О компании</a></li> <li class="navg__item"><a href="#" class="navg__link">Контакты</a></li> </ul> </div> 

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

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