在另一个包装div内的div上的保证金顶部

时间:2015-03-23 02:35:44

标签: html css margin

我刚接触前端编码,我在这里遇到了margin的一些问题。 我有#header div作为基础,其他div位于其中。



body {
  margin: 0;
  padding: 0;
}

#header {
  max-width: 100%;
  height: 135px;
  background-color: #ebebeb;
}

#headWrapper {
  max-width: 1200px;
  height: 85px;
  margin: auto;
  float: left;
}

.logo {
  background-image: url("img/logo.png");
  float: left;
  width: 350px;
  height: 78px;
  margin-top: 20px;
}

#naviWrapper {
  float: left;
  max-width: 530px;
  height: 40px;
  margin-left: 275px;
  font-family: 'Open Sans', sans-serif;
  margin-top: 50px;
}

.homeBTN,
.aboutBTN,
.productBTN,
.solutionBTN,
.contactBTN {
  float: left;
  margin: 5 20;
}

<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>

</head>

<body>

  <div id="header">
    <div id="headWrapper">
      <div class="logo"></div>
      <div id="naviWrapper">
        <div class="homeBTN">Home</div>
        <div class="aboutBTN">About us</div>
        <div class="productBTN">Products</div>
        <div class="solutionBTN">Solutions</div>
        <div class="contactBTN">Contact us</div>
      </div>
    </div>
  </div>

</body>

</html>
&#13;
&#13;
&#13;

我的问题是内部div如果我在其上添加margin-top,整个div会一起降低,我怎样才能将div移到#header内{1}}?我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

您的代码存在许多问题。

更改摘要

  1. 移除所有边距,浮标和高度。
  2. 对于导航链接,请使用<a href=""></a>代替divs,并考虑使用ul li a结构。
  3. 然后将导航垂直放在中间位置:

    • 添加到#header容器display: table;
    • 添加到#headWrapper一个display: table-cell; vertical-align: middle;
  4. 要在中间使用text-align: center;

  5. 对齐导航文字

    代码:

    &#13;
    &#13;
    body {
      margin: 0;
      padding: 0;
    }
    #header {
      display: table;
      max-width: 100%;
      height: 135px;
      background-color: #ebebeb;
      width: 100%;
    }
    #headWrapper {
      display: table-cell;
      vertical-align: middle;
      width: 100%;
      height: 40px;
      font-family: 'Open Sans', sans-serif;
      text-align: center;
    }
    #naviWrapper a {
      text-decoration: none;
      padding: 10px;
    }
    #naviWrapper ul li {
      list-style: none;
      display: inline-block;
    }
    .logo {
      background-image: url("img/logo.png");
      float: left;
    }
    &#13;
    <html>
    
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    </head>
    
    <body>
      <div id="header">
        <div id="headWrapper">
          <div class="logo"></div>
          <nav id="naviWrapper">
            <ul>
              <li><a href="#">Home</a>
              </li>
              <li><a href="#">About us</a>
              </li>
              <li><a href="#">Products</a>
              </li>
              <li><a href="#">Solutions</a>
              </li>
              <li><a href="#">Contact</a>
              </li>
            </ul>
          </nav>
        </div>
      </div>
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;