Height 100% not working for nested div

前端 未结 2 1711
鱼传尺愫
鱼传尺愫 2020-12-12 02:00

I was tried to make child div take height 100% but it\'s not working, so I\'d like to know why it is not working: I give html, body height: 100% then .hero height 100% and .

2条回答
  •  被撕碎了的回忆
    2020-12-12 02:35

    Height 100% is a very elusive issue, and normally creates more problems than it solves. However, to answer your question:

    Basically, every container between the html element and the element you want to be 100% must have height: 100%; on it.

    So, in your case, this means the following CSS must be added:

    /* These styles get all of the containers to 100% height */
    /* address ONLY sub-elements of .hero element to prevent issues with other pages / code */
    .hero .container-fluid,
    .hero .row,
    .hero [class*="col-"] {
       height: 100%;
    }
    

    Below is your code, built into a snippet, so you can see it work. Note that I've additionally added col-sm-6 classes to your col-lg-6 elements so you can see it work in a narrower window. (NOTE: click the "Expand Snippet" link in order to get a wide enough window to see it working).

    html,
    body {
      height: 100%;
    }
    
    .hero {
      width: 100%;
      height: 100%;
      border: 1px solid #0094ff;
    }
    
    .hero-image {
      width: 100%;
      height: 100%;
      background-image: url('http://via.placeholder.com/500x100');
      background-size: cover;
    }
    
    /* These styles get all of the containers to 100% height */
    .hero .container-fluid,
    .hero .row,
    .hero [class*="col-"] {
       height: 100%;
    }
    
    
    Hello

    Hey, I Am Mike Ross

    Creative Art Director from San Francisco. Husband, photographer, surfer and tech fanatic.

提交回复
热议问题