Why my page in bigger than my window in html? [duplicate] - html

http://codepen.io/basickarl/pen/Wrwdam?editors=110
html:
<div class="back">
</div>
css:
html,
body {
height: 100%;
}
div.back {
margin-top: 50px;
display: absolute;
width: 30px;
height: 30px;
background-image: url('http://simpleicon.com/wp-content/uploads/arrow-35.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
The scrollbar on the right displays. I have to have html, body at 100% because of a sticky footer I'm using. Any ideas?

the body element has a default margin: 8px; in most major browsers so first you have to remove that by resetting the margin property on the body element
body {
margin: 0;
}
Then, instead of using height: 100%; on the body use min-height: 100%; so that the body can also grow beyond 100% height when used in conjunction with overflow: hidden;
Since min-height doesn't work if the parent height isn't explicitly set, a height attribute has to be added to the html element as well.
html {
height: 100%;
}
body {
margin: 0;
min-height: 100%;
overflow: hidden;
}
Also, your div.back selector has an invalid value on the display property. the display property accepts inline, inline-block, block, none, flex, inline-flex, grid etc... whereas the position property accepts static (default for every element), absolute, relative or fixed. This is not a problem but rather something the browser just ignores as it doesn't understand it.
div.back {
display: absolute; // <------ remove this line
//... snipped ...
}

It's partially because the default browser style sheets add some margins and/or padding. Resetting them to 0 will fix part of it (JSFiddle).
However you've also got a div with some margins applied (50px). The way the box-model works will mean you'll need to minus that from your 100%. You can use calc if you really need to (or just change it to padding):
height: calc(100% - 50px);
Example using the above code: http://codepen.io/anon/pen/mVPpYK
Example using padding: http://codepen.io/anon/pen/OMNzqB?

If you inspect element, you can see it because of back margin-top style.
Solution
Instead of margin-top, use top.
And then add margin: 0to your HTML/BODY.

There is another thought, even though some of these answers work. Set the body to have a margin of 0 and float the div to the left.
Example - Code Pen
html, body { height: 100%; margin: 0; }
div.back {
float: left;
margin-top: 50px;
width: 30px;
height: 30px;
background-image: url('http://simpleicon.com/wp-content/uploads/arrow-35.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}

You should use overflow.
body {
height: 100%;
overflow:hidden;
}

Try using:
html, body {
height: 100%; overflow: hidden;
}

Related

Parent Container not "maintaining" the same height

I have a bizarre CSS bug happening.
I was able to adjust the height of main #app to always be 100% of a given screen size.
This worked through every page of my app but one. I don't know what's possibly causing the conflict as the classes are named identically on every page.
The only difference is that some pages have less content than others. But should this matter? I've tried adding !important but no dice.
I have my:
html, body {
height: 100%;
min-height: 100%;
}
#app {
background: url('someurl') no-repeat fixed;
background-size: contain;
height: 100%;
min-height: 100%;
width: 100%;
margin: 0 auto;
display: inline-block; //this is what fixed in other pages
}
#app .child-container {
float: right;
z-index: 9999;
margin: 30px auto 0 auto;
width: 70%;
}
The bug looks like the example bellow:
Ps: I removed the background image and added a red color just to show how the height of the #app is changing inexplicably.
You could try using the vh unit instead of percentage. 1 vh represents 1% of the viewports,
therefore
html, body {
min-height: 100vh;
}
just min-height should suffice.
Further reading: https://developer.mozilla.org/en-US/docs/Web/CSS/length
If you want an HTML element to fill the screen height ( or width )
you can use 'vh/vw' ( for view height/width) instead of '%' on the body
html, body {
height: 100vh;
}

Background image not working although path is correct

I'm trying to clone a website in my local environment. For some reason, background image doesn't show even though the path is correct as shown in the jpg below.
The photo is in the same folder as index.html and style.css. The css is below:
.wallpaper {
background-image:url('jobbatical-1-wallpaper.jpg');
background-repeat: no-repeat;
background-position: top;
background-size: cover;
height: 100%;
}
.navbar {
overflow: hidden;
background-color: green;
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 10;
}
.navbar a {
float: left;
display: block;
color: blue;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
The problem is that 100% height is useless if the parent has no height.
What do height do you want the image to have? 100% of the viewport or just fill the page?
(100% of the viewport would mean that you have to scroll to see the full picture because of your navbar)
Here is the code of the image should copy the height of the viewport:
(EDIT: maybe you should set the height of the image to 100vh instead of bodys height)
html,
body
{
height: 100%;
margin: 0px;
}
.navbar
{
background-color:green;
height:50px;
}
.image
{
height: 100%;
background-image: linear-gradient(black,gray);
}
<div class="navbar"></div>
<div class="image"></div>
Here is the code if you want to fill the parent (using flex):
body
{
margin: 0px;
}
.viewport
{
height: 100vh;
display: flex;
flex-direction: column;
width: 100%;
}
.navbar
{
background-color: green;
height: 50px;
}
.image
{
background-image: linear-gradient(black,gray);
flex-grow: 1;/*This will make the image to fill the parents space*/
}
<div class="viewport">
<div class="navbar"></div>
<div class="image"></div>
</div>
Your div has no height. You can tell because you can see the position of the paragraph under it.
Since it has no height, there are no pixels to display the background image on.
You need to give it a height.
You have tried giving it height: 100% but a percentage height means height: auto (i.e. the height of the content, of which you have none) when the parent element's height is auto (which the body element is by default).
If you want to make a div take up the remaining vertical space after other elements have been accounted for, look at flexbox for layout. The Holy Grail layout is a fairly extreme example of that.
Space characters aren't allowed in url's.
So you either don't use spaces or encode it when calling it with %20 instead of the space.
I personally use - or _ instead.
Stackoverflow: "href syntax : is it okay to have space in file name"

CSS moving a div vertically down

Im trying to move a div inside another div down a bit, but when I use
margin-top: 10px;
It makes a white gap at the top. Heres the html:
<div id="topb">
<div id="selection">
</div>
</div>
And heres the CSS:
#topb {
background: url(images/tomato-background.jpg) no-repeat fixed;
background-size: cover;
height: 100%;
width: 101%;
margin-left: -10px;
}
#selection {
background-color: #4d4d4d;
width: 60%;
height: 500px;
margin: auto;
margin-top: 40px;
}
And heres a screenshot of the website:
For this, you can use position: absolute. Here is the code:
#topb {
background: url(images/tomato-background.jpg) no-repeat fixed;
background-size: cover;
height: 100%;
width: 101%;
margin-left: -10px;
}
#selection {
background-color: #4d4d4d;
width: 60%;
height: 500px;
margin: auto;
position: absolute;
top: 40px; /*This is where it is changed as well as the line above*/
}
Hope it helps! I think padding would still leave a background, so this is a better idea.
maybe you can modify the parent element by adding padding-top:10px; instead of modifying the child.
This is a "collapsed margin" problem.
It has been answered in this question :
Why would margin not be contained by parent element?
You would have to change the parent div to either (1) add a border, (2) position absolute, (3) display as inline-block, (4) overflow auto.
Refer to the posted link for more detail.
Here is the working fiddle Hope it may help.
position:absolute;
position:relative;
This is because when you have a block element (display: block) inside another block element, the margins will collapse. It will only be considered the largest margin.
So, in your example it will only consider one of the margins (40px).
See reference about collapsing margins.
There are a few workarounds. Choose any:
using padding instead of marginfor the component inside.
Change display type. e.g. display: inline-block.
Use absolute positioning.
Remove margin-top style in #selection, and apply padding-top to #topb

