Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a <div> inherit the height of parent (<td>) using only css?

http://jsfiddle.net/ZAvDd/

As my table row grows in height, I'd like my div inside to automatically match it as well.

For the code above, my div's height is always 0.

I understand that the height:inherit only works when you explicitly state the height of the parent (<td>) but I don't want to do that because I have no idea what content height will be displayed there.

Is there any css trick to do this or do I have to resort to some JS?

like image 378
meiryo Avatar asked Jan 21 '13 16:01

meiryo


People also ask

How do you inherit parent height in CSS?

height: 100% will match the height of the element's parent, regardless of the parent's height value. height: inherit will, as the name implies, inherit the value from it's parent. If the parent's value is height: 50% , then the child will also be 50% of the height of it's parent.

How do I make my Div take 100% height of parent DIV?

container div has two parent elements: the <body> and the <html> element. And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

How do I get the height of an element in CSS?

height() It returns the current height of the element. It does not include the padding, border or margin. It always returns a unit-less pixel value. Note: The height() will always return the content height, regardless of the value of CSS box-sizing property.


1 Answers

You cannot using a height rule, but you can still achieve the effect you're after by positioning the div absolutely. Since this changes the layout of the div, you will then have to apply the width to the td instead:

td.fooed {position:relative; width:30px;}
td.fooed .foo {
    position:absolute; top:0px; left:0px; right:0px; bottom:0px;
    background:gray;
}

Here's your fiddle: http://jsfiddle.net/ZAvDd/2/

like image 142
rgthree Avatar answered Oct 02 '22 18:10

rgthree