【问题标题】:How to attach a div to the top of the page through CSS? [duplicate]如何通过 CSS 将 div 附加到页面顶部? [复制]
【发布时间】:2018-11-29 02:17:33
【问题描述】:

我正在模仿这个网页作为练习:

https://getbootstrap.com/docs/3.3/getting-started/

我很难将带您到 Bootstrap 4 的蓝色 div 附加到页面顶部。我想确保 div 之间没有空格,就像提供的网站一样。

这是我目前的代码:

  body {
    background-color: white;
  }

  #top {
    background-color: #386CD5;
    text-align: center;
    color: white;
    text-decoration: bold;
    padding: 16px;
    border-radius:0px;
  }

  #special1:visited {
    color:#386CD5;
  }

  #top:hover {
    background-color: #2C62BF;
  }
<a id="special1" href="https://getbootstrap.com">
 <div id="top">
  <strong>Bootstrap 4 is here!</strong>
 </div>
</a>

谢谢。

【问题讨论】:

    标签: html css


    【解决方案1】:

    我从您的问题中了解到,您不希望 div 周围有白色。您可以通过将 body 的边距设置为零来轻松解决此问题。

    看起来像这样:

      body {
        background-color: white;
      margin: 0;
      }
    
      #top {
        background-color: #386CD5;
        text-align: center;
        color: white;
        text-decoration: bold;
        padding: 16px;
        border-radius:0px;
      }
    
      #special1:visited {
        color:#386CD5;
      }
    
      #top:hover {
        background-color: #2C62BF;
      }
    <a id="special1" href="https://getbootstrap.com">
     <div id="top">
      <strong>Bootstrap 4 is here!</strong>
     </div>
    </a>

    【讨论】:

      【解决方案2】:

      您可以在正文中添加margin: 0;。如果您没有明确包含此规则,许多浏览器会默认添加自己的边距。

      【讨论】:

        猜你喜欢
        • 2011-10-10
        • 1970-01-01
        • 1970-01-01
        • 2014-01-31
        • 2012-04-11
        • 1970-01-01
        • 1970-01-01
        • 2011-01-09
        • 1970-01-01
        相关资源
        最近更新 更多