如何使 div 与我的导航栏对齐下方?

3

我有一个包含导航栏和4个div的布局。第一个div被导航栏遮挡了。有谁知道我该怎么做吗?

<body>
  <div id="app" class="flex-container">
    <nav class="navbar default-layout p-0 fixed-top d-flex flex-row" style="height: 63px;">
      <select id="region" onchange="initProcess();" style="position:absolute;background:#eaeaea;">
      </select>
      <div style="width: calc(100% - 74px); padding-left: 74px; left: 0; text-align: center; color: #fff">
        <img src="images/co-logo.png" style="width: 100px; margin-top: -6px">
        <h4>Dashboards</h4>
      </div>
      <div class="navbar-menu-wrapper d-flex align-items-right">
        <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-right" type="button" data-toggle="offcanvas">
                    <span class="mdi mdi-menu"></span>
                </button>
      </div>
    </nav>
    <div class="flex-item" style="background-color:red"></div>
    <div class="flex-item" style="background-color:blue"></div>
    <div class="flex-item" style="background-color:yellow"></div>
    <div class="flex-item" style="background-color:grey"></div>
  </div>
</body>

enter image description here

3个回答

3

您可以轻松地添加padding-top CSS属性,使其等于导航栏的高度。 在您的情况下,它将是:

.flex-container {
     padding-top: 63px;
}

1
这个CSS在你的情况下可以使用:

body {
    margin: 0;
    padding: 0;
}
.flex-container {
    position: relative;
    padding-top: 100px;
}
.navbar {
    position: fixed;
    top: 0;
    background: purple;
    z-index: 1;
    width: 100%;
}
.flex-item {
    width: 100%;
    height: 60px;
}
.flex-container .flex-item:first-of-type {
    position: absolute;
    top: 50px;
    width: 100%;
    height: 50px;
}

1
由于导航栏设置为position:fixed,它会忽略元素的正常流。因此,其他元素会在其下方对齐。在这种情况下,只需将margin-top: 63px;添加到第一个元素即可(63px是您的导航栏的确切高度)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接