The goal
Set a div having 80% of the height of the parent that does not have a defined height. To be more specific, I want a responsive-effect.
The problem
I have five layers: .header, .site > .container > .right sidebar and .left content.
The height of the header is 50 pixels. The site's height is the viewport height - 50 pixels (from the header). Inside of the site's div, there is a container. I want to set his height to 80% of viewport height - 50 pixels from the header - the site's div offset (1.5em padding) and I'm not getting to work with percentage, only pixels — what I need to do?
The FiddleJS
The problem is illustrated here.
Technical details
Seems to work perfect, right? Yes, works perfeclty...... with pixels. Try to change the height of .left-content
to 80% and you will see what is my problem.
The code (is the same of the FiddleJS)
HTML:
<div class="header">
</div>
<div class="site">
<div class="container">
<div class="container-head">
</div>
<div class="container-body">
<div class="right-sidebar">
Just a menu will be right here.
</div>
<div class="left-content">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</div>
</div>
CSS:
body {
font-family: Tahoma
}
.header {
background-color: #49c98e;
width: 100%;
height: 50px;
}
.site {
padding: 1.5em 0;
}
.container {
background-color: #c7c7c7;
width: 95%;
margin: 0 auto;
position: relative;
}
.right-sidebar {
background-color: #a94cd5;
width: 300px;
height: 100%;
position: absolute;
right: 0;
}
.left-content {
background-color: #dbdbdb;
width: 100%;
height: 500px;
overflow-y: scroll;
}
.left-content ul {
padding: 25px 0;
float: left;
}
.left-content ul li {
background-color: #a94cd5;
width: 150px;
height: 100px;
float: left;
margin: 15px;
list-style: none;
}
jQuery/JavaScript:
function calculateResponsiveWidth() {
$realWidth = $(".container").width() - $(".right-sidebar").outerWidth(),
$containerContent = $(".left-content");
$containerContent.css({
"width": $realWidth
});
}
$(window).on("resize", function () {
calculateResponsiveWidth();
});
calculateResponsiveWidth();
Thanks in advance.
Update v1
I'm beginning to think that using percentage is not the best option. After the container area I want a spacing of 25 pixels — and with percentage it was not maintained in the different resolutions.
There was a Media Query suggestion, but I need an alternative to Internet Explorer 8 — Someone have a guess? Maybe JavaScript could resolve my problem?
Update v2
The problem was resolved with a bit of JavaScript, math and logic. Thanks to all who contributed!