Take height of parent and assign to child with jQuery - javascript

I have this CSS and HTML structure:
.body_wrapper {
height: 100%;
}
.forumcontents {
width: calc(100% - 290px) !important;
float: left;
}
.sidebar_container {
width: 270px;
float: right;
height: 100%;
}
.clearfix {
zoom: 1;
clear: both;
}
<div class="body_wrapper clearfix">
<div class="forumcontents"></div>
<div class="sidebar_container"></div>
</div>
Unfortunately, i need to float .sidebar_container, but with the float, this div doesn't take the 100% of the height of .body_wrapper and I for some reasons can't use the absolute positioning.
I tried with a display: table-cell to .sidebar_container but doesn't work, so i thought that the only solution is to take the .body_wrapper height after page loading ad assign it to the .sidebar_container.
How I can do this with jQuery?

Here is the jquery
$(function () {
$(".sidebar_container").height($(".body_wrapper").height());
});
Here is a fiddle showing it in action (I added borders to show boundaries): http://jsfiddle.net/48uxr49p/
However, in the jsfiddle, using height:100% on sidebar works fine (I commented it out to show that the jquery works). You may want to dig around to see if there is another element/CSS preventing height:100% from working.
Here is the jsfiddle demonstrating that height:100% works: http://jsfiddle.net/w3dmx7qm/

You can do that with flexbox.
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html,
body {
height: 100%;
}
.body_wrapper {
height: 100%;
background: #f00;
display: flex;
}
.forumcontents {
flex: 1;
background: lightgreen;
}
.sidebar_container {
flex: 0 0 270px;
background: lightblue;
}
<div class="body_wrapper">
<div class="forumcontents"></div>
<div class="sidebar_container"></div>
</div>

Related

fix sliding accurately based on specific proportion ? CSS/JS

The project:
I made a 3 items slider just using CSS and JS. I'm trying to adjust the proportion of one item to make sliding based on this proportion.
The problem:
the proportion isn't fixed and there is a slight space from the left side of all items and between items themselves. It seems they have borders that take a tiny space. and that space also affects sliding itself. I don't know if it's a CSS or JS problem!
Source Code on Codepen
HTML
<div id="slider">
<div class="SlideContainer">
<div class="img">
<img src="https://www.nicepng.com/png/detail/11-112605_punk-cat-berkley-cats-illustrations.png">
</div>
<h1 style="text-align:center;">1</h1>
</div>
<div class="SlideContainer">
<div class="img">
<img src="https://i.pinimg.com/736x/84/78/b9/8478b93283dc6eac074d437097a9fe74.jpg">
</div>
<h1 style="text-align:center;">2</h1>
</div>
...... etc
</div>
CSS
#slider {
height: 500px;
margin: 0;
padding: 0 .5%;
overflow: hidden;
white-space: nowrap;
scroll-behavior: smooth;
}
.SlideContainer{
vertical-align: top;
background-color: purple;
color: white;
height: 99% ;
margin: .5% 0;
width: 33%;
display: inline-block;
}
.img {
overflow: hidden;
max-width:200px;
max-height:200px;
border-radius: 50%;
display: block;
margin: 10px auto 0 auto;
text-align: center;
}
.img img{
height: 200px;
width: 200px;
}
JS
var Container = document.getElementById("slider");
var unitWidth = (Container.offsetWidth/3);
var slidesNo = (document.getElementsByClassName("SlideContainer").length - 2);
var i = 0;
var positionInfo = Container.getBoundingClientRect();
var height = positionInfo.height;
var width = (positionInfo.width/3);
function SlideLoop(){
var interval = setInterval(function(){
Container.scrollTo((unitWidth * i), 0);
//Container.scrollBy(unitWidth, 0);
i++;
if(i == slidesNo) {
i = 0;
clearInterval(interval);SlideLoop();
}
}, 1500);
};
SlideLoop();
Your problem is with using white-space: nowrap; in the slider and display: inline-block; in the slider items.
Using such methods to display items is not recommended because it would cause strange unexpected behavior. I suggest you learn flexbox. This is a great and easy guide that will help you.
as for your problem, change those css elements like this and it should work propely:
#slider {
height: 500px;
margin: 0;
padding: 0 .5%;
overflow: hidden;
scroll-behavior: smooth;
display: flex; /* displays items horizontally */
}
.SlideContainer{
vertical-align: top;
background-color: purple;
color: white;
height: 99% ;
margin: .5% 0;
min-width:33%; /* force the minimum width of items */
}

