I want the body to have margins of like 15px and stay 100% height and width of the window, 100 vh is the same thing.
I want a welcome screen which will fit to the screen (resolution of the user) so basically the body should resize it self to the screen height and width when the user resizes the window.
Ok, so the problem is when I use 100% or vh with margin it overflows, i cannot work with hidden cuz i need the bottom part, now its okay with width because the its display block which fixes the problem for width.
h
https://jsfiddle.net/0dx36zb4/
Try
body {
width: 100vw;
height: 100vh;
}
You have to use box-sizing: border-box; along with width: 100vw; and height; 100vh;
Don't use margin on the body, instead use a parent/child element and set padding on the parent. Margin is outside of the element, and so is not considered in the calculation - yours overflows because it is 100% of the width/height, plus 15px in each direction. Padding is inside the element and you can set the browser to consider it in your width/height specifications with the box-sizing:border-box property.
HTML
<html>
<body>
<div id="parent">
<div id="child">
</div>
</div>
</body>
</html>
CSS
body {
width:100vw;
height:100vh;
}
#parent {
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
width:100%;
height:100%;
padding:15px;
margin:0;
}
#child {
width:100%;
height:100%;
margin:0;
}
EDIT
I was also able to get your JSFiddle working so you can see an example of that if you replace your code with the code below.
html,body {
background-color: red;
height: 100%;
margin:0;
padding:15px;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
section {
background-color: yellow;
height: 100%;
margin: 0;
}
Use padding and box-sizing to fix the issue. I updated your JSFiddle to work:
https://jsfiddle.net/0dx36zb4/4/
html,body {
background-color: red;
height: 100%;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
body {
padding:15px;
margin: 0;
}
section {
background-color: yellow;
height: 100%;
}
h1 {
margin: 0;
padding: 0;
}
<body>
<section>
<h1>hello</h1>
</section>
</body>
html, body {
height: 100%;
width: 100%;
margin: 0px;
border: 0;
overflow: hidden;
}
.splash {
height: 100%;
width: 100%;
z-index: 0;
}
<img src="http://www.planwallpaper.com/static/images/desktop-year-of-the-tiger-images-wallpaper.jpg
" class="splash" />
Related
So I made a contact page and I want the footer div to be sticking to the bottom of the page not right after the contact form.
But if I put everything to a container div with height:100%; and make footer bottom:0; then the page will be "too long", you have to scroll, etc...
My css so far:
#footer{
background-color:#fff;
font:bold 14px;
color:#1E88E5;
width:100%;
height:auto;
padding:1%;
border-top:1px solid #1E88E5;
}
Footer is just a normal full width div with some centered text atm.
You can probably use position: fixed to achieve this.
.footer {
position: fixed;
bottom: 0;
}
With this you will need to offset the bottom of the page so would suggest adding a padding-bottom to .main that is the height of the footer.
.main {
padding-bottom: 30px /*whatever the height of your footer is*/
}
Pritesh Gupta's solution works really well for me:
I'm copy+pasting the code in case their site goes down:
Here's the HTML:
<!DOCTYPE html>
<html>
<head>
<title>Sticky Footer</title>
</head>
<body>
<main>stuff</main>
<footer>© 2016</footer>
</body>
</html>
Here's the CSS:
body {
margin: 0;
}
main {
min-height: calc(100vh - 4rem);
}
footer {
height: 4rem;
}
I don't know if it works in old browsers but I'm not so worried about that myself.
It also depends on you knowing the height of your footer, although it's worth pointing out that you don't necessarily have to set the height manually like in the code above since you can always figure out what it is if you know how much vertical padding and line-height the contents have...
Hope this helps, I spent most of the morning trying every single sticky footer tutorial on the web before stumbling across this technique and whilst other techniques do work this one requires minimal effort.
If you need sticky footer you can make it with 2 solutions.
Solution 1:
HTML:
<div class="wrap">
Content
</div>
<div class="footer">
Sticky Footer
</div>
CSS:
body, html, .wrap{
height:100%;
}
body > .wrap{
height:auto;
min-height:100%;
}
.wrap:after {
content: "";
display: block;
height: 100px;
}
.footer{
background:#662e8c;
margin-top:-100px;
height:100px;
color:#fff;
position:relative;
line-height:180%;
padding:0 10px;
}
Example: https://jsfiddle.net/ta1amejn/
Solution 2 (With table properties):
HTML:
Content
Footer
CSS:
body{
display:table;
width: 100%;
}
html, body{
height:100%;
}
.main{
height:100%;
width:100%;
padding-bottom:20px;
background:#eee;
display:table-row;
}
.footer{
/*height:30px;*/
line-height:30px;
width:100%;
background:#00f0ad;
display:table-row;
}
Example: https://jsfiddle.net/zbtaoq1b/
If you want a fixed footer use this solution:
.footer{
position: fixed;
bottom: 0;
}
You can do that easily with the display: flex.
You don't care about height body or wrapper tag.
Example: Please change the height of main tag any value if you want, footer always sticky to bottom(not position: fixed).
https://codepen.io/tronghiep92/pen/dzwRrO
HTML markup
<div id="wrapper">
<header>my header</header>
<main>main content, please change height</main>
<footer>
my footer
</footer>
</div>
CSS Solution
#wrapper {
display: flex;
flex-direction: column;
min-height: 100vh;
}
header {
height: 100px;
background: yellow;
}
footer {
height: 50px;
background: red;
margin-top: auto; /* this is the solution */
}
main {
height: 100px
}
Or you can:
#wrapper {
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 100vh;
}
header {
height: 100px;
background: yellow;
}
footer {
height: 50px;
background: red;
}
main {
flex: 1;
height: 100px;
}
I have a div and I want it to fill the whole page without any horizontal or vertical scrolling.
The html is like:
<body>
<div class="container">
</div>
</body>
and the css like:
body{
background: #222;
margin:0;
padding: 0;
}
.container{
margin:0 auto;
padding:20px;
width:800px;
background: rgba(20,20,20,0.2);
height: 100vh;
}
Normally with vh it works, but because of some padding applied on container it doesn't work. So what technique can I use to solve this problem?
The JSFiddle is here
Try using box-sizing: border-box on your .container element. Doing so will have the padding and border of an element included with width and height assignments.
.container {
box-sizing: border-box;
margin: 0 auto;
padding: 20px;
width: 800px;
background: rgba(20,20,20,0.2);
height: 100vh;
}
This has to do with the way that css adds the padding to the height to calculate the total height. There's one quick and flexible fix for all of your elements though, as explained in Paul Irish's box-sizing:
/* apply a natural box layout model to all elements, but allowing components to change */
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
Not Sure Will this help you but you can give it a try-
give position relative to your body and position fixed to your div.container with width 100% and height 100%.
body{
background: #222;
margin:0;
padding: 0;
position:relative;
}
.container{
position:fixed;
left:0;
top:0;
width:100%;
background: red;
height: 100%;
}
Box-sizing FTW! If you need to include the padding as part of the elements dimensions then box-sizing: border-box is your only hope.
FWIW you should be aware that Viewport Units are not fully supported so if you need something more cross-browser you can easily avoid using 100vh by using 100% instead.
E.G:
html, body {
height:100%;
min-height:100%;
}
body{
background: #222;
margin:0;
padding: 0;
}
.container{
margin:0 auto;
padding:20px;
width:800px;
background: red;
height: 100%;
box-sizing: border-box;
}
<div class="container">
</div>
Trying to stack 3 DIVs vertically, so that the top DIV is 25% of screen height, middle is 50%, and bottom is 25%, but they seem to extend the screen and I end up having a scrollbar.
body,html {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#top {
width: 100%;
height: 25%;
background: #464646;
}
#middle {
width: 100%;
padding: 15px 0 15px 0;
margin: 0 auto 0 auto;
min-width: 657px;
height: 50%;
text-align: center;
vertical-align:middle;
}
#bottom {
width: 100%;
padding: 15px 0 15px 0;
height: 25%;
background: #988056;
}
<div id="top"></div>
<div id="middle"><img src="logo.png"></div>
<div id="bottom"></div>
As Hashem mentions in a comment above, box-sizing: border-box is considered best practice nowadays. Add the following to your CSS and you should be good to go:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
Here is a good read-up for you.
That said, if you are working on an existing product and have lots of legacy code that would be broken if you did this, you need to work around the margins and paddings on your site sections, they add height, and that makes it all add up to more than 100%.
And if you are uncomfortable with that as well, look up flex-box layout. Only works in modern browsers though, so don't do it if you need old IE support.
This is due to the padding that you have added to middle and bottom divs.
The width and height styles always specify the width/height of textual area i.e. width/height of the "div's content" and they do NOT include the padding value. The padding is an extra space added apart from the width/height.
Try the following, and it should give you the desired results:
HTML:
<div id="top"></div>
<div id="middle"><img src="logo.png"></div>
<div id="bottom"></div>
CSS:
body,html {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#top {
width: 100%;
height: 25%;
background: #464646;
}
#middle {
width: 100%;
margin: 0 auto 0 auto;
min-width: 657px;
height: 50%;
text-align: center;
vertical-align:middle;
}
#bottom {
width: 100%;
height: 25%;
background: #988056;
}
Working LIVE.
The CSS flexbox layout module is especially made to handle requirements like this.
You can use the flex-grow property:
The flex-grow property is a sub-property of the Flexible Box Layout module.
IT defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.
For example, if all items have flex-grow set to 1, every child will set to an equal size inside the container. If you were to give one of the children a value of 2, that child would take up twice as much space as the others.
*{
margin: 0;
padding: 0;
}
html,body {
height: 100%;
}
#container{
-webkit-display:flex;
-moz-display:flex;
-ms-display:flex;
display:flex;
-webkit-flex-direction:column;
-moz-flex-direction:column;
-ms-flex-direction:column;
flex-direction:column;
height:100%;
}
#top {
-webkit-flex:1;
-moz-flex:1;
-ms-flex:1;
flex:1;
background: #464646;
}
#middle {
-webkit-flex:2;
-moz-flex:2;
-ms-flex:2;
flex:2;
background:dodgerblue;
}
#bottom {
-webkit-flex:1;
-moz-flex:1;
-ms-flex:1;
flex:1;
background: #988056;
}
<div id="container">
<div id="top"></div>
<div id="middle"></div>
<div id="bottom"></div>
</div>
In this scenario, since you're concerned about screen height, you might want to investigate the 'vh' css rule.
For instance, if you wanted to stack your top, middle, and bottom evenly, you could do it with pure css:
#top, #bottom, #middle {
height: 32vh;
}
Or, as pertains to the question:
#top { height: 25vh; }
#middle { height: 50vh; }
#bottom { height 24vh; } /*24 vh so you have a little wiggle room*/
Examine here:
body { margin : 0; padding: 0}
div { border: #ccc solid 1px; }
#top { height: 25vh; }
#middle { height: 50vh; }
#bottom { height: 24vh; }
/*24 vh so you have a little wiggle room*/
<div id="top">top</div>
<div id="middle">middle</div>
<div id="bottom">bottom</div>
This question already has answers here:
Make a div fill the height of the remaining screen space
(41 answers)
Closed 8 years ago.
I have a header div and a div underneath it. I need the div underneath the header div to adjust depending on the height of the browser window size.
In CSS, when I add height:100% it creates a scroll bar at the side of the page. When I adjust the percentage of the width, spacing at the bottom of the page constantly changes because it is done with percentages.
I would like the div below the header to always adjust with the window size in height with no spacing at the bottom.
How do I do this?
Here is the Fiddle
JS Fiddle
I am not sure why but in JSFiddle the bottom div is not extending height: 100%
here is the code:
HTML
<div class = "main">
Header
</div>
<div class="left">
Bottom Div
</div>
CSS
.main {
width:100%;
height:60px;
border: solid;
}
.left {
height: 100%;
width: 300px;
border:solid;
}
try to use something like this code
html:
<div class = "main">
Header
</div>
<div class="left">
Bottom Div
</div>
css:
* {
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
}
html, body {
height:100%;
}
body {
padding:60px 0 0 0; /* 60 — header height*/
margin:0;
}
.main,
.left {
border:1px solid #000;
}
.main {
width:100%;
height:60px;
margin-top: -60px; /* 60 — header height*/
}
.left {
height: 100%;
width: 300px;
}
You have a few options to achieve the layout you would like.
There are plenty of answers that address your problem from this similar question:
Make a div fill the height of the remaining screen space
However, here is my solution:
Just change your CSS a bit
body, html {
height: 100%;
padding: 0;
margin: 0;
}
.main {
width:100%;
height:60px;
border: solid;
position: absolute;
background-color: #fff;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.left {
height: 100%;
width: 300px;
border:solid;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding-top: 60px;
}
The box-sizing will prevent the padding-top and the borders from pushing the dimensions outside the browser window. The body,html height: 100%; is needed to allow other items to be 100% height (why your fiddle wouldn't work).
CSS allows you to do some basic math, so the following would help you:
Given that your header has a fixed height of 60px:
.left {
height: calc(100% - 60px);
}
Edit: you also have some extra padding and borders that you might want to take into consideration while calculating. Although I'm not a big fan of hard-coding values like that.
Try this in your style sheet
CSS
.left {
height: 100vh;
width: 100%;
border:solid;
}
Refer link
https://stackoverflow.com/questions/1622027/percentage-height-html-5-css
I have two main div. One is #header for menu, and one is #container for the content. I want that #container to reach the bottom of the page, whether is filled with content or not.
The problem is that adding height:100%; to body, html and #container causes the additional white space and scrollbar, which i do not want when not necessary.
HTML:
<div id='header'></div>
<div id='container'></div>
CSS:
body{
margin:0;
}
body,html {height:100%;}
#header {
height:70px;
width:100%;
background-color:red;
}
#container {
width:600px;
background-color:gray;
height:100%;
margin:0 auto;
}
JSFIDDLE: http://jsfiddle.net/ymBnw/
If you play with the padding and the margin of the #container, and position the #header absolutely, you can achieve this. I'm not taking into consideration the width, which you can set as you like.
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
#header {
position: absolute;
top: 0;
width: 100%;
height: 70px;
background-color: red;
z-index: 10;
}
#container {
width: 600px;
height: 100%;
background-color: gray;
margin: -70px auto -70px auto;
padding-top: 70px;
}
#content
{
padding-top: 70px;
}
Working example: http://jsfiddle.net/ymBnw/15/
EDIT
I've made a mistake setting the padding, which needs to be (obviously) the double of the margin (140px instead of 70px). Code fixed.
EDIT 2
Not happy again. The previous edit made the scrollbars to come back. The new solution proposed adds a new div within the #container.
Yes it would do that. Because you've given the #container 100% height, that is relative to the body. So you've given the #container the same height as the body. On top of that, you've got the #header height. So your total content is now 100% + 70px (header).
The way around this would be to set no height on the #container and have the grey background colour on the body.
You could also try:
#container {
position: relative;
z-index: 0;
top: -70px;
padding-top: 70px;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box; }
#header {
position: relative;
z-index: 10; }
Or you could try:
#container {
margin-top: -70px;
padding-top: 70px;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box; }
I'm not a fan of the second method. You could also do this with absolute positioning and a 70px padding on the top of the container.
You could do a position:absolute on the container div.
Code:
#container {
width:600px;
background-color:gray;
margin:auto;
bottom:0;
top:70px;
position: absolute;
left:50%;
margin-left:-300px;
}
Demo
You should use min-height: 100% instead of height: 100% to fix the background-color issue.
Here is a working solution:
CSS
#header {
height:70px;
width:100%;
background-color:red;
position: relative;
z-index: 10;
}
#container {
width:600px;
background-color:gray;
min-height: 100%;
margin:0 auto;
margin-top: -70px;
padding-top: 70px;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
JSFiddle Demo #1
JSFiddle Demo #2
As an alternative solution, instead of box-sizing, you can use ::before pseudo-element as the following:
#container {
width:600px;
background-color:gray;
min-height: 100%;
margin:0 auto;
margin-top: -70px;
}
#container:before {
content: ' ';
display: block;
height: 70px;
}
JSFiddle Demo #3
You're specifying the height of the container to be 100% but you're then setting the header height to be 70px. This will ultimately lead to your full body being 100% of the browser window + 70px.
That's why you will be getting a scrollbar, because 100% + 70px results in overflow.
Edit:
As others have suggested, you could use an absolutely positioned header, with a padded container. You would obviously lose flow in this scenario though. When it comes to specifying heights in HTML, there are always trade-offs...
try this
#container {
width:100%;
background-color:gray;
height:100%;
margin:0 auto;
}
#header {
height:70px;
width:100%;
background-color:red;
position:absolute;
}
demo
Using the new flexbox layout, all you have to do is to add these CSS properties to the body.
body {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
}
This sets the layout to flexbox and specifies that the direct children of the body element should be aligned top to bottom. For a more thorough guide to flexbox, have a look at this tutorial. Note that the flexbox layout is currently a candidate recommendation and older browsers are not going to support it. Current Webkit based browsers still need the -webkit vendor prefix.