简体   繁体   English

在不使用填充和边距的情况下垂直对齐拉伸的 flex

[英]Vertically aligning a stretched flex without using paddings and margin

I'm using a flexbox with align-items-stretch as I needed the borders to be reaching on both top and bottom.我正在使用带有 align-items-stretch 的 flexbox,因为我需要边框同时到达顶部和底部。 Not allowing the usage of margins and paddings, how can I come about vertically aligning the text within flex without facing any property conflict?不允许使用边距和内边距,如何在 flex 中垂直对齐文本而不会遇到任何属性冲突?

I've tried using the align-items-stretch and within the ul li added a align-middle class but it doesn't seem to do anything seeing that the property clashes.我试过使用 align-items-stretch 并在ul li添加了一个align-middle类,但它似乎没有做任何事情看到属性冲突。

<div class="navbar-collapse collapse align-items-stretch" id="navbarCollapse" style="">
    <ul class="navbar-nav ml-auto">
        <li class="nav-item active">Home</li>
        <li class="nav-item">About Us</li>
        <li class="nav-item ">Services</li>
    </ul>
</div>

This current code does the trick of stretching my borders but the text within the flexbox could not be centralized.这段当前的代码可以拉伸我的边框,但是 flexbox 中的文本无法集中。

How about adding the line-height with value the same the height of the .nav-item ?添加值与.nav-item高度相同的line-height怎么样?

Example:例子:

 .navbar-nav { display: flex; justify-content: center; } .nav-item { border: 1px solid pink; height: 3rem; line-height: 3rem; text-align: center; } ul { list-style: none; margin: 0; padding: 0; }
 <div class="navbar-collapse collapse align-items-stretch" id="navbarCollapse" style=""> <ul class="navbar-nav ml-auto"> <li class="nav-item active">Home</li> <li class="nav-item">About Us</li> <li class="nav-item ">Services</li> </ul> </div>

And here's the fiddle as well: https://jsfiddle.net/jutvsw26/这也是小提琴: https : //jsfiddle.net/jutvsw26/

For more detailed answer could you please add some CSS that you're currently using.有关更详细的答案,请添加一些您当前正在使用的 CSS。

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

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