Achieving the Desired Height
In order to set a div with 80% of the height of its parent element without a defined height, I am aiming for a responsive effect.
The Layered Layout Issue
Within my layout consisting of five layers - .header, .site, .container, .right sidebar, and .left content, I am facing a challenge. The header has a fixed height of 50 pixels, while the site's height is determined by the viewport height minus the 50 pixels from the header. Inside the site's div, there is a container that I want to set at 80% of the remaining space after considering the header and site dimensions, but I'm struggling to achieve this using percentages instead of fixed pixels.
View the Problem on FiddleJS
Check out the issue demonstration here.
Technical Analysis
The current solution works flawlessly when pixel values are used. However, changing the height of .left-content to 80% reveals the underlying problem due to the dependency on absolute pixel measurements.
Code Snippet (Same as in 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();
Your insights are much appreciated.
Update v1
I am starting to consider that utilizing percentage values may not offer the most optimal solution. Post-container spacing of 25 pixels is necessary, which does not scale well across different resolutions. A Media Query suggestion was made, but I require a JavaScript-based alternative compatible with Internet Explorer 8. Any suggestions or solutions?
Update v2
The identified issue has been overcome through strategic application of JavaScript, mathematical calculations, and logical reasoning. Grateful for all contributions!