Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position fixed with width 100% is ignoring body padding

Tags:

html

css

I am trying to make a footer that spans the width of a page minus 10px on the left and right. I am trying to do this by giving the body a padding on all sides of 10px. In the code below the header works just fine, but the footer is ignoring the body padding on the right side. Why is it doing that and how can I fix it?

<!DOCTYPE html>
<html>
    <head>
        <style>
            div {
                padding:0;
                margin:0;
            }
            body {
                margin: 0;
                padding: 0 10px;
            }
            #header {
                height: 150px;
                width: 100%;
                margin: 0 auto;
                background: #333;
            }
            #footer {
                position: fixed;
                bottom: 5px;
                width: 100%;
                background: #f63;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div id="header"></div>
        <div id="footer">I am the footer!</div>
    </body>
</html>
like image 312
Jgonzalez731 Avatar asked Mar 03 '14 23:03

Jgonzalez731


1 Answers

your footer not ignoring body padding, look through console at that element sizes and you will see that width of your footer is 100% of window width + 10px from left padding + 10px from right padding.

you can use calc function in css: https://developer.mozilla.org/en-US/docs/Web/CSS/calc

#footer {
  width: calc(100% - 20px);
}

JSFiddle

like image 173
Epsil0neR Avatar answered Oct 26 '22 22:10

Epsil0neR