CSS: auto height on containing div, 100% height on background div inside containing div

前端 未结 9 930
忘了有多久
忘了有多久 2020-12-22 20:10

The problem, is that I have a content div which stretches its container height-wise (container and content div have auto height).

I want the background container,

相关标签:
9条回答
  • 2020-12-22 20:59

    Make #container to display:inline-block

    #container {
      height: auto;
      width: 100%;
      display: inline-block;
    }
    
    #content {
      height: auto;
      width: 500px;
      margin-left: auto;
      margin-right: auto;
    }
    
    #backgroundContainer {
      height: 200px; /*200px is example, change to what you want*/
      width: 100%;
    }
    

    Also see: W3Schools

    0 讨论(0)
  • 2020-12-22 21:00

    Somewhere you will need to set a fixed height, instead of using auto everywhere. You will find that if you set a fixed height on your content and/or container, then using auto for things inside it will work.

    Also, your boxes will still expand height-wise with more content in, even though you have set a height for it - so don't worry about that :)

    #container {
      height:500px;
      min-height:500px;
    }
    
    0 讨论(0)
  • 2020-12-22 21:01

    Okay so someone is probably going to slap me for this answer, but I use jQuery to solve all my irritating problems and it turns out that I just used something today to fix a similar issue. Assuming you use jquery:

    $("#content").sibling("#backgroundContainer").css("height",$("#content").outerHeight());
    

    this is untested but I think you can see the concept here. Basically after it is loaded, you can get the height (outerHeight includes padding + borders, innerHeight for the content only). Hope that helps.

    Here is how you bind it to the window resize event:

    $(window).resize(function() {
      $("#content").sibling("#backgroundContainer").css("height",$("#content").outerHeight());
    });
    
    0 讨论(0)
提交回复
热议问题