100% height minus nPx via css or jquery - javascript

i am trying to make my main content div 100% height base on browser windows size but i want it will start after header. i am getting 100% height but it is start from top but i want it will leave header are then it will 100% height. bellow is code.
mainly i want to make my contentarea div will cover 100% width and height but minus will be header height
<style>
* {
margin: 0;
padding: 0
}
body {
background-color: #F90;
}
header {
width: 100%;
float: left;
height: 100px;
background-color: #36F;
border-bottom: solid 2px #FF3300;
opacity: 0.3;
z-index: 5;
}
.contentarea {
background-color: #396;
z-index: 2;
}
</style>
<script>
jQuery(function($){
//Fix height of the slider wraper
$(window).on('resize', function(){
var h = $(this).height() - $('header').height();
if(h<=200) h=200
$('#home').height(h);
}).trigger('resize');
});
</script>
<header>
<div class="companyname"> sdf </div>
<div class="nav">
<ul>
<li>Home</li>
<li>Space Available</li>
<li>Parking / Amenities</li>
</ul>
</div>
</header>
<div class="contentarea" id="home">adasdasdasdas
adsada
dasdasdas
<div class="footer">copy right</div>
</div>
demo http://jsfiddle.net/o0Lcrv3w/
i already search some in here and got many solution, many post i tried with those but not really work any yet.
like
height: -o-calc(100% - 100px); /* opera */
height: -webkit-calc(100% - 100px); /* google, safari */
height: -moz-calc(100% - 100px); /* firefox */

Update Content area class as:
.contentarea {
background-color: #396;position:relative; z-index:2;
top:100px;bottom:0px;}
You can check it at http://jsfiddle.net/o0Lcrv3w/2/

Why not make the body 100% height and put the header inside it like this:
body {
height: 100%;
background-color: blue;
padding-top: 100px 0 0;
position: relative;
margin: 0;
}
header {
top: 0;
position: absolute;
height: 100px;
background-color: red;
width: 100%;
}
http://jsfiddle.net/91qwq6rn/

Related

Making div fade in and out when scrolling

I have a div that is set to:
{
height: 100vh;
width: 100%;
background-color: black;
opacity: 0;
position: absolute;
z-index: 2;
}
This div is also at the top of the page.
I want to make it so that as I scroll down (and away from the div), it slowly fades in, and when I scroll back up it fades out.
How would I do this?
You may refer following code snippet. The point I am trying to make is in the script tag at the bottom just added the window scroll function which sets the opacity to your entire window of desired height in your css class ".top". So when you try to scroll in and out it will dynamically add an animation effect to your window.
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<style>
body {
margin: 0;
height: 1000px;
}
.top {
margin: 0;
position: relative;
width: 100%;
background-color: #aaa;
height: 300px;
opacity: 1;
text-align: center;
font-family: 'helvetica';
font-size: 80px;
font-weight: 100;
color: #fff;
}
.title {
position: absolute;
top: 60%;
left: 100px;
}
</style>
</head>
<body>
<div class="top">
<div class="title">Fade Away</div>
</div>
</body>
</html>
<script>
$(window).scroll(function() {
$(".top").css("opacity", 1 - $(window).scrollTop() / 250);
});
</script>
document.body.addEventListener('wheel', (e) => {
let faceLayer = document.getElementById('your_id');
faceLayer.style.opacity = Math.abs(faceLayer.getBoundingClientRect().top / faceLayer.clientHeight).
})

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/

How to display background color linearly after scrolling on menu section?

