Expanding the parent container with 100% height to account for floated content

前端 未结 6 955
清酒与你
清酒与你 2020-11-30 21:58

I\'m struggling with a client project. All of my divs have no absolute positioning, height:100% for html, body, and conta

相关标签:
6条回答
  • 2020-11-30 22:36

    You have a float in static-maincontent, which removes it from the regular flow of the content of the document, and hence static-content doesn't care about its height any more, and so won't expand to cover it. Additionally, remove height:100% for static-content.

    0 讨论(0)
  • 2020-11-30 22:43

    If height: 100% doesn't work well for you, you can try this calc function from CSS3:

    /* Firefox */
    height: -moz-calc(100%);
    /* WebKit */
    height: -webkit-calc(100%);
    /* Standard */
    height: calc(100%);
    

    You can try this either with height, or with min-height, as already said. You can with this calc functions also other calculations like:

     height: -moz-calc(100% - 50px);
    

    And this is sometimes very useful, as you might guess.

    0 讨论(0)
  • 2020-11-30 22:45

    If you have to use overflow:visible for some reason, there's other way to force container to stretch to contain all floated content. You have to put element with clear:both as a last container's elements. If you ignore ancient IEs (<8) you can do it with very simple css (vide https://css-tricks.com/snippets/css/clear-fix/):

    .your-container:after {
      content: "";
      display: table;
      clear: both;
    }
    
    0 讨论(0)
  • 2020-11-30 22:49

    You used the wrong overflow-y property for clearing, and you should set a min-height instead of a regular height. Try this:

    #static-content {
        background-color: #FFFFFF;
        margin: 0 auto;
        min-height: 100%; /* Set to minimum height so overflow doesn't get hidden */
        overflow-y: hidden; /* HIDE overflow; I know, it doesn't make much sense */
        position: relative;
        width: 960px;
    }
    

    Floating Content by Itself

    Given this green box which has a padding of 20px (for visibility), notice how a single red box floated to the left will expand past the boundary of its parent box. This is because floating content doesn't actually take up any "space" in the visual area. All other elements will expand underneath it, and only text will wrap around it.

    Example 1

    Clearing Floated Content in the Parent

    In order to counter this and make the green box completely encompass the area of its child red box, we can add overflow: hidden to its styles. This will expand the box down far enough.

    Example 2

    Expanding the Parent to 100% Height

    You might think that just adding height: 100% is the simplest way to make it expand to where it needs to be.However, the height property specifies an absolute height. Since the content which is floated does not actually take up any vertical space, our overflow: hidden property will cut off all the content that goes past the parent's height.

    Example 3

    Using a Minimum Height Instead

    Since we want it to expand to at least a 100% height, we can use the min-height property to force it there and still maintain the "automatic" height needed to make the parent green box fully encompass the child red box, letting it push past the 100% only when it needs too.

    Example 4

    How You Were Set Up

    All elements, by default, are set to overflow: visible so that property didn't really change anything. The only difference you had between this and the first example I provided was that you had a height: 100% set on the element. So the parent was expanding to 100% height but still not encompassing the full height of its child red box.

    Example 5

    0 讨论(0)
  • 2020-11-30 22:50

    READ FOR ANSWER!!!-- Okay so I had the same problem, All that was needed was to remove the "Positioning" Style. Should work perfectly fine.

    0 讨论(0)
  • 2020-11-30 22:52

    height:100% is the height of the content that flows with your container at hand and is not taking into account your floated content, so that is why the height of your container is stopping short. Remove it and clear your container properly to clear your floated elements within and it will work:

    #static-content:before, #static-content:aftr {
     display:table;
     content:"";
    }
    
    #static-content:after {
     clear:both;
    }
    
    #static-content {
     zoom:1; /*ie fix*/
    }
    
    0 讨论(0)
提交回复
热议问题