Question

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>
Was it helpful?

Solution

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

OTHER TIPS

Footer width and padding are calculated separately. You can use use box-sizing: border-box to prevent this from happening

Use this for all elements to behave this way
* {
     box-sizing: border-box;
  }

There is a good video by Travis Neilson on his YouTube channel DevTips, where he explains the box-modal concept.

#footer {
   position: fixed;
   bottom: 5px;
   left: 10px;
   right: 10px;
   background: #f63;
   text-align: center;
}

demo: http://jsbin.com/benosofo/3/

A fixed element is not fixed in relation to the body, it's fixed in relation to the window. If it would be fixed in relation to the body then it would be just as absolute positioning, and it would scroll with the body.

You can make a fixed container for the footer, so that you can use a padding on that.

HTML:

<div id="footercontainer"><div id="footer">I am the footer!</div></div>

CSS:

#footercontainer {
    position: fixed;
    bottom: 5px;
    width: 100%;
    padding: 0 10px;
}
#footer {
    background: #f63;
    text-align: center;
}

None of the solutions in the net worked for me. so I solved it another way. I was trying to create a modal for adding address and was testing it on the mobile mode. I wanted a fixed layer with rgba(0,0,0,0.75) to cover all the window and in the center, a white form appear for the user. the form header was hiding in the top (and unscrollable) and in the bottom, was sticking to the bottom of window which was not looking good (in some cases, some element won't work when they don't have enough space from the window borders). so I solved the problem by putting a div after the form div in the bottom (to stick to the window bottom instead of my form) and made it transparent. so it worked! (I have to mention that I am writing react code)

this is my div:

<div className="modal-padding"/>

and this is my styling for this div:

  .modal-padding {
      width: 100%;
      border: 10vh solid transparent;
   }

I used one, before the form div and one after that. Be careful. I tested giving a width: 100vw and height: 10vh to the div but when it has no content, it doesn't work, seems it doesn't exist at all. so I gave a border. I hope this solve your problem too, or give you an idea for solving the issue. Good luck.

You could make a wrapper for your footer and apply the 10px padding to that instead.

#footer-wrap {
    position:fixed;
    bottom:0px;
    left:0px;
    padding:10px;
}

and then when you place your footer inside it will be correctly padded. This way is the most backwards compatible solution as it doesn't rely on css3 calc.

JSFIDDLE

http://jsfiddle.net/pk8uU/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top