html, body 100% causing scrollbar to appear

http://codepen.io/basickarl/pen/Wrwdam?editors=110
html:
<div class="back">
</div>
css:
html,
body {
height: 100%;
}
div.back {
margin-top: 50px;
display: absolute;
width: 30px;
height: 30px;
background-image: url('http://simpleicon.com/wp-content/uploads/arrow-35.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
The scrollbar on the right displays. I have to have html, body at 100% because of a sticky footer I'm using. Any ideas?
the body element has a default margin: 8px; in most major browsers so first you have to remove that by resetting the margin property on the body element
body {
margin: 0;
}
Then, instead of using height: 100%; on the body use min-height: 100%; so that the body can also grow beyond 100% height when used in conjunction with overflow: hidden;
Since min-height doesn't work if the parent height isn't explicitly set, a height attribute has to be added to the html element as well.
html {
height: 100%;
}
body {
margin: 0;
min-height: 100%;
overflow: hidden;
}
Also, your div.back selector has an invalid value on the display property. the display property accepts inline, inline-block, block, none, flex, inline-flex, grid etc... whereas the position property accepts static (default for every element), absolute, relative or fixed. This is not a problem but rather something the browser just ignores as it doesn't understand it.
div.back {
display: absolute; // <------ remove this line
//... snipped ...
}
It's partially because the default browser style sheets add some margins and/or padding. Resetting them to 0 will fix part of it (JSFiddle).
However you've also got a div with some margins applied (50px). The way the box-model works will mean you'll need to minus that from your 100%. You can use calc if you really need to (or just change it to padding):
height: calc(100% - 50px);
Example using the above code: http://codepen.io/anon/pen/mVPpYK
Example using padding: http://codepen.io/anon/pen/OMNzqB?
If you inspect element, you can see it because of back margin-top style.
Solution
Instead of margin-top, use top.
And then add margin: 0to your HTML/BODY.
There is another thought, even though some of these answers work. Set the body to have a margin of 0 and float the div to the left.
Example - Code Pen
html, body { height: 100%; margin: 0; }
div.back {
float: left;
margin-top: 50px;
width: 30px;
height: 30px;
background-image: url('http://simpleicon.com/wp-content/uploads/arrow-35.png');
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
You should use overflow.
body {
height: 100%;
overflow:hidden;
}
Try using:
html, body {
height: 100%; overflow: hidden;
}

overflow: hidden of my div is allowing my window height to be truly 100% when it shouldn't be

I am pretty new to CSS and I did what I intended to do, which is to make my window width and height 100% for a div so it fills up my window. However, what I am confused about is that I had to add:
.myDiv{
height:100%;
width:100%;
overflow: hidden;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAATklEQVQYV2NkYGAwZmBgOMuAACA+CKCIMSIpADGRNaEYgKwQ3WQUjTCF6CYhWw2WAynEpgjmIpg7jUlSiM0TWK2GWUOUZ7ApxggeogIcABHJFtfoX9tJAAAAAElFTkSuQmCC
) repeat;
}
When I didn't add overflow: hidden;I got a little bit of whitespace above myDiv, which is the first div of my html. To avoid this, I came across overflow:hidden but the idea of overflow: hidden is that the content of the div should be clipped. However, in my case, the content of myDiv is expanded to cover the whitespace after adding overflow:hidden; Why is that so?
margin of body element is set to 8px that is why..
With vw and vh: (not fully supported) see can i use vieuwport units
body {
margin: 0;
}
div {
width: 100vw;
height: 100vh;
background-color: green;
}
<div>MAGIC</div>
with % height is not affected on block elements:
Eumz width is 100% on block element no need to define it again
body {
margin: 0;
}
div {
background-color: green;
}
<div>MAGIC</div>
Edit: final
body {
margin: 0;
}
div {
min-height: 100vh;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAATklEQVQYV2NkYGAwZmBgOMuAACA+CKCIMSIpADGRNaEYgKwQ3WQUjTCF6CYhWw2WAynEpgjmIpg7jUlSiM0TWK2GWUOUZ7ApxggeogIcABHJFtfoX9tJAAAAAElFTkSuQmCC
) repeat;
}
<div>MAGIC</div>
All browsers have set padding or margin around the edge of the web page so as to not have text right next to the screen.
You also don't need to use the overflow: hidden property if you use vw and vh which specify to be 100% of the viewport height/width.
This needs to be reset in your CSS:
html,
body {
padding: 0;
margin: 0;
}
.myDiv {
height: 100vh;
width: 100vw;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAATklEQVQYV2NkYGAwZmBgOMuAACA+CKCIMSIpADGRNaEYgKwQ3WQUjTCF6CYhWw2WAynEpgjmIpg7jUlSiM0TWK2GWUOUZ7ApxggeogIcABHJFtfoX9tJAAAAAElFTkSuQmCC
) repeat;
}
<div class="myDiv"></div>
Viewport Units
Add * { margin : 0; } in the beginning.
Here is the snippet.
* {
margin : 0;
}
.myDiv {
position : absolute;
height: 100%;
width: 100%;
overflow: hidden;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAATklEQVQYV2NkYGAwZmBgOMuAACA+CKCIMSIpADGRNaEYgKwQ3WQUjTCF6CYhWw2WAynEpgjmIpg7jUlSiM0TWK2GWUOUZ7ApxggeogIcABHJFtfoX9tJAAAAAElFTkSuQmCC) repeat;
}
<div class='myDiv'></div>