While priting a div content it is taking space and blank pages

I want to print a specific <div> with an id using onclick() function on a button and window.print() property on it.
For this, I wrote some #media print CSS.
It prints the <div> content but it takes a lot of spacing, causing blank pages. Here is my code:
#media print {
body * {
visibility: hidden;
}
#printpage,
#printpage * {
visibility: visible;
}
#printpage {
overflow: visible;
position: absolute !important;
left: 0;
top: 0;
}
}
<button type="submit" class="btn btn-primary" onclick="window.print();">Print</button>
<div id="printpage"></div>
visibility: hidden; doesn't stop the element from taking up space on the page, it effectively makes it invisible but still take up space.
You may want to use display: none; which will also remove the space that the element takes up.
.container {
display: inline-block;
width: 100px;
height: 100px;
background-color: #888;
vertical-align: top;
}
.container div {
height: 20px;
}
.vis {
visibility: hidden;
}
.dis {
display: none;
}
<div class="container">
<div class="vis"></div>
Visibility
</div>
<div class="container">
<div class="dis"></div>
Display
</div>
How about setting the height and width of the printpage that has fixed position
#media print {
body{ margin: 0; padding: 0 }
#printpage {
min-height: 100vh; margin: 0; width: 100%; position: fixed; top: 0; left: 0
}
}

Is there css height: rest property? [duplicate]

This question already has answers here:
Make a div fill the height of the remaining screen space
(41 answers)
Closed 5 years ago.
If you have an html document that goes like this:
<body>
<div id="top"></div>
<div id="rest"></div>
</body>
and I give the "top" div a height of 40px, is there a way to give "rest" div the rest of the height of the body in css. I know it's possible in Javascript like this
function derest(){
var bodyheight = window.getComputedStyle(document.getElementsByTagName('body')[0], null).height;
bodyheight = bodyheight.substring(0, bodyheight.length - 2);
var topheight = window.getComputedStyle(document.getElementById('top'), null).height;
topheight = topheight.substring(0, topheight.length - 2);
restheight = bodyheight - topheight;
document.getElementById("rest").style.height = restheight;
}
but this takes a lot of time, so is there a better way to do this?
You could just use pure CSS
#top{
height:40px;
background:green;
}
#rest{
height:calc(100% - 40px);
background:red;
}
body,html{
height:100%;
margin:0;
padding:0;
}
You can use flex to allow the #rest to stretch to fill the height:
html {
height: 100%;
margin:0;
padding:0;
}
body {
height: 100%;
margin:0;
padding:0;
display: flex;
flex-direction: column
}
#top {
height: 40px;
background: red;
}
#rest {
flex: 1 1 auto;
background: blue;
}
<div id="top"></div>
<div id="rest"></div>
If the container is display: flex then you can set an element to grow (with flex-grow) to fill the remaining space.
html,
body {
min-height: 100vh;
margin: 0;
padding: 0;
}
body {
display: flex;
flex-direction: column;
}
header {
background-color: #aaf;
}
main {
background-color: #afa;
flex: 1 0 auto;
}
footer {
background-color: #faa;
}
<header>
Header
</header>
<main>
Main
</main>
<footer>
Footer
</footer>
You can use:
#top {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 40px;
}
#rest {
position: fixed;
top: 40px;
left: 0px;
width: 100%;
height: calc(100% - 40px);
}
or even use padding-top 40px instead of doing fixed the #rest div
https://jsfiddle.net/lonking/L5mwd6r5/

Give body 100% of the browser height

