This question already has answers here:
How do nested vertical margin collapses work?
                                
                                    (3个答案)
                                
                        
                                3年前关闭。
            
                    
我正在尝试设计包含“字母”和“ h1”的中心大部分“ jumpotron”。当我尝试在中央区域垂直向下推“字母”或“ h1”时,它将整个“ jumpotron”与背景图像一起移动。为什么会这样呢?为什么它不尊重“字母”是“ jumpotron”的子代的事实,因此必须将其边距移到“ jumpotron”内部?还有没有一种更好的方法可以从我使用的图像中为“ jumpotron”创建大图像背景?

这是代码:

    <body>
<header>
  <div class="container">
    <h2>Trunk Club</h2>
    <nav>
      <ul>
        <li>How it Works</li>
        <li>What to Expect</li>
        <li>Stylists</li>
        <li>Log In</li>
        <li><a id="getstarted" href="#">Get Started</a></li>
      </ul>
    </nav>
  </div>
</header>
<div id="jumpotron">
  <div id="letters">
    <h1>Premium Clothing,<br>
      Great Advice,<br>
      Zero Work</h1>
  </div>
</div>
</body>
</html>


@charset "utf-8";
/* CSS Document */
body {
    font-family: 'Source Sans Pro', sans-serif;
}

.container {
    width: 80%;
    margin: 0 auto;
}

header {
    width: 100%;
    min-width: 800px;
    height: 60px;
    background-color: #151e28;
    text-transform: uppercase;
}

header h2 {
    color: #FFFFFF;
    float: left;
    margin-top: 15px;
}

nav {
    float: right;
    margin-top: 5px
}

nav li {
    float: left;
    list-style: none;
    color: #878b85;
    margin-left: 20px;
    font-size: 12px;
    font-weight: bold;

}

nav ul li:nth-child(4) {
    border-left: 1px solid #878b85;
    padding-left: 15px;
    padding-top: 5px;
    padding-bottom: 5px;
    margin-top: -5px;
}

nav ul li:nth-child(5) {
    border: 1px solid #878b85;
    padding-right: 20px;
    padding-left: 20px;
    padding-top: 5px;
    padding-bottom: 7px;
    margin-top: -5px;
}

#getstarted {
    text-decoration: none;
    color: #878b85;
}

#getstarted:visited {
    color: #878b85;
}


#jumpotron {
    width: 100%;
    min-height: 670px;
    background-image: url(images/Stylist_Product_Exp30-v3.jpg);
    background-position: center;
    background-size: 100% auto;
}

#letters {
    margin-top: 500px;
}

#jumpotron h1 {
    color: #FFFFFF;
    text-align: center;
    font-size: 55px;
}

最佳答案

您必须将#letters的填充设置为500,而不是空白

关于html - 为什么将Div下推到页面上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36159504/

10-12 03:42