简体   繁体   English

在容器内垂直对齐对齐的内联块元素

[英]Vertically align justified inline-block elements inside a container

I know that this questions has been asked several times, but I couldn't find a solution for this specific situation. 我知道这个问题已经问过几次了,但是我找不到针对这种特定情况的解决方案。

So, here's what I have: DEMO 所以,这就是我所拥有的: 演示

Basically it's a list of rows: 基本上是一个行列表:

<ul>
    <li>
        <label>
            <span>element1</span>
            <span>element2</span>
            <span>element3</span>
            <span>element4</span>
        </label>
    </li>
    <li>
        <label>
            <span>element1</span>
            <span>element2</span>
            <span>element3</span>
            <span>element4</span>
        </label>
    </li>
</ul>

And the span elements are all inline-blocks and label has text-align: justify . 并且span元素都是inline-blocks并且label具有text-align: justify

Everything works as expected, and as I want it to, but the vertical alignment. 一切都按预期进行,但按我的意愿进行,但垂直对齐。 All the items are attached to the top, kind of. 所有项目都附在顶部,有点像。 I thought it was the usual inline-block annoyance of the empty line afterward, but I tried setting the font-size to 0, or removing the white space and nothing changed. 我以为这是空行之后常见的行内嵌块烦恼,但我尝试将font-size设置为0,或删除了空白,但没有任何变化。

This seems to have to do with the line-height – somehow it makes that pseudo element take up additional space. 这似乎与line-height –某种程度上使伪元素占用了额外的空间。 You can mitigate the effects a little bit by setting line-height:0 for the label and the li , and then a line-height:1 for .product-row-price again … but that still doesn't look that good, it still leaves some space at the bottom. 您可以通过为labelli设置line-height:0 ,然后为.product-row-price再次设置line-height:1来稍微减轻影响,但是看起来还是不太好,仍然在底部留有一些空间。 (And a lower line-height will make the text in .product-row-price overlap.) (较低的行高将使.product-row-price的文本重叠。)

I think a better solution would be a negative margin-bottom for the label, and overflow set to hidden: 我认为更好的解决方案是将标签的底边距设为负底边,并将溢出设置为隐藏:

.product-row-label {
    margin-bottom: -1.5em;
    overflow: hidden;
    /* rest of existing styles */
}

http://jsfiddle.net/db0onh6c/7/ http://jsfiddle.net/db0onh6c/7/

FYI: div in span is invalid HTML – so you should replace those divs inside of .product-row-price with spans and make them block. 仅供参考: div中的span是无效的HTML –因此,您应将.product-row-price内的div替换为span并将其阻止。

Still doesn't look to pretty though, because the different widths of the content in the first and third column makes the whole thing look uneven, so the content in the second column doesn't align horizontally. 不过,看起来还是不够漂亮,因为第一列和第三列中内容的宽度不同,使整个内容看起来很不均匀,因此第二列中的内容没有水平对齐。 (Although with a set width for the first and last column that could be avoided, if you're ok with a little space at the right of the “shorter” values in the third column – something like this: http://jsfiddle.net/db0onh6c/8/ ) (尽管可以避免为第一列和最后一列设置宽度,但如果可以的话,请在第三列的“较短”值的右边留一点空格-像这样: http:// jsfiddle。网路/ db0onh6c / 8 /

Maybe you could try to set property display: table; 也许您可以尝试设置属性display: table; to <label> elements and display:table-cell; <label>元素并display:table-cell; to your <span> elements. 您的<span>元素。 This fix element to display as a <table> html element. 此修订元素显示为<table> html元素。 Just fix padding and margin display properly. 只需正确修复填充和边距显示即可。

DEMO DEMO

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

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