I have this:
I want this:
I've tried this:
html, body
{
height: 100%; //and this -> 100vh
}
but it didn't works.
Here is my code:
https://jsfiddle.net/zbjaaxe6/9/
Any solutions?
This problem is a good candidate for flexbox:
CSS
body {
display: flex;
flex-direction: column;
margin: 0;
min-height: 100vh; // set min-height instead of height, otherwise body won't
// grow with content, if content is bigger than viewport
}
header {
height: 50px; // adjust to what you want
}
main {
flex: 1; // This will make the 'main' block to expand !
}
footer {
height: 30px; // adjust to what you want
}
HTML
<body>
<header>HEADER</header>
<main>MAIN</main>
<footer>FOOTER</footer>
</body>
Result:
Fiddle
Flexbox is an IE10+ solution. Browser support in detail
"With vw/vh, we can size elements to be relative to the size of the
viewport. The vw/vh units are interesting in that 1 unit reflects 1/100th > the width of the viewport. To make an element the full width of the
viewport, for example, you'd set it to width:100vw."
-- Jonathan Snook, Sizing With CSS3's VW and VH Units
CSS:
[class*="col"] {
border: 1px solid black;
}
#menu{
display:none;
margin-top:20px;
position:absolute;
background: #fff;
z-index: 1;
}
body {
font: caption;
}
#content{
min-height: 90vh;
}
#footer{
min-height: 5vh;
}
#header{
min-height: 5vh;
}
HTML:
<div class="container">
<div class="row">
<!-- Small devices >= 768px Collapsed to start, horizontal above breakpoints -->
<div id = "header" class="col-xs-10"><span id="btnMenu" class="glyphicon glyphicon-menu-hamburger" aria-hidden="true">TITLE</div>
<div id="menu" class="col-xs-3 menu">
MENU
</div>
<div id="content" class="col-xs-10 content">
</span>CONTENT
</div>
<div class="col-xs-10" id = "footer">FOOTER</div>
</div>
</div>
Not ideal, but you could use absolute positioning:
.content {
position: absolute;
top: 20px;
bottom: 0;
}
Or, you could use viewport percentages if you are cool with supporting ie9+:
.content {
height: 100vh;
}
The styles should be on the content section, not the html/body.
EDIT: fiddle
I don't know if that's what you want but take a look :
https://jsfiddle.net/zbjaaxe6/23/
.content{
position: relative;
margin: 0;
min-height: 100vh;
}
I solved your issue using flexbox property.
.container,
.row {
display: flex;
flex-direction: column;
height: 100%;
}
#title,
#footer {
flex: none;
}
#content {
flex: 1 0 auto;
}
You can see here the solution.

How to make the main content div fill height of screen with css [duplicate]

