Div width 100% minus fixed amount of pixels

后端 未结 10 2038
猫巷女王i
猫巷女王i 2020-11-28 17:57

How can I achieve the following structure without using tables or JavaScript? The white borders represent edges of divs and aren\'t relevant to the question.

相关标签:
10条回答
  • 2020-11-28 18:11

    While Guffa's answer works in many situations, in some cases you may not want the left and/or right pieces of padding to be the parent of the center div. In these cases, you can use a block formatting context on the center and float the padding divs left and right. Here's the code

    The HTML:

    <div class="container">
        <div class="left"></div>
        <div class="right"></div>
        <div class="center"></div>
    </div>
    

    The CSS:

    .container {
        width: 100px;
        height: 20px;
    }
    
    .left, .right {
        width: 20px;
        height: 100%;
        float: left;
        background: black;   
    }
    
    .right {
        float: right;
    }
    
    .center {
        overflow: auto;
        height: 100%;
        background: blue;
    }
    

    I feel that this element hierarchy is more natural when compared to nested nested divs, and better represents what's on the page. Because of this, borders, padding, and margin can be applied normally to all elements (ie: this 'naturality' goes beyond style and has ramifications).

    Note that this only works on divs and other elements that share its 'fill 100% of the width by default' property. Inputs, tables, and possibly others will require you to wrap them in a container div and add a little more css to restore this quality. If you're unlucky enough to be in that situation, contact me and I'll dig up the css.

    jsfiddle here: jsfiddle.net/RgdeQ

    Enjoy!

    0 讨论(0)
  • 2020-11-28 18:12

    You can make use of Flexbox layout. You need to set flex: 1 on the element that needs to have dynamic width or height for flex-direction: row and column respectively.

    Dynamic width:

    HTML

    <div class="container">
      <div class="fixed-width">
        1
      </div>
      <div class="flexible-width">
        2
      </div>
      <div class="fixed-width">
        3
      </div>
    </div>
    

    CSS

    .container {
      display: flex;
    }
    .fixed-width {
      width: 200px; /* Fixed width or flex-basis: 200px */
    }
    .flexible-width {
      flex: 1; /* Stretch to occupy remaining width i.e. flex-grow: 1 and flex-shrink: 1*/
    }
    

    Output:

    .container {
      display: flex;
      width: 100%;
      color: #fff;
      font-family: Roboto;
    }
    .fixed-width {
      background: #9BCB3C;
      width: 200px; /* Fixed width */
      text-align: center;
    }
    .flexible-width {
      background: #88BEF5;
      flex: 1; /* Stretch to occupy remaining width */
      text-align: center;
    }
    <div class="container">
      <div class="fixed-width">
        1
      </div>
      <div class="flexible-width">
        2
      </div>
      <div class="fixed-width">
        3
      </div>
    
    </div>


    Dynamic height:

    HTML

    <div class="container">
      <div class="fixed-height">
        1
      </div>
      <div class="flexible-height">
        2
      </div>
      <div class="fixed-height">
        3
      </div>
    </div>
    

    CSS

    .container {
      display: flex;
    }
    .fixed-height {
      height: 200px; /* Fixed height or flex-basis: 200px */
    }
    .flexible-height {
      flex: 1; /* Stretch to occupy remaining height i.e. flex-grow: 1 and flex-shrink: 1*/
    }
    

    Output:

    .container {
      display: flex;
      flex-direction: column;
      height: 100vh;
      color: #fff;
      font-family: Roboto;
    }
    .fixed-height {
      background: #9BCB3C;
      height: 50px; /* Fixed height or flex-basis: 100px */
      text-align: center;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    .flexible-height {
      background: #88BEF5;
      flex: 1; /* Stretch to occupy remaining width */
      text-align: center;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    <div class="container">
      <div class="fixed-height">
        1
      </div>
      <div class="flexible-height">
        2
      </div>
      <div class="fixed-height">
        3
      </div>
    
    </div>

    0 讨论(0)
  • 2020-11-28 18:14

    You can use nested elements and padding to get a left and right edge on the toolbar. The default width of a div element is auto, which means that it uses the available width. You can then add padding to the element and it still keeps within the available width.

    Here is an example that you can use for putting images as left and right rounded corners, and a center image that repeats between them.

    The HTML:

    <div class="Header">
       <div>
          <div>This is the dynamic center area</div>
       </div>
    </div>
    

    The CSS:

    .Header {
       background: url(left.gif) no-repeat;
       padding-left: 30px;
    }
    .Header div {
       background: url(right.gif) top right no-repeat;
       padding-right: 30px;
    }
    .Header div div {
       background: url(center.gif) repeat-x;
       padding: 0;
       height: 30px;
    }
    
    0 讨论(0)
  • 2020-11-28 18:15

    We can achieve this using flex-box very easily.

    If we have three elements like Header, MiddleContainer and Footer. And we want to give some fixed height to Header and Footer. then we can write like this:

    For React/RN(defaults are 'display' as flex and 'flexDirection' as column), in web css we'll have to specify the body container or container containing these as display: 'flex', flex-direction: 'column' like below:

        container-containing-these-elements: {
         display: flex,
         flex-direction: column
        }
        header: {
         height: 40,
        },
        middle-container: {
         flex: 1, // this will take the rest of the space available.
        },
        footer: {
         height: 100,
        }
    
    0 讨论(0)
  • 2020-11-28 18:20

    what if your wrapping div was 100% and you used padding for a pixel amount, then if the padding # needs to be dynamic, you can easily use jQuery to modify your padding amount when your events fire.

    0 讨论(0)
  • 2020-11-28 18:22

    In some contexts, you can leverage margin settings to effectively specify "100% width minus N pixels". See the accepted answer to this question.

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