Div height 100% and expands to fit content

后端 未结 15 805
攒了一身酷
攒了一身酷 2020-11-29 15:51

I have a div element on my page with its height set to 100%. The height of the body is also set to 100%. The inner div has a background and all that and is different from t

相关标签:
15条回答
  • 2020-11-29 16:32

    Usually this problem arises when the Child elements of a Parent Div are floated. Here is the Latest Solution of the problem:

    In your CSS file write the following class called .clearfix along with the pseudo selector :after

    .clearfix:after {
    content: "";
    display: table;
    clear: both;
    }
    

    Then, in your HTML, add the .clearfix class to your parent Div. For example:

    <div class="clearfix">
        <div></div>
        <div></div>
    </div>
    

    It should work always. You can call the class name as .group instead of .clearfix , as it will make the code more semantic. Note that, it is Not necessary to add the dot or even a space in the value of Content between the double quotation "". Also, overflow: auto; might solve the problem but it causes other problems like showing the scroll-bar and is not recommended.

    Source: Blog of Lisa Catalano and Chris Coyier

    0 讨论(0)
  • 2020-11-29 16:32

    You can also use

     display: inline-block;
    
    

    mine worked with this

    0 讨论(0)
  • 2020-11-29 16:33

    Ok, I tried something like this:

    body (normal)

    #MainDiv { 
      /* where all the content goes */
      display: table;
      overflow-y: auto;
    }
    

    It's not the exact way to write it, but if you make the main div display as a table, it expands and then I implemented scroll bars.

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