Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does overflow hidden affect height and how can I fix it in this example?

Tags:

html

css

Fiddle: https://jsfiddle.net/wa51kdh7/

Code:

HTML: Hello world

<span class="span2">
    Goodbye cruel world
</span>

CSS:

span {
    display: inline-block;
    margin: 0;
    padding: 0;
    line-height: 16px;
    font-size: 16px;
    height: 16px;
}

.span1 {
    background-color: lightblue;
}

.span2 {
    background-color: pink;
    overflow: hidden;
    width: 130px;
    text-overflow: ellipsis;
}

Here I am trying to create two spans next to each other, only one of them has overflow: hidden and the other shouldn't have overflow: hidden. For some reason the overflow: hidden affects the heights and they don't line up - even when I use an explicit height.

Any idea how to fix this?

like image 459
Natarajan Shanker Avatar asked Aug 24 '15 20:08

Natarajan Shanker


People also ask

What happens if we use overflow hidden?

hidden - The overflow is clipped, and the rest of the content will be invisible. scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content. auto - Similar to scroll , but it adds scrollbars only when necessary.

How do I fix the overflow in CSS?

To change this, set the min-width or min-height property.” This means that a flex item with a long word won't shrink below its minimum content size. To fix this, we can either use an overflow value other than visible , or we can set min-width: 0 on the flex item.

How do I break out of overflow hidden?

You can do this by setting the child to be position: absolute. Usually, this should work: overflow: hidden doesn't clip absolutely positioned descendants unless the same container doesn't have position:relative (see cssmojo.com/clearfix-reloaded-overflowhidden-demystified).

What is the purpose of the hidden value in the overflow property?

The hidden value of the overflow property hides all the content that is exceeding the box area. This property should be handled with care because the content that this property hide is completely invisible to the user, however, it is best suited for displaying content that is dynamic in nature.


3 Answers

This can also be fixed by adding vertical-align: top to the span's CSS rule. The reason that both rules fix the problem is that they enforce the vertical alignment of the divs.

Adding a vertical-align rule will keep you from potentially having the elements that follow from needing to be cleared.

like image 159
Beau Townsend Avatar answered Oct 10 '22 10:10

Beau Townsend


span {
    display: inline-block;
    margin: 0;
    padding: 0;
    line-height: 16px;
    font-size: 16px;
    height: 16px;
    vertical-align: middle;
}
like image 32
HoomanMns Avatar answered Oct 10 '22 10:10

HoomanMns


Hi I just updated your span to float: left and it works. It's not the overflow but the fact the spans aren't floated that they are misaligned.

like image 25
Nathaniel Flick Avatar answered Oct 10 '22 10:10

Nathaniel Flick