I have to display linearly background color after scroll on menu. In the current section there is no background color but after scrolling a background color should be displayed. How can I achieve this?
//jQuery to collapse the navbar on scroll
$(window).scroll(function() {
if ($(".entry_section").offset().top > 50) {
$(".scroll-menu").addClass("scroll-menu-padding");
$(".scroll-menu").addClass("fixed-entry-field-scroll-bg");
} else {
$(".scroll-menu").removeClass("scroll-menu-padding");
$(".scroll-menu").removeClass("fixed-entry-field-scroll-bg");
}
});
body{
height: 900px;
}
.entry_section
{
position: fixed;
top: 0;
left: 0;
right: 0;
height: 0;
text-align: center;
}
.fixed-entry-field
{
display: inline-block;
color: red;
}
.fixed-entry-field-scroll-bg
{
background-color: #000;
width: 100%;
padding: 25px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="entry_section scroll-menu" >
<div class="fixed-entry-field">
<p>linear background color after scrolling</p>
</div>
</div>
It looks weird as you're attributting height: 0; on your fixed element, that way when you apply padding: 25px it pushes the content down getting that unlinear effect you've mentioned.
By simply removing that constrain you get the desired effect on your menu upon scrolling.
Check this updated snippet for full behaviour.
PS: If you want the height to remain the same, just remove that padding of 20px on your .fixed-entry-field-scroll-bg class that it will do the trick.
//jQuery to collapse the navbar on scroll
$(window).scroll(function() {
if ($(".entry_section").offset().top > 50) {
$(".scroll-menu").addClass("scroll-menu-padding");
$(".scroll-menu").addClass("fixed-entry-field-scroll-bg");
} else {
$(".scroll-menu").removeClass("scroll-menu-padding");
$(".scroll-menu").removeClass("fixed-entry-field-scroll-bg");
}
});
body{
height: 900px;
}
.entry_section
{
position: fixed;
top: 0;
left: 0;
right: 0;
text-align: center;
}
.fixed-entry-field
{
display: inline-block;
color: red;
}
.fixed-entry-field-scroll-bg
{
background-color: #000;
width: 100%;
padding: 25px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="entry_section scroll-menu" >
<div class="fixed-entry-field">
<p>linear background color after scrolling</p>
</div>
</div>
check the css part .fixed-entry-field-scroll-bg here I used transition for the effect and linear-gradient for the color LiveFiddle. For more details read this Linear Gradient & for Transition Effect.
//jQuery to collapse the navbar on scroll
$(window).scroll(function() {
if ($(".entry_section").offset().top > 50) {
$(".scroll-menu").addClass("scroll-menu-padding");
$(".scroll-menu").addClass("fixed-entry-field-scroll-bg");
} else {
$(".scroll-menu").removeClass("scroll-menu-padding");
$(".scroll-menu").removeClass("fixed-entry-field-scroll-bg");
}
});
body{
height: 900px;
}
.entry_section
{
position: fixed;
top: 0;
left: 0;
right: 0;
height: 0;
text-align: center;
}
.fixed-entry-field
{
display: inline-block;
color: red;
}
.fixed-entry-field-scroll-bg
{
/* For browsers that do not support gradients */
background: red;
/* Standard syntax */
background: linear-gradient(#000, #bbb);
/* For Safari this the part */
-webkit-transition: width 0.7s, height 0.8s;
transition: width 0.7s, height 0.8s;
width: 100%;
height: 75px;
padding: 25px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="entry_section scroll-menu" >
<div class="fixed-entry-field">
<p>linear background color after scrolling</p>
</div>
</div>

Make a child of a div's position fixed to the top of the div on scroll and also animate height of child

I have div called box with a header div as a child. when the div scrolls I want that header div to be sticked(fixed) to the top so the users can always see the header. when the user scrolls down,the header's height should decrease to take up less space but to still allow users to see the header content. When the user scrolls back up the header should be unstuck and the height should be like the way it was before any scrolling took place when it was at the top.
I don't like my attempt I'm about to show you because I get the offset of the box relative to the document. I feel like that might not be necessary because there might be a css solution. this one is for the scroll of the whole document. That demo shows using position fixed and it fixes to the top of the page. I cant use position fixed because I believe that is meant for the window and not for divs so what the next best thing?
I dont like how the header's width is over the scrollbars. and the animation is jumpy and it doesn't work.
$(function(){
var btop = $(".box").offset().top;
bwidth = $(".box").innerWidth();
$(".box").on("scroll", function(e){
if($(this).scrollTop() > 50){
$(this).find(".header").css({
"position" : "absolute",
"top" : btop,
"max-width" : bwidth,
}).animate({
"height" :"2em"
})
}else{
$(this).find(".header").css({
"position" : "static",
"top" : btop,
"max-width" : bwidth
}).animate({
"height" :"3em"
})
}
})
})
.box{
margin: 4em auto;
height: 12em;
width: 20em;
background: blue;
overflow-y: scroll;
/*overflow-x: hidden;*/
/*position: relative;*/
}
.header{
background: orange;
width: 100%;
height: 3em;
font-size: 1.3em;
}
.content{
width: 90%;
margin: 0 auto;
background: powderblue;
height: 12em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="box">
<div class="header">This will stick</div>
<div class="content">Other content</div>
<div class="content">Somemore content</div>
</div>
It's just an idea, but in your situation, you can fake a sticky header with a simple trick.
Remove the overflow-y: scroll property of your .box div.
Set a position: absolute property to your .header.
Add a div .content-container who contains your .content div and
set it a position: absolute property too and an overflow-y: scroll.
This way your header is always on top of your .box div and you can scroll your content. Then with your JS code you can change the height of your header on scroll event.
$(document).ready(function(){
$('.content').scroll(function(){
if ($('.content').scrollTop() >= 60){
$('.header').addClass('sticky');
} else {
$('.header').removeClass('sticky');
}
});
});
.box{
margin: 4em auto;
height: 360px;
width: 20em;
background: blue;
position: relative;
}
.header{
position: absolute;
top: 0;
background: orange;
width: 100%;
height: 60px;
font-size: 1.3em;
z-index: 9999;
transition: height 0.3s ease 0s;
-webkit-transition: height 0.3s ease 0s;
-moz-transition: height 0.3s ease 0s;
}
.header.sticky{
height: 30px;
}
.content{
position: absolute;
top: 0;
width: 100%;
height: 300px;
padding-top: 60px;
background: powderblue;
overflow-y: scroll;
}
.content > div{
height: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="box">
<div class="header">This will stick</div>
<div class="content">
<div>Content</div>
<div>Somemore content</div>
</div>
</div>

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