CSS Div width percentage and padding without breaking layout

前端 未结 3 510
忘掉有多难
忘掉有多难 2020-12-07 15:54

There may be a simple fix for this, but it has troubled me for ages now...

Let me explain the situation. I have a div with the ID \'container\' that holds all the

相关标签:
3条回答
  • 2020-12-07 16:19

    If you want the #header to be the same width as your container, with 10px of padding, you can leave out its width declaration. That will cause it to implicitly take up its entire parent's width (since a div is by default a block level element).

    Then, since you haven't defined a width on it, the 10px of padding will be properly applied inside the element, rather than adding to its width:

    #container {
        position: relative;
        width: 80%;
    }
    
    #header {
        position: relative;
        height: 50px;
        padding: 10px;
    }
    

    You can see it in action here.

    The key when using percentage widths and pixel padding/margins is not to define them on the same element (if you want to accurately control the size). Apply the percentage width to the parent and then the pixel padding/margin to a display: block child with no width set.


    Update

    Another option for dealing with this is to use the box-sizing CSS rule:

    #container { 
        -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
        -moz-box-sizing: border-box;    /* Firefox, other Gecko */
        box-sizing: border-box;         /* Opera/IE 8+ */
    
        /* Since this element now uses border-box sizing, the 10px of horizontal
           padding will be drawn inside the 80% width */
        width: 80%;
        padding: 0 10px;
    }
    

    Here's a post talking about how box-sizing works.

    0 讨论(0)
  • 2020-12-07 16:26

    Try removing the position from header and add overflow to container:

    #container {
        position:relative;
        width:80%;
        height:auto;
        overflow:auto;
    }
    #header {
        width:80%;
        height:50px;
        padding:10px;
    }
    
    0 讨论(0)
  • 2020-12-07 16:28

    You can also use the CSS calc() function to subtract the width of your padding from the percentage of your container's width.

    An example:

    width: calc((100%) - (32px))
    

    Just be sure to make the subtracted width equal to the total padding, not just one half. If you pad both sides of the inner div with 16px, then you should subtract 32px from the final width, assuming that the example below is what you want to achieve.

    .outer {
      width: 200px;
      height: 120px;
      background-color: black;
    }
    
    .inner {
      height: 40px;
      top: 30px;
      position: relative;
      padding: 16px;
      background-color: teal;
    }
    
    #inner-1 {
      width: 100%;
    }
    
    #inner-2 {
      width: calc((100%) - (32px));
    }
    <div class="outer" id="outer-1">
      <div class="inner" id="inner-1"> width of 100% </div>
    </div>
    
    <br>
    <br>
    
    <div class="outer" id="outer-2">
      <div class="inner" id="inner-2"> width of 100% - 16px </div>
    </div>

    0 讨论(0)
提交回复
热议问题