Reputation: 97
I need help setting 'div1' to 50% of its parent element: 'div2'. The height of 'div2' is decided by an image inside it. How can I set 'div1' to 50% 'div2'
Example: http://jsfiddle.net/k39048eh/
For 'div1' I have tried: height: 50%
, min-height: 50%
;
For 'div2' I have tried: height: auto
Upvotes: 0
Views: 1678
Reputation: 1552
The height of an element can have 5 possible properties 1:
auto
- The calculated height for this element by the browser.200px
, 2em
, or even 0
inherit
- From its parentinitial
- the default value - auto
hereThe reason your CSS isn't working is because when you tell .div1
to have a percent-based height, it tries to calculate that value based on a value set for its parent. And since .div2
has a height of auto
(by default), and not a specific length based height, .div1
also defaults to a height of auto
.
You have a couple options in this scenario:
Give .div2
a fixed height in pixels or ems. For example, if you set height: 200px;
Fiddle: http://jsfiddle.net/vyder/k39048eh/3/
Give .div2
a percent height based on its parents (which are html, and body in my example here).
Fiddle: http://jsfiddle.net/vyder/k39048eh/2
However, that's not an ideal solution since you're trying to match .div2
's height to fit the image child element's height.
Perhaps, you can set a fixed height for .div2
, and resize the image to have the same height to give the same effect like this.
Fiddle: http://jsfiddle.net/vyder/k39048eh/5/
Finally, when you set an element's position to be absolute
, the browser follows different rules when drawing it out.
So here I've set .div1
's position to be absolute
, and .div2
's to relative
, so that .div1
's dimensions and position are calculated relative to .div2
's.
Fiddle: http://jsfiddle.net/vyder/k39048eh/8/
The problem you face here though is that if .div1
's content increases, and its height exceeds that of its parent, .div2
won't automatically increases its height to fit .div1
.
Let me know if you have questions.
Upvotes: 1
Reputation: 7207
Try like this: Demo
.div2 {
border: 3px solid blue;
width: 40%;
float: right;
position: absolute;
right: 0;
top: 0;
bottom:50%;
}
.parent {
overflow: hidden;
position: relative;
width: 100%;
}
HTML:
<div class="parent">
<div class="div1">
<img src="http://lorempixel.com/400/200" class="image" />
<div class="div2">Needs to be 50% height of DIV1</div>
</div>
</div>
Upvotes: 2
Reputation: 6928
You can do it using CSS calc function:
See: jsfiddle.net/k39048eh/4/
The CSS3 calc() function is primarily used to calculate lengths, numbers, angles, transition/animation times or sound frequencies. However, it allows you to mix measurement types — a powerful concept in CSS.
Upvotes: -1
Reputation: 2131
You could use JQuery but here is my pure CSS way using the table method.
HTML
<div class="container">
<div class="column"><img src="http://lorempixel.com/400/200" class="image"/></div>
<div class="column">
<div class="row right-side">Upper half</div>
</div>
</div>
CSS
body {
margin 0;
padding 0;
}
.container {
width: 100%;
display: inline-block;
padding: 0;
margin: 0;
border: 3px solid red;
}
img {
width: 100%;
padding 0;
margin 0;
border: none;
}
.column {
width: 50%;
padding: 0;
margin: 0;
display: table-cell;
vertical-align: top;
border: 3px solid green;
}
.row {
width: 100%;
height: 50%;
padding: 0;
margin: 0;
vertical-align: top;
display:table;
}
.right-side {
height:50%;
display:inline-table;
border: 3px solid blue;
}
Upvotes: 2
Reputation: 4286
Use jquery for this
<script type="text/javascript">
$(document).ready(function() {
var div1=$( '.div1' ).height();
$('.div2').height(div1/2);
});
</script>
Upvotes: 1