设置inline-block元素的overflow:hidden意外增加元素总体高度的问题
2016-10-12 16:59
513 查看
来源1:https://my.oschina.net/u/214483/blog/663133
来源2:http://www.yidianzixun.com/home?page=article&id=0BwQmgTc
摘要: 设置inline-block元素的overflow:hidden意外增加元素总体高度的问题工作中遇到的一个问题,设置inline-block元素的overflow:hidden意外增加元素总体高度。
描述如下:
设 A为子容器,B为父容器。
A设置为inline-block,并且overflow为hidden,A高度为23,B高度为30。
A设置为block,A高度为23,B高度为23。
通过stackoverflow找到原因(http://stackoverflow.com/questions/22421782/css-overflow-hidden-increases-height-of-container),摘抄如下:
Let me explain to you why this is happening.
According to CSS 2.1 Specs,
The baseline of an ‘inline-block’ is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its ‘overflow’ property has a computed value other than ‘visible’, in which case the baseline is the bottom margin edge.
To explain in simple words,
i) If inline-block in question has its overflow property set to visible (which is by default so no need to set though). Then its baseline would be the baseline of the containing block of the line. ii) If inline-block in question has its overflow property set to OTHER THAN visible. Then its bottom margin would be on the baseline of the line of containing box.
So, in your case the inline-block cell has overflow:hidden (not VISIBLE), so its margin-bottom, the border of cell is at the baseline of the container element container.
That is why the element cell looks pushed upwards and the height of container appears increased. You can avoid that by setting cell to display:block.
翻译如下:
‘inline-block’的baseline是其在normal flow中的最后一个line box的baseline,除非它没有in-flow line boxes,或者其‘overflow’属性不等于‘visible’,这种情况下,其baseline位于bottom margin边上。
解释如下:
i) 如果inline-block的overflow设为visible(默认属性),则其baseline是当前行的containing block的baseline。
ii) 如果overflow设为其他,则其bottom margin位于前行的containing block的baseline;
我们这种情况下,inline-block元素的overlow:hidden,所以元素的底部边框在父元素的baseline。
因此高度才会看起来增加了。
解决方法
常用的解决方法是为上述inline-block元素添加vertical-align: bottom。你可以在上面的例子中在线测试下。
解决方法
可以将inline-block设为block,即可解决问题。
来源2:http://www.yidianzixun.com/home?page=article&id=0BwQmgTc
摘要: 设置inline-block元素的overflow:hidden意外增加元素总体高度的问题工作中遇到的一个问题,设置inline-block元素的overflow:hidden意外增加元素总体高度。
描述如下:
设 A为子容器,B为父容器。
A设置为inline-block,并且overflow为hidden,A高度为23,B高度为30。
A设置为block,A高度为23,B高度为23。
通过stackoverflow找到原因(http://stackoverflow.com/questions/22421782/css-overflow-hidden-increases-height-of-container),摘抄如下:
Let me explain to you why this is happening.
According to CSS 2.1 Specs,
The baseline of an ‘inline-block’ is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its ‘overflow’ property has a computed value other than ‘visible’, in which case the baseline is the bottom margin edge.
To explain in simple words,
i) If inline-block in question has its overflow property set to visible (which is by default so no need to set though). Then its baseline would be the baseline of the containing block of the line. ii) If inline-block in question has its overflow property set to OTHER THAN visible. Then its bottom margin would be on the baseline of the line of containing box.
So, in your case the inline-block cell has overflow:hidden (not VISIBLE), so its margin-bottom, the border of cell is at the baseline of the container element container.
That is why the element cell looks pushed upwards and the height of container appears increased. You can avoid that by setting cell to display:block.
翻译如下:
‘inline-block’的baseline是其在normal flow中的最后一个line box的baseline,除非它没有in-flow line boxes,或者其‘overflow’属性不等于‘visible’,这种情况下,其baseline位于bottom margin边上。
解释如下:
i) 如果inline-block的overflow设为visible(默认属性),则其baseline是当前行的containing block的baseline。
ii) 如果overflow设为其他,则其bottom margin位于前行的containing block的baseline;
我们这种情况下,inline-block元素的overlow:hidden,所以元素的底部边框在父元素的baseline。
因此高度才会看起来增加了。
解决方法
常用的解决方法是为上述inline-block元素添加vertical-align: bottom。你可以在上面的例子中在线测试下。
解决方法
可以将inline-block设为block,即可解决问题。
相关文章推荐
- inline-block元素overflow:hidden对齐问题
- inline-block元素设置overflow:hidden属性导致相邻行内元素向下偏移
- inline-block元素设置overflow:hidden属性导致相邻行内元素向下偏移
- 子div设置float,父div不再增加高度,需要给父元素加个overflow:hidden属性
- position:absolute与overflow:hidden,解决子元素设置position:absolute后父元素 overflow:hidden无效的问题
- inline元素设置display:inline-block存在空隙问题
- Html中display为inline-block的元素有内容和没有内容情况下高度不一致问题的讨论
- overflow :hidden 和 clear:both 与 clearfix撑起高度坍塌的父元素应用
- 清除浮动2-父元素设置overflow:hidden
- inline-block元素间距问题的几种解决方案
- inline-block元素间间距问题
- 解决: 排列inline-block元素出现顶部不对齐的问题
- inline-block元素间的间隙问题
- 关于inline-block元素的行高问题
- 父容器根据子容器高度自适应:设置父容器 height:100%;overflow:hidden;
- 对同一元素设置overflow-x:hidden,overflow-y:visible;属性值不生效
- inline-block元素间的间隙问题
- font-size问题——当你设置font-size为12px时height为14,每增加一个像素高度相应就增加二个像素
- css中子元素根据父元素设置百分比高度的问题。