高度100%不适用于嵌套div [英] Height 100% not working for nested div

查看:121
本文介绍了高度100%不适用于嵌套div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让儿童div高度100%,但它不起作用,所以我想知道它为什么不起作用:
我给html,身高:100%然后.hero身高100 %和.hero-image必须为100%:

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 .hero-image must be 100%:

html, body{
    height:100%;
}
.hero{
    width:100%;
    height:100%;
    border:1px solid #0094ff;
    .hero-image{
        width:100%;
        height:100%;
        background-image:url('../images/1.jpg');
        background-size:cover;
    }
}

<section class="hero">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-6">
                <div class="hero-image">
                    Hello
                </div>
            </div>
            <div class="col-lg-6">
                <div class="hero-content">
                    <h1>Hey, I Am Mike Ross</h1>
                    <p>
                        Creative Art Director from San Francisco. Husband, photographer, surfer and tech fanatic.
                    </p>
                </div>
                <div class="skills">

                </div>
            </div>
        </div>
    </div>
</section>

推荐答案

高度100%是一个非常难以捉摸的问题,通常会产生比解决的问题更多的问题。但是,要回答你的问题:

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

基本上, html 元素之间的每个容器并且你想要100%的元素必须有 height:100%; 就可以了。

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

所以,在你的情况,这意味着必须添加以下CSS:

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%;
}

以下是您的代码,内置于代码段中,因此您可以看到它的工作原理。请注意,我还在 col-lg-6 元素中添加了 col-sm-6 类,以便您可以看它在狭窄的窗口工作。 (注意:点击展开代码段链接以获得足够宽的窗口以查看其是否正常工作)。

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%;
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<section class="hero">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-6 col-sm-6">
        <div class="hero-image">
          Hello
        </div>
      </div>
      <div class="col-lg-6 col-sm-6">
        <div class="hero-content">
          <h1>Hey, I Am Mike Ross</h1>
          <p>
            Creative Art Director from San Francisco. Husband, photographer, surfer and tech fanatic.
          </p>
        </div>
        <div class="skills">

        </div>
      </div>
    </div>
  </div>
</section>

这篇关于高度100%不适用于嵌套div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