html - 另一个包装 div 内的 div 上的边距顶部

标签 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>

我的问题是里面的div,如果我在上面加上margin-top,整个div会一起掉下去,怎么办只需将 div 移动到 #header 中?我的代码有什么问题吗?

最佳答案

你的代码有很多问题。

变更摘要:

  1. 移除所有边距、 float 和高度。
  2. 导航链接使用 <a href=""></a>而不是 divs并考虑拥有 ul li a结构。
  3. 然后将导航垂直放置在中间:

    • 添加到 #header容器a display: table;
    • 添加到 #headWrapper一个display: table-cell; vertical-align: middle;
  4. 要在中间对齐导航文本,请使用 text-align: center;

代码:

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

关于html - 另一个包装 div 内的 div 上的边距顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29202491/

相关文章:

css - 全局更改我网站上使用的所有纸张元素中墨水颜色的最佳方法是什么?

html - 如何在 <td> 中制作水平滚动条代码块?

javascript - 在移动设备上查看时如何将导航项移出屏幕

css - 如何修复顶部和底部边距折叠的问题?

html - 在一定时间内顺利加载进度条

html - 页眉和页脚之间 100% 且无滚动的内容

java - 我如何使用java提取span的名称?

html - CSS 边距自动

css - 避免由 float 边距引起的巨大空间

javascript - 将内联 block 元素与 td 内的链接居中