简体   繁体   English

CSS溢出:隐藏与显示:内联块

[英]css overflow:hidden with display:inline-block

I want to use text-overflow: ellipsis to cut some text when it is too long,but I have some problems when I use overflow:hidden with display:inline-block. 我想使用text-overflow:省略号在太长时会剪切某些文本,但是当我使用display:inline-block的overflow:hidden时,我会遇到一些问题。

html: HTML:

<span class="text">
    <span class="inner left">Click to add overflow</span>
    <span class="inner right"> long text here</span>
</span>
<div class="bottom"></div>

css: CSS:

.text {
    line-height: 50px;
    font-size: 20px;
    display: inline-block;
}
.right {
    display:inline-block;
    text-overflow: ellipsis;
    width: 100px;
    white-space: nowrap;
}
.overflow {
    overflow: hidden;
}

javascript: JavaScript的:

$('.text').on('click', function() {
    $(this).toggleClass('overflow');
    $('.right').toggleClass('overflow');
})

jsfiddle: http://jsfiddle.net/zhouxiaoping/knw7m5k2/2/ jsfiddle: http : //jsfiddle.net/zhouxiaoping/knw7m5k2/2/

My question is : 我的问题是:

  • why there is 2px blank between .text element and .bottom element when the .text has overflow:hidden attribute 为什么.text溢出时,.text元素和.bottom元素之间有2px的空白:hidden属性

  • why the .right elment not align with the left when it has overflow:hidden attribute 为什么.right元素溢出时未与左侧对齐:hidden属性

  • what dose the overflow:hidden really do 溢出的剂量:隐藏的确实是

my question is not how to fix it but figure out what happened 我的问题不是如何解决,而是找出发生了什么

related: Why is this inline-block element pushed downward? 相关: 为什么将内联块元素向下推?

the really reason is : 真正的原因是:

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. “ inline-block”的基线是正常流中其最后一个线框的基线,除非它没有流入流线框,或者如果其“ overflow”属性的计算值不是“ visible”,则在这种情况下,基线是下边距。

thank you for the help of all 谢谢大家的帮助

Answer to your questions as below. 回答您的问题如下。

why there is 2px blank between .text element and .bottom element when the .text has overflow:hidden attribute 为什么.text溢出时,.text元素和.bottom元素之间有2px的空白:hidden属性

A > You need to add a vertical-align property to align the elements to see no gaping. A>您需要添加一个vertical-align属性来对齐元素,以确保没有间隙。 Link With Vertical Align 垂直对齐链接

Code

.overflow {
overflow: hidden;
vertical-align: top;

} }

PS: You can change vertical-align:top; PS: 您可以更改vertical-align:top; to any other vertical-align properties as per your needs. 根据您的需要设置为其他任何vertical-align属性。

why the .right elment not align with the left when it has overflow:hidden attribute 为什么.right元素溢出时未与左侧对齐:hidden属性

A > Alignment has nothing to do with overflow. >对齐与溢出无关。 You need to use vertical-alignment to align it as per you want. 您需要根据需要使用vertical-alignment进行对齐。 I also believe, that this has a link to question 1. So if you check the above, it now aligns. 我也相信,这可以链接到问题1。因此,如果您检查以上内容,现在可以对齐。

what dose the overflow:hidden really do 溢出的剂量:隐藏的确实是

This value indicates that the content is clipped and that no scrolling mechanism should be provided to view the content outside the clipping region; 该值表示内容已被裁剪,并且不应提供滚动机制来查看位于裁剪区域之外的内容。 users will not have access to clipped content. 用户将无法访问剪辑的内容。 The size and shape of the clipping region is specified by the 'clip' property. 裁剪区域的大小和形状由'clip'属性指定。

Source 资源

Hope this helps. 希望这可以帮助。

you need to use float:left; 您需要使用float:left; for both span. 对于两个跨度。 after that some adjustment like line-height, margin. 之后进行一些调整,例如行高,边距。 find fiddle demo 查找小提琴演示

.left {
font-size: 12px;
font-weight: bold;
float:left;
line-height:55px;
}
.right {
margin-left:4px;    
float:left;
text-overflow: ellipsis;
width: 100px;
white-space: nowrap;
}

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

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