【问题标题】:Space between the divs? [duplicate]div之间的空间? [复制]
【发布时间】:2023-03-27 01:49:01
【问题描述】:

我想问一下我的 html 中 div 之间的空间。 .container 和 .main 之间总是有空格。谁能解释为什么? 例如:我想将 h1 文本“MOVE”移动到页面顶部(没有空格)。

非常感谢,

代码如下: HTML

html, body {
            margin: 0;
          }
    
          h1, h2, a {
            font-family: 'Oswald', sans-serif;
            text-transform: uppercase;
          }
    
          p {
            font-family: Helvetica, Arial, sans-serif; 
          }
    
          .container {
            max-width: 940px;
            margin: 0 auto;
          }
         h1 {margin: 0;}
          /* Main */
          .main {
            text-align: center;
            background: url(https://s3.amazonaws.com/codecademy-content/projects/move/bg.jpg) no-repeat center center;
            background-size: cover;
            height: 600px;
          }
    
          .main .container {
            position: relative;
            top: 100px;
          }
    
          .main h1 {
            color: #fff;
            margin: 0;
            font-size: 150px;
          }
    
          .main p {
            color: #fff;
            margin: 0 0 20px 0;
            font-size: 18px;
          }
    
          .main .btn {
            background-color: #1c1c1c;
            color: #fff;
            font-size: 18px;
            padding: 8px 30px;
            text-decoration: none;
            display: inline-block;
          }
    
          /* Supporting */
          .supporting {
            background-color: #1c1c1c;
            text-align: center;
            padding: 50px 0 80px;
          }
    
          .supporting .col {
            float: left;
            width: 28%;
            padding: 10px;
          }
    
          .supporting h2 {
            color: #ffa800;
            font-size: 20px;
            margin-bottom: 10px;
          }
    
          .clearfix {
            clear: both;
          }
    
          .supporting p {
            color: #efefef;
            margin-bottom: 20px;
            line-height: 20px;
            font-size: 12px;
          }
    
          .supporting .btn {
            background-color: #eee;
            color: #1c1c1c;
            font-size: 18px;
            padding: 8px 30px;
            text-decoration: none;
            display: inline-block;
          }
    
          /* Feature */
          .feature {
            background: url(https://s3.amazonaws.com/codecademy-content/projects/move/feature.jpg) no-repeat center center;
            background-size: cover;
            height: 600px;
            text-align: center;
          }
    
          .feature .container {
            position: relative;
            top: 200px;
          }
    
          .feature h2 {
            color: #fff;
            font-size: 40px;
            margin:0;
            padding:50px 0 0;
    
          }
    
          /* Footer */
          .footer {
            background: url(https://s3.amazonaws.com/codecademy-    content/projects/move/footer.jpg) no-repeat center center;
            background-size: cover;
            height: 600px;
            text-align: center;
          }
    
          .footer .container {
            position: relative;
            top: 200px;
          }
    
          .footer h2 {
            color: #fff;
            font-size: 40px;
            margin: 0 0 20px 0;
            padding:50px 0 0;
          }
    
          .footer p {
            color: #fff;
            margin: 0 0 20px 0;
            font-size: 18px;
          }
    
          .footer .btn {
            background-color: #1c1c1c;
            color: #fff;
            font-size: 18px;
            padding: 8px 30px;
            text-decoration: none;
            display: inline-block;
          }
    
          a.btn:hover {
            background:#ffa800;
            color:#000;
          }
    
          @media (min-width:600px) {
            .main h1 {
              font-size: 200px;
            }
    
            .supporting .col {
              width: 30%;
            }
    
            .supporting h2 {
              font-size: 40px;
            }
    
            .supporting p {
              font-size: 14px;
            }
    
            .feature h2 {
              font-size: 60px;
            }
          }
<div class="container">
            <h1>Move</h1>
            <p>Form healthy habits to take your fitness to the next level.</p>
            <a class="btn" href="#">Start Now</a>
          </div>
        </div>
    
        <div class="supporting">
          <div class="container">
            <div class="col">
              <h2>Move</h2>
              <p>Become more active by tracking your runs, rides, and walks.</p>
            </div>
            <div class="col">
              <h2>Sync</h2>
              <p>Access your activity on your phone, tablet, or computer.</p>
            </div>
            <div class="col">
              <h2>Compete</h2>
              <p>Set personal challenges and see how you rank against your friends.</p>
            </div>
            <div class="clearfix"></div>
          </div>
        </div>
    
        <div class="feature">
          <div class="container">
            <h2>Move. Rest. Recover. Move.</h2>
          </div>
        </div>
    
        <div class="supporting">
          <div class="container">
            <h2>Go Premium</h2>
            <p>Receive recommendations based on your activity to level up.</p>
            <a class="btn" href="#">Learn More</a>
          </div>
        </div>
    
        <div class="footer">
          <div class="container">
            <h2>Stop scrolling. Start moving</h2>
            <a class="btn" href="#">Start Now</a>
          </div>
        </div>

【问题讨论】:

  • 当您在 chrome 或任何浏览器中加载页面时,打开其开发者工具,在Elements 标签下选择您的元素。在右侧,您应该能够检查应用于该特定元素的所有 CSS 规则,这将帮助您找到造成差距的原因,您可以通过在样式表上添加所需的 CSS 规则来纠正。

标签: html css


【解决方案1】:

您的H1 标签上可能有一些上边距,使您的.container div 被推离您的.main div。

您可以通过简单地从 H1 标记中删除上边距来缓解这种情况。

h1 {
    margin-top:0;
}

【讨论】:

  • 非常感谢,我会试试那个设置!
【解决方案2】:

这可能是浏览器默认启动,您应该考虑使用 HTML 重置样式表。阅读Which HTML5 reset CSS do you use and why? 获取一些示例。

【讨论】:

  • 我认为这是解决方案。感谢您的帮助!
猜你喜欢
  • 2023-04-01
  • 2012-04-20
  • 2011-04-26
  • 2012-11-18
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多