Why isn't this DIV centered? [duplicate] - html

This question already has answers here:
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
Closed 9 years ago.
I have a DIV that I want to keep centered and docked to the bottom of the page. I've pretty much achieved that except that it lists to the right. I've setup a jsFiddle to demonstrate the problem.
I suspect that I need to adjust margins but so far my attempts have been fruitless. I tried adding:
margin: 0;
but nothing. What am I missing?

If you mean the little bit of space to the left, then I would think adding left: 0; would solve the problem.
#footer {
position: fixed;
bottom: 0;
width: 100%;
border: 1px solid red;
text-align: center;
margin: 0;
left: 0;
}
You need to watch though because you are setting the width to be 100% and also setting a border which makes the div wider than 100%.

I suspect this is the default margin/padding on the body
body {
margin: 0;
}
will fix it.

You need to set left to 0 (zero) and box-sizing to border-box!
Check this: http://jsfiddle.net/pR4P5/7/
#footer {
position: fixed;
bottom: 0;
width: 100%;
border: 1px solid red;
text-align: center;
margin: 0;
box-sizing: border-box;
left:0;
}
Have a nice day,
Alberto

Remove border: 1px solid red; and add left: 0; to #footer.
Here is the JSFiddle.

Try replacing margin: 0; with left: 0;. This should lock the div onto the left of the page, centering it. I would also not use a border, that will add on some pixels to the width and height, making it less centered. I would instead use only border-top: 1px solid red;.
Hope I helped you solve your annoying problem.

Paulie_D's answer is correct you need to add margin: 0 to the body. I'd imagine you think its not properly centred because of the border on the footer.
Try adding:
box-sizing: border-box;
to your footer element.

Change the width: 100% to
left: 0;
right: 0;
So it would become: http://jsfiddle.net/pR4P5/6/
Complete CSS:
#footer {
position: fixed;
bottom: 0;
left:0;
right: 0;
border: 1px solid red;
text-align: center;
margin: 0;
}
Result would be 100% width while you can keep your border of 1px.
Note when defining width 100% and adding a border you might be better of adding box-sizing to it.
Also, margin: 0; to body is a great way to avoid headaches.
(box-sizing: content-box; - http://quirksmode.org/css/user-interface/boxsizing.html)

To Center the div:
html, body {
margin:0;
padding:0;
width:100%;
height:100%;
}
To keep the borders included in the positioning, too:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
DEMO: http://jsfiddle.net/pR4P5/8/
Read More at:
CSS Reset: http://meyerweb.com/eric/tools/css/reset/
box-sizing: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

Related

How do you set the body to the full size of the screen in html and css?

The body in my code is a little off of the left and top of the screen, I tried removing content-box and removing margin, but it isn't working. I also tried absolutely positioning it left:0; and top:0; but none of that works, here is my code;
body {
padding: none;
margin: none;
box-sizing: border-box;
position: absolute;
top: 0;
left: 0;
width:100%;
height: 100vh;
border: 2px solid red;
}
Never-mind. I fixed this by simply changing margin:none;, to margin:0;.
In CSS make height and width 100% .

fixed html divs going over the edge

I'm trying to learn basic html&css and I was making a simple webpage with a navbar, but when I fix my header it goes over right side of the page.
<div ID="header">
<table ID="navbar">
<tr>
<td>Home</td>
<td>About</td>
</tr>
</table>
</div>
#header {
background-color: yellow;
position: fixed;
height: 35px;
width: 100%;
border-radius: 10px;
border: 2px solid red}
How can I fix this? Thanks.
Your Html file is correct but change in Css file.
CSS:
#header {background-color: yellow;position: fixed;height: 35px;width: 100%;border-radius: 10px;box-sizing: border-box;top: 0;left: 0;}
Past above code in your css file.
Here you can see working Demo.☺
add below css:
#header{
box-sizing: border-box;
top: 0;
left: 0;
}
box-sizing: border-box means that the width (100%) will contain border. Otherwise width of element would be 100% + 4px - and that is wider than your browser window
You may also get sick of default margin & padding setting in browsers, so i recommend to add:
html, body{
margin: 0;
padding: 0;
}
at the beggining of css file if you haven't already done this
You need to define spacing for left and right.
here i made a fiddle: https://jsfiddle.net/yq6zoyhk/
#header {
background-color: yellow;
box-sizing: border-box;
position: fixed;
top: 0;
left: 0;
height: 35px;
width: 100%;
border-radius: 10px;
border: 2px solid red}
When you are using position:fixed you need to define the spacing from left and top, if you want it from left, add left:0; top:0; or if you want to center it then add left:0; right:0; top:0;

Stick a div to left of page

