I am creating a full width footer with margin, but it seems like when I add the margin it overflows. How can i avoid this?
Fiddle
footer {
bottom: 0;
position: absolute;
margin 40px;
width: 100%;
font-size: 2em;
}
footer .left_div {
float: left;
}
footer .right_div {
float: right;
}
<footer>
<div class="left_div">This is a long test</div>
<div class="right_div">This is another text</div>
</footer>
That's because you're specifying a width of 100% so when you add the margins then it becomes 100% + 40px on each side making it 80px wider that the window.
There's a few ways to fix it, you can use calc for the width so subtract the extra margin on the sides:
footer {
bottom: 0;
position: absolute;
margin: 40px;
width: calc(100% - 80px); /* 40px on each side = 80px */
font-size: 2em;
}
Or even easier remove the width property and add left and right properties to stretch the element without directly messing with the size:
footer {
bottom: 0;
left: 0;
right: 0;
position: absolute;
margin: 40px;
font-size: 2em;
}
You're missing : in your css declaration. And yes, this is fatal.
margin 40px;
be
margin: 40px;
Your footer is positioned at a specified position relative to its closest positioned ancestor or to the containing block. As MDN says about Absolute Position. And your there's no relative closest parents to it. Which in your case, in the fiddle, width: 100% will take the width of iframe which is 700px.
But, you can have the workaround like this,
footer {
font-size: 2em;
margin: 40px;
bottom:0;
left: 0;
position: absolute;
right: 0;
}
Here's the updated fiddle
from my experience the best way is remove width: 100% and use left: 0; right: 0;
footer {
position:absolute;
left:0;
right:0;
bottom:0;
margin: 40px;
font-size: 2em;
}
footer .left_div {
float: left;
}
footer .right_div {
float: right;
}
<footer>
<div class="left_div">This is a long test</div>
<div class="right_div">This is another text</div>
</footer>
Reset default margin in body and add box-sizing:border-box to footer,
because you are applying margin and using position:absolute you need to use calc()
plus, you have a typo on margin 40px, should be margin:40px
body {
margin: 0
}
footer {
bottom: 0;
position: absolute;
margin: 40px;
width: calc(100% - 80px);
font-size: 2em;
box-sizing: border-box;
background: red
}
footer .left_div {
float: left;
}
footer .right_div {
float: right;
}
<footer>
<div class="left_div">This is a long test</div>
<div class="right_div">This is another text</div>
</footer>
add
left:0; to the footer css :)
It will overflow as margin increases the space from outside the div or block. If you want to hide the overflow then use overflow: hidden.
If you want to show all the data with margin then you should reduce width and then apply margin.
It is not worth to apply margin on div/block instead use padding. Padding increases from inside.
Related
I need a way to fill almost the entire page with the iframe. But as you can see here, there is a big white space after the iframe:
http://jsfiddle.net/mW9WF/896/
How can I remove the whitespace ?
This is my code:
<div class="header" >
</div>
<iframe class="mainBody" src="http://www.repubblica.it/tecnologia/2015/07/16/news/cellulari_e_tariffe_estive_gigabyte_extra_e_servizi_di_intrattenimento_ecco_le_offerte-119221137/"></iframe>
<div class="footer">
</div>
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%;
}
top and bottom css attributes are not ment to go together in the same rule. my suggestion is to set the iframe, html and body tags width and height to 100% and set a padding to the boddy in order to get the desired space between the viewport limits.
.mainBody, html, body {
width: 100%;
height: 100%;
}
body {
padding: 5%;
}
The white space after your iframe is due to the bottom: 20px; attribute in your .mainBody rule.
If you want the white space to be fixed no matter the viewport size, you can change the padding from px to %. Also, if you don't want the white space from all sides to be the same, remember that padding can be set to 4 values (padding: 40px 10px 20px 10px;)
I often have this problem with a lot of fixed navbars i.e. when I have a fixed navbar, how do I give the element below it some margin, so that the fixed navbar is not covering that element?
I was just wondering if there is a more elegant way of doing this apart from the <br> tag and margin-top.
The sample code would be like:
HTML code :
<nav>
I AM NAVBAR
</nav>
<br><br>
<div>
</div>
CSS code :
* {
padding: 0;
margin: 0;
}
nav {
height: 50px;
width: 100%;
background: #444;
color: #fff;
text-align: center;
font-weight: bold;
font-family: verdana;
position: fixed;
top: 0;
left: 0;
}
div {
height: 500px;
width: 100%;
background: tomato;
}
Fiddle here.
Fixed position relatives to the screen's viewport. You can just set top margin or padding on the body tag, and make the value >= the navbar height.
body {
margin-top: 50px; /*or padding*/
}
http://jsfiddle.net/5k5mxcn1/1/
There's a theory in CSS that you only apply bottom margins.
http://csswizardry.com/2012/06/single-direction-margin-declarations/
So to keep things modular, you could create a wrapping class:
<nav class="nav__wrapper">
<div class="nav__content">
Navigation
</div>
</nav>
<p>Text content</p>
css:
.nav__wrapper {
height: 30px;
margin-bottom: 10px // breathing room
}
.nav__content {
background: #dadada;
height: 30px;
line-height: 30px;
position: fixed;
width: 100%;
}
fiddle: http://jsfiddle.net/wv53qLwz/
A fixed element is position relative to the viewport, meaning it stays at the same designate spot and does not leave a gap in the page where it would normally have been located.
You can apply a top margin to the element that is directly following the fixed element.
div {
margin-top: 50px;
}
However, I've found out that using the scroll-margin property does the trick. It's explained better here https://css-tricks.com/almanac/properties/s/scroll-margin/#aa-enter-scroll-margin
div {
scroll-margin-top: 50px;
}
I'm trying to make a fixed sidebar, that will stay on that position when you scroll the page(as in the 2nd image), but the page content is going under that sidebar, not on its right (first image, you can see the blue part on the sidebar, that's the div going under the bar.)
Img1:
img2:
(I'm using images links because i can't post them)
html:
<div class='barralado'>
~sidebar content~
</div>
<div class='conteudo'>
<div class='inicio'>
<div class='topo'>
<p class='titulo'>Liga Juizforana</p>
</div>
</div>
</div>
css:
.barralado {
position: fixed;
top: 0;
left: 0;
}
.conteudo {
}
.conteudo .topo {
background-color: #ffffff;
}
.topo .titulo {
font-size: 4em;
color: white;
text-shadow: 2px 2px 1px rgba(150, 150, 150, 1);
margin-top: 3em;
margin-left: 3.5em;
}
I tried to put both to float left, i tried to clear, i tried all the positions on both, and it didn't worked
2nd mini question: how to make the "conteudo" div 100vh after put it on the right of the sidebar? When i try it, it doesn't change to 100vh.
A fixed positioned div will be fixed on the page, even if you scroll it. And all the stuff goes under it (it has z-index priority).
To fix it, give your content div left padding equal to the width of your sidebar.
.barralado {
position: fixed;
top: 0;
left: 0;
min-height: 300px; // I suggest you give a value to your sidebar.
width:300px; //This is your width;
}
.contneudo{
padding-left:300px; //This is the padding that will go around your sidebar
}
If your sidebar has a fixed width you could using padding to the left of your content container of the sidebar width. Or you could float it to the right and set the width with the CSS calc function.
.conteudo {
width: -webkit-calc(100% - 200px);
width: calc(100% - 200px);
float:right;
}
Using your code and padding option:
.barralado {
position: fixed;
top: 0;
left: 0;
width: 300px;
}
.conteudo {
padding-left: 300px;
}
.conteudo .topo {
background-color: #ffffff;
}
.topo .titulo {
font-size: 4em;
color: white;
text-shadow: 2px 2px 1px rgba(150, 150, 150, 1);
margin-top: 3em;
margin-left: 3.5em;
}
There are a few other ways but hopefully one of the 2 options will help you achieve what you want.
I find it easier to set a grid for your body content, with an empty spacer div, and then your main content within the div.
HTML File:
<div class="gridWrapper">
<div class="spacer"> </div>
<div class="yourMainContent"> Your content here</div>
</div>
CSS File:
.gridWrapper {
display: grid;
grid-template-columns: 250px 1fr;
}
In this scenario of fixed widths, you can make your content div absolute positioned, to avoid going under your sidebar:
.conteudo {
position: absolute;
top: 0;
left: 192px; // this is your sidebar width, according to your screenshot
}
or if for some reason you need to avoid absolute positioning, you can offset it with margin:
.conteudo {
margin-left: 192px; // this is your sidebar width, according to your screenshot
}
I have the following html:
<body>
<h1>Something</h1>
<img id="myid" src='images/bigimage.png'/>
<div id="container">
<div id="fast-back">
<p class="big-font">SOMETHING</p>
<p class="small-font">SOMEThiNG ELSE</p>
</div>
</div>
</body>
And the CCS for it is:
html {
overflow-x: hidden;
}
body {
margin: 0;
padding: 0;
background: url(images/body-background.png) top no-repeat;
min-height: 860px;
height: 860px;
}
h1 {
margin: 0;
padding: 0;
position: absolute;
color: white;
visibility: hidden;
}
#container {
margin: 0 auto;
padding: 0;
position: relative;
min-width: 1336px;
height: 860px;
width: 1336px;
}
#myid{
position: absolute;
left: 50%;
right: 50%;
margin-left: -1280px;
margin-right: -1280px;
z-index: 1004;
}
#fast-back {
position: relative;
margin-left: 15%; /*it moves even using pixel*/
top: 272px;
z-index: 99999;
text-align: center;
width: 126px;
}
However, when I resize the browser window, the "fast-back" div moves to the right.
How can I prevent this behaviour?
Thanks!
Looking at #fastback CSS rule, you are using percentage instead of pixels on margin-left. Change it to pixels as unit of measure.
If you are using percentage as unit of measure, the left margin of the element, in your case, will move in relation to the viewport.
And if you are using pixels, on the other hand, the margin stays on the same location, even if the browser is resized.
Update
The solution is remove the width of the #container. See the following link.
http://jsfiddle.net/jlratwil/LB8rf/1/
The reason why the first solution does not work because the width of the container is set to 1336 pixels and centered aligned via margin: 0 auto. If the browser viewport width reaches beyond 1336 pixels during resize, the #fastback element will move.
I try to center a div element ( the footer div in this case ) in my webpage but it insists on staying on the left side.
I am not quite sure what is wrong... Any ideas?
Thank you in advance.
HTML :
<div id='main'>
</div>
<div id='footer'>Centered Text</div>
CSS :
* {
padding: 0;
margin: 0;
font-size: 12px;
}
body {
font-family: helvetica, serif;
font-size: 12px;
overflow-y:scroll;
}
#main {
border: 1px solid #bbbbbb;
margin: 3% 5%;
padding: 10px 10px;
}
#footer {
font-size: 75%;
margin: 0px auto;
position: absolute;
bottom: 0px;
}
http://jsfiddle.net/DjPjj/2/
http://jsfiddle.net/DjPjj/13/
Try this:
#footer {
font-size: 75%;
width: 100%;
position: absolute;
bottom: 0px;
text-align: center;
}
Because your footer is absolutely positioned, you must tell it what width to take relative to its parent container. You can then use text-align to center the text within it.
Here is another example: http://jsfiddle.net/DjPjj/17/
This one centers a box within the absolutely positioned element. The inner box can be centered using margin: 0 auto because it is not absolutely positioned.
#footer {
font-size: 75%;
width: 100%;
position: absolute;
bottom: 0px;
}
#footerInner {
margin: 0 auto;
width: 300px;
background-color: #ddd;
text-align: center;
}
This is more flexible because the inner element gives you a new container to work with that is centered relative to the parent.
The reason it won't center is because of the positon: absolute;.
Keep in mind this means that the footer will always be at the bottom of the page, even if the content overflows past it. It will overlap. If you want to have it be attached to the bottom of the page, you must set the min-height of a container above it to 100% and then deal with a negative margin-top and remove the position: abosolute;
http://jsfiddle.net/4fuk7/1/
Notice how the centered text is overwritten.
If you are looking for something to always be at the bottom, this would work
http://jsfiddle.net/4fuk7/3/
Sorry, the last one would scroll to the top. This one doesn't, but you'd need to fiddle with it a bit to get it to properly align around the margin's you've set. http://jsfiddle.net/4fuk7/9/
http://www.tlunter.com/Layout 2/ is where I did something similar. You can reference that if you want.