简体   繁体   English

两个内联块和块元素之间的垂直空间

[英]Vertical space between two inline-block and a block element

 .rectangle { width: 420px; height: 143px; color: #fff; background: rgba(0, 0, 0, 0.7); padding: 20px 0px 20px 10px; position: relative; display: inline-block; vertical-align: top; } .triangle { width: 0; height: 0; border-top: 92px solid transparent; border-bottom: 92px solid transparent; border-left:45px solid rgba(0, 0, 0, 0.7); display: inline-block; } .block { width: 200px; height: 80px; background: red; } 
 <div class="rectangle"></div><!-- --><div class="triangle"></div> <div class="block"></div> 

Where does it come from? 它从何而来? How to get rid of it without negative margins (because on some screens they may overlap). 如何在没有负边距的情况下摆脱它(因为在某些屏幕上它们可能重叠)。

there is a many solution for this 对此有很多解决方案

Solution 0: No Space Between Elements 解决方案0:元素之间没有空格

<ul><li>Item content</li><li>Item content</li><li>Item content</li></ul>

Solution 1: font-size: 0 on Parent 解决方案1:父级上的font-size:0

.inline-block-list { /* ul or ol with this class */
    font-size: 0;
}

.inline-block-list li {
    font-size: 14px; /* put the font-size back */
}

Solution 2: HTML Comments 解决方案2:HTML评论

<ul>
    <li>Item content</li><!--
 --><li>Item content</li><!--
 --><li>Item content</li>
</ul>

Solution 3: Negative Margin 解决方案3:负保证金

.inline-block-list li {
    margin-left: -4px;
}

Solution 4: Dropping Closing Angle 解决方案4:降低闭合角度

<ul>
    <li>Item content</li
 ><li>Item content</li
 ><li>Item content</li>
</ul>

more about this 更多关于这个

The other answers provide solutions, but not the why this happens: 其他答案提供了解决方案,但不是为什么会发生这种情况:

Some given funny joke
-----^---------^-^

In that string I've marked three characters. 在那个字符串中我标记了三个字符。 Those three have so called ' decenders ' (eg: the loop under the G, the legs under the Y and J) . 这三个人都有所谓的“下行者(例如:G下的循环,Y和J下的腿)
When you declare something inline-block , it gets the properties of both block and inline elements. 当您声明inline-block ,它会获取blockinline元素的属性。 The inline elements are often text ( eg a or span ), thus have decenders, thus your div has room for decenders . 内联元素通常是文本( 例如aspan ),因此具有下伸,因此你的div有下拉的空间

This is why setting line-height:0; font-size:0; 就是设置line-height:0; font-size:0; line-height:0; font-size:0; does the trick. 诀窍。

You can use CSS pseudo element :after in this case. 你可以使用CSS伪元素:after在这种情况下。

Check below example: 检查以下示例:

 .rectangle { width: 420px; height: 143px; color: #fff; background: rgba(0, 0, 0, 0.7); padding: 20px 0px 20px 10px; position: relative; vertical-align: top; } .rectangle:after { top: 0; left: 100%; content: ""; position: absolute; width: 0; height: 0; border-top: 92px solid transparent; border-bottom: 92px solid transparent; border-left: 45px solid rgba(0, 0, 0, 0.7); } .block { width: 200px; height: 80px; background: red; } 
 <div class="rectangle"></div> <div class="block"></div> 

its because of line-height , also, your .rectangle was missing 1px in height(143->144) 因为line-height ,你的.rectangle缺少1px的高度(143-> 144)

NOTE: im not sure, if this is a cross-browser, so consider using float: left instead 注意:我不确定,如果这是一个跨浏览器,所以考虑使用float: left代替

 .rectangle { width: 420px; height: 144px; color: #fff; background: rgba(0, 0, 0, 0.7); padding: 20px 0px 20px 10px; position: relative; display: inline-block; vertical-align: top; } .triangle { width: 0; height: 0; border-top: 92px solid transparent; border-bottom: 92px solid transparent; border-left:45px solid rgba(0, 0, 0, 0.7); display: inline-block; } .block { width: 200px; height: 80px; background: red; } .wrapper{ line-height: 0; } 
 <div class="wrapper"><div class="rectangle"></div><!-- --><div class="triangle"></div><!-- --><div class="block"></div></div> 

使用font-size:0到父元素

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

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