Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use overflow:hidden without an explicit height somehow?

Tags:

css

I have an image with float:left, and I’d like it to overflow its parent, but cut off the overflow. Here’s what it looks like without any overflow rules:

                                   enter image description here

Here’s what I want:

                                   enter image description here

Here’s a fiddle: http://jsfiddle.net/ZA5Lm/

For some reason, it was decided that overflow:hidden without an explicit height results in the element growing.

Can I somehow achieve the effect I’m after without setting an explicit height? An explicit height doesn’t work because I want this div to size automatically based on content length and browser width.

like image 282
Roman Starkov Avatar asked Mar 30 '12 11:03

Roman Starkov


People also ask

Why does overflow hidden not work?

It is because you are using position absolute. You cannot use position absolute with overflow hidden, because position absolute moves the targeted element out of context with the document structure. Save this answer.

How do I hide content of overflow?

Set the div with a width or height, (otherwise it won't know whether something is overflowing). Then, add the overflow:hidden; CSS property-value pair.

How does overflow hidden work?

overflow: hiddenWith the hidden value, the overflow is clipped, and the rest of the content is hidden: You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.

How do I make an image overflow hidden?

To activate the overflow property enclose the image, within a div of a particular width and height, and set overflow:hidden . This will ensure that the base container will retain its structure, and any image overflow will be hidden behind the container.


2 Answers

In my opinion using overflow: hidden without setting dimensions doesn't make sense. If you don't want to specify the height of the container and if your images have a fixed width you could use this solution: http://jsfiddle.net/ZA5Lm/11/

The image is positioned with absolute, taking it out of the text-flow. However - and I'm aware that this may be ugly - you need to specify a padding-left to move the text away from the image.

like image 51
T. Junghans Avatar answered Oct 12 '22 10:10

T. Junghans


It's a bit tricky (I use relative + absolute positioning and a specific padding to position text) but it does the effect you asked without changing markup or setting height:

body {
    padding: 10px;
}
img {
    float: left;
    position: absolute;
    left : 10px;
}
div {
    border: 1px solid black;
    padding: 10px 10px 10px 280px;
    position : relative;
    overflow: hidden;
}

I just inserted style (even if float:left would be no longer necessary)

like image 39
Fabrizio Calderan Avatar answered Oct 12 '22 09:10

Fabrizio Calderan