This question already has answers here:
Make a div fill the height of the remaining screen space
(41 answers)
Closed 8 years ago.
So I have a webpage with a header, mainbody, and footer.
I want the mainbody to fill 100% of the page (fill 100% in between footer and header)
My footer is position absolute with bottom: 0. Everytime I try to set the mainbody to 100% height or change position or something it will also overflow the header. If if set the body to position absolute with top: 40 (cause my header is 40px high), it will just go 40px too far down, creating a scroll bar.
I created a simple html file since i cannot actually post the entire page/css from the actual project. With the sample code, even though the maincontent body fills the screen, it goes 40px too far down (cause of the header I assume).
html,
body {
margin: 0;
padding: 0;
}
header {
height: 40px;
width: 100%;
background-color: blue;
}
#maincontent {
background-color: green;
height: 100%;
width: 100%;
}
footer {
height: 40px;
width: 100%;
background-color: grey;
position: absolute;
bottom: 0;
}
<html>
<head>
<title>test</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<header></header>
<div id="maincontent">
</div>
<footer></footer>
</body>
</html>
Anyone knows the answer?
These are not necessary
remove height in %
remove jQuery
Stretch div using bottom & top :
.mainbody{
position: absolute;
top: 40px; /* Header Height */
bottom: 20px; /* Footer Height */
width: 100%;
}
check my code : http://jsfiddle.net/aslancods/mW9WF/
or check here:
body {
margin:0;
}
.header {
height: 40px;
background-color: red;
}
.mainBody {
background-color: yellow;
position: absolute;
top: 40px;
bottom: 20px;
width:100%;
}
.content {
color:#fff;
}
.footer {
height: 20px;
background-color: blue;
position: absolute;
bottom: 0;
width:100%;
}
<div class="header" >
</div>
<div class="mainBody">
<div class="content" >Hello world</div>
</div>
<div class="footer">
</div>
No Javascript, no absolute positioning and no fixed heights are required for this one.
Here's an all CSS / CSS only method which doesn't require fixed heights or absolute positioning:
/* Reset */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
/* Essentials */
.container {
display: table;
}
.content {
display: table-row;
height: 100%;
}
.content-body {
display: table-cell;
}
/* Aesthetics */
.container {
width: 100%;
height: 100%;
}
.header,
.footer {
padding: 10px 20px;
background: #f7f7f7;
}
.content-body {
padding: 20px;
background: #e7e7e7;
}
<div class="container">
<header class="header">
<p>This is the header</p>
</header>
<section class="content">
<div class="content-body">
<p>This is the content.</p>
</div>
</section>
<footer class="footer">
<p>This is the footer.</p>
</footer>
</div>
The benefit of this method is that the footer and header can grow to match their content and the body will automatically adjust itself. You can also choose to limit their height with css.
There is a CSS unit called viewport height / viewport width.
Example
.mainbody{height: 100vh;} similarly html,body{width: 100vw;}
or 90vh = 90% of the viewport height.
**IE9+ and most modern browsers.
This allows for a centered content body with min-width for my forms to not collapse funny:
html {
overflow-y: scroll;
height: 100%;
margin: 0px auto;
padding: 0;
}
body {
height: 100%;
margin: 0px auto;
max-width: 960px;
min-width: 750px;
padding: 0;
}
div#footer {
width: 100%;
position: absolute;
bottom: 0;
left: 0;
height: 60px;
}
div#wrapper {
height: auto !important;
min-height: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
div#pageContent {
padding-bottom: 60px;
}
div#header {
width: 100%;
}
And my layout page looks like:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
</head>
<body>
<div id="wrapper">
<div id="header"></div>
<div id="pageContent"></div>
<div id="footer"></div>
</div>
</body>
</html>
Example here: http://data.nwtresearch.com/
One more note, if you want the full page background like the code you added looks like, remove the height: auto !important; from the wrapper div: http://jsfiddle.net/mdares/a8VVw/
Using top: 40px and bottom: 40px (assuming your footer is also 40px) with no defined height, you can get this to work.
.header {
width: 100%;
height: 40px;
position: absolute;
top: 0;
background-color:red;
}
.mainBody {
width: 100%;
top: 40px;
bottom: 40px;
position: absolute;
background-color: gray;
}
.footer {
width: 100%;
height: 40px;
position: absolute;
bottom: 0;
background-color: blue;
}
JSFiddle
Well, there are different implementations for different browsers.
In my mind, the simplest and most elegant solution is using CSS calc(). Unfortunately, this method is unavailable in ie8 and less, and also not available in android browsers and mobile opera. If you're using separate methods for that, however, you can try this: http://jsfiddle.net/uRskD/
The markup:
<div id="header"></div>
<div id="body"></div>
<div id="footer"></div>
And the CSS:
html, body {
height: 100%;
margin: 0;
}
#header {
background: #f0f;
height: 20px;
}
#footer {
background: #f0f;
height: 20px;
}
#body {
background: #0f0;
min-height: calc(100% - 40px);
}
My secondary solution involves the sticky footer method and box-sizing. This basically allows for the body element to fill 100% height of its parent, and includes the padding in that 100% with box-sizing: border-box;. http://jsfiddle.net/uRskD/1/
html, body {
height: 100%;
margin: 0;
}
#header {
background: #f0f;
height: 20px;
position: absolute;
top: 0;
left: 0;
right: 0;
}
#footer {
background: #f0f;
height: 20px;
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
#body {
background: #0f0;
min-height: 100%;
box-sizing: border-box;
padding-top: 20px;
padding-bottom: 20px;
}
My third method would be to use jQuery to set the min-height of the main content area. http://jsfiddle.net/uRskD/2/
html, body {
height: 100%;
margin: 0;
}
#header {
background: #f0f;
height: 20px;
}
#footer {
background: #f0f;
height: 20px;
}
#body {
background: #0f0;
}
And the JS:
$(function() {
headerHeight = $('#header').height();
footerHeight = $('#footer').height();
windowHeight = $(window).height();
$('#body').css('min-height', windowHeight - headerHeight - footerHeight);
});
Not sure exactly what your after, but I think I get it.
A header - stays at the top of the screen?
A footer - stays at the bottom of the screen?
Content area -> fits the space between the footer and the header?
You can do this by absolute positioning or with fixed positioning.
Here is an example with absolute positioning: http://jsfiddle.net/FMYXY/1/
Markup:
<div class="header">Header</div>
<div class="mainbody">Main Body</div>
<div class="footer">Footer</div>
CSS:
.header {outline:1px solid red; height: 40px; position:absolute; top:0px; width:100%;}
.mainbody {outline:1px solid green; min-height:200px; position:absolute; top:40px; width:100%; height:90%;}
.footer {outline:1px solid blue; height:20px; position:absolute; height:25px;bottom:0; width:100%; }
To make it work best, I'd suggest using % instead of pixels, as you will run into problems with different screen/device sizes.
Relative values like: height:100% will use the parent element in HTML like a reference, to use relative values in height you will need to make your html and body tags had 100% height like that:
HTML
<body>
<div class='content'></div>
</body>
CSS
html, body
{
height: 100%;
}
.content
{
background: red;
width: 100%;
height: 100%;
}
http://jsfiddle.net/u91Lav16/1/
Although this might sounds like an easy issue, but it's actually not!
I've tried many things to achieve what you're trying to do with pure CSS, and all my tries were failure. But.. there's a possible solution if you use javascript or jquery!
Assuming you have this CSS:
#myheader {
width: 100%;
}
#mybody {
width: 100%;
}
#myfooter {
width: 100%;
}
Assuming you have this HTML:
<div id="myheader">HEADER</div>
<div id="mybody">BODY</div>
<div id="myfooter">FOOTER</div>
Try this with jquery:
<script>
$(document).ready(function() {
var windowHeight = $(window).height();/* get the browser visible height on screen */
var headerHeight = $('#myheader').height();/* get the header visible height on screen */
var bodyHeight = $('#mybody').height();/* get the body visible height on screen */
var footerHeight = $('#myfooter').height();/* get the footer visible height on screen */
var newBodyHeight = windowHeight - headerHeight - footerHeight;
if(newBodyHeight > 0 && newBodyHeight > bodyHeight) {
$('#mybody').height(newBodyHeight);
}
});
</script>
Note: I'm not using absolute positioning in this solution, as it might look ugly in mobile browsers
This question is a duplicate of Make a div fill the height of the remaining screen space and the correct answer is to use the flexbox model.
All major browsers and IE11+ support Flexbox. For IE 10 or older, or Android 4.3 and older, you can use the FlexieJS shim.
Note how simple the markup and the CSS are. No table hacks or anything.
html, body {
height: 100%;
margin: 0; padding: 0; /* to avoid scrollbars */
}
#wrapper {
display: flex; /* use the flex model */
min-height: 100%;
flex-direction: column; /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */
}
#header {
background: yellow;
height: 100px; /* can be variable as well */
}
#body {
flex: 1;
border: 1px solid orange;
}
#footer{
background: lime;
}
<div id="wrapper">
<div id="header">Title</div>
<div id="body">Body</div>
<div id="footer">
Footer<br/>
of<br/>
variable<br/>
height<br/>
</div>
</div>
In the CSS above, the flex property shorthands the flex-grow, flex-shrink, and flex-basis properties to establish the flexibility of the flex items. Mozilla has a good introduction to the flexible boxes model.

Categories