html - 无法向下移动 div

标签 html css

我确定这很简单,但我今天已经看了一段时间,就是想不通。

我想要做的就是将顶部 div“#template-nav”向下移动一点,以便在其上方有一些空白。我试过 margin-top 和 padding 但它就是不会向下移动。

我有下面的 jsfiddle 和代码,非常感谢能找出我在这里出错的地方。

https://jsfiddle.net/rufusbear/Lgx7eksa/

<div id="template">
  <div class="template-row">
    <div id="template-nav"></div>
  </div>
  <div class="template-row">
    <div id="template-jumbo"></div>
  </div>
  <div class="template-row">
    <div class="template-col-1"></div>
    <div class="template-col-2"></div>
  </div>
  <div class="clearfix"></div>
  <div class="template-row">
    <div id="template-footer"></div>
  </div>


</div>

#template {
  background: #fff;
  width: 400px;
  height: 400px;
  margin-top: 10%;
}

.template-row {
  max-width: 380px;
  margin: 0 auto;
}

#template-nav {
  height: 40px;
  background: #8dcdc1;
  padding-top: 10px;
}

#template-jumbo {
  height: 150px;
  margin: 2% 0%;
  background: #d3e397;
}

.template-col-1 {
  height: 130px;
  width: 48%;
  float: left;
  margin-right: 2%;
  background: #fff5c3;
}

.template-col-2 {
  height: 130px;
  width: 48%;
  float: left;
  margin-left: 2%;
  background: #fff5c3;
}

#template-footer {
  height: 40px;
  margin: 2% 0%;
  background: #d3e397;
}

.clearfix:after {
  content: '.';
  clear: both;
  display: block;
  height: 0;
  visibility: hidden;
}

谢谢大家

最佳答案

#template 中向顶部元素添加 margin-top 是行不通的,因为该元素的边距及其顶部子元素会相互影响。 但是你可以添加一个padding-top#template:

https://jsfiddle.net/wh6cgLt0/

关于html - 无法向下移动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428190/

相关文章:

php - 如何阻止特定文件夹向访客查看其内容?

javascript - 在隐藏/显示地址栏上禁用 iphone 缩放/调整大小?

javascript - iOS - html5 - 响应缓慢

javascript - 试图在井字游戏中设置一个 Action ,但是当我回调该函数时我一直未定义

html - 使用 flexbox 在一行上显示标签并在一行上输入

html - 使用 HTML 标记同级样式嵌套文本

html - CSS:在子元素中显示有序列表项索引

javascript - 如何检查哪个资源导致网页加载缓慢

html - 为三 Angular 形 div 创建阴影

javascript - 如何在 React 中设置带有图像的模态弹出窗口