I have a container and inside I have a div I wanted to stick to the left corner of the screen, but I always have a gap
How do I obtain something like this? Thanks in advance
https://onedrive.live.com/redir?resid=22090721B1B171B7!12771&authkey=!AEDRyX0320pmcLk&v=3&ithint=photo%2cjpg
i hope to help with this example https://jsfiddle.net/step/L9rn4fkg/ you play with margin-top
.sticky {
width:100px;
height:100px;
float: left;
position: fixed;
background-color: #30cff0;
margin-top:50px;
z-index:1000;
}
The most basic way to reach this is:
html, body {
margin: 0;
padding: 0;
}
And make sure your div doesn't have any margin or padding on the left side either (for instance, when you use bootstrap's class="container" it will give you a left and right padding of 15px.
I think you should reset your body(or your elements parent of you want stick) margin.
you can use:
* { /* it's a hard method */
margin: 0;
padding: 0;
}
.fix-el {
position: fixed;
width: 100px;
height: 100px;
/* center it */
line-height: 100px;
border: 1px solid #EEE;
top: 50%;
margin-top: -50px;
text-align: center;
}
<div class="fix-el">
fix this
</div>
hope you can solve it

How to add padding to 100% div in Bootstrap?

I previously asked how to make a design akin to this, with a 100% height div and one below it with the same background but outside view:
The answer seemed pretty straightforward, just add padding to the top div, as seen here: http://jsfiddle.net/s6wnavqc/
HTML:
<div class="background">
<div class="bottom-div">
</div>
</div>
CSS:
.background {
background: url('http://www.mixflavour.com/wp-content/uploads/2014/11/Nature-Wallpaper-03.jpg');
position: relative;
height: 100%;
padding-bottom: 100px;
}
.bottom-div {
position: absolute;
bottom: 0;
background:rgba(0,0,0,0.5);
width: 100%;
height: 100px;
}
However, when I add Bootstrap the design completely breaks for unknown reasons! I mean, simply adding it does that, not even using any containers or rows. I figured this warranted its own thread, since the previous answer did correctly address my problem as stated.
Take a look at this fiddle to see the padding being completely ignored: http://jsfiddle.net/s6wnavqc/1/
Not even adding !important after works.
Does anyone know why and how to fix this? Do I have to use calc on the height instead? I'd rather not do that because it's not supported on some browsers.
Add box-sizing:content-box to .background. By default in bootstrap all elements are set to box-sizing:border-box which means padding is absorbed into height/width, not added to it.
From bootstrap:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Override:
.background {
background: url('http://www.mixflavour.com/wp-content/uploads/2014/11/Nature-Wallpaper-03.jpg');
position: relative;
height: 100%;
padding-bottom: 100px;
box-sizing: content-box;
}

Full screen width div area

What's the best approach to creating a div that will take the full width of the screen, no matter what resolution? I'm trying to add a 'top' bar and bottom 'footer' area with divs that have a black background and styled border that I'd create with a small image and repeat. For some reason my attempts are leading to small spaces on the top and sides of the div?
My markup is similar to:
<div id="top">
Top bar stuff
</div>
<div id="pagewrap">
All the page content
</div>
CSS
#top {
width: 100%;
margin: 0;
padding: 0;
background-color:#000
Usually this is the body tag having some paddings and/or margins. Try adding:
body {
padding: 0;
margin: 0;
}
If this works, you may want to consider using a normalization stylesheet that fixes this type of issue as well as many other related types of issues.
Extended answer...
The above answers the core issue folks landing here seem to have. But to expand a bit, try to directly answer the original question, and also providing some staple code that I use for things nowaydays:
Here's how I would create a full-width, but also full-height div inside a body in a cross-browser (but modern browser only) way:
document.getElementById("pagewrap").innerHTML = "All the page content<br>".repeat(100);
* {
box-sizing: border-box; /* not completely needed, yet useful */
}
html, body {
margin: 0;
padding: 0;
height: 100%;
}
body {
display: flex; /* or css grid for more intricate layouts */
flex-direction: column;
}
#top {
background-color: LightCoral;
height: 150px;
border-bottom: 3px solid Crimson;
}
#pagewrap {
background-color: LightGreen;
flex-grow: 1; /* make it stretch to the bottom even if little content */
overflow-y: scroll; /* optional */
}
<div id="top">Top bar stuff</div>
<div id="pagewrap">All the page content</div>
Just use top:0; and left: 0; and you can also eliminate padding: 0. Don't use top: 0; for other div except top, use left: 0; for other div for eliminate the left space.
#top {
width: 100%;
margin: 0;
padding: 0;
background-color:#000
top: 0;
left: 0;
}
Ensure that body has padding and margin set to 0 in CSS.