html - 我无法在页面顶部设置导航栏

标签 html css flexbox

<分区>

现在我正在制作一个单页应用程序,我希望导航栏位于页面顶部,用于显示 Google map 的下一个 div“ map ”将适合所有页面。

当我将容器设置为 display: flex 时, map 出现但导航栏被包裹在左侧。

当我将容器显示设置为 flex 时的页面:

page when I set container display to flex

未将容器显示设置为 flex 的页面:

page WITHOUT setting container display to flex

我该怎么办?这是我的 HTML 代码和 CSS:

/*
        DEMO STYLE
    */

@import "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700";
html {
  box-sizing: border-box;
}

body {
  font-family: 'Poppins', sans-serif;
  background: #fafafa;
}

p {
  font-family: 'Poppins', sans-serif;
  font-size: 1.1em;
  font-weight: 300;
  line-height: 1.7em;
  color: #999;
}

a,
a:hover,
a:focus {
  color: inherit;
  text-decoration: none;
  transition: all 0.3s;
}

.navbar {
  padding: 15px 10px;
  background: #fff;
  border: none;
  border-radius: 0;
  margin-bottom: 40px;
  box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  flex: 1;
}

.navbar-btn {
  box-shadow: none;
  outline: none !important;
  border: none;
}

.navbar-header {
  height: 30%;
}

.line {
  width: 100%;
  height: 1px;
  border-bottom: 1px dashed #ddd;
  margin: 40px 0;
}


/* ---------------------------------------------------
        SIDEBAR STYLE
    ----------------------------------------------------- */

#sidebar {
  width: 250px;
  position: fixed;
  top: 0;
  left: -250px;
  height: 100vh;
  z-index: 999;
  background: #7386D5;
  color: #fff;
  transition: all 0.3s;
  overflow-y: scroll;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.2);
}

#sidebar.active {
  left: 0;
}

#dismiss {
  width: 35px;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background: #7386D5;
  position: absolute;
  top: 10px;
  right: 10px;
  cursor: pointer;
  -webkit-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}

#dismiss:hover {
  background: #fff;
  color: #7386D5;
}

.overlay {
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.7);
  z-index: 998;
  display: none;
}

#sidebar .sidebar-header {
  padding: 20px;
  background: #6d7fcc;
}

#sidebar ul.components {
  padding: 20px 0;
  border-bottom: 1px solid #47748b;
}

#sidebar ul p {
  color: #fff;
  padding: 10px;
}

#sidebar ul li a {
  padding: 10px;
  font-size: 1.1em;
  display: block;
}

#sidebar ul li a:hover {
  color: #7386D5;
  background: #fff;
}

#sidebar ul li.active>a,
a[aria-expanded="true"] {
  color: #fff;
  background: #6d7fcc;
}

a[data-toggle="collapse"] {
  position: relative;
}

a[aria-expanded="false"]::before,
a[aria-expanded="true"]::before {
  content: '\e259';
  display: block;
  position: absolute;
  right: 20px;
  font-family: 'Glyphicons Halflings';
  font-size: 0.6em;
}

a[aria-expanded="true"]::before {
  content: '\e260';
}

ul ul a {
  font-size: 0.9em !important;
  padding-left: 30px !important;
  background: #6d7fcc;
}

ul.CTAs {
  padding: 20px;
}

ul.CTAs a {
  text-align: center;
  font-size: 0.9em !important;
  display: block;
  border-radius: 5px;
  margin-bottom: 5px;
}

a.download {
  background: #fff;
  color: #7386D5;
}

a.article,
a.article:hover {
  background: #6d7fcc !important;
  color: #fff !important;
}


/* ---------------------------------------------------
        CONTENT STYLE
    ----------------------------------------------------- */

#content {
  width: 100%;
  padding: 0;
  min-height: 100vh;
  transition: all 0.3s;
  position: absolute;
  top: 0;
  right: 0;
  display: flex
}

#map {
  width: 100%;
  flex: 2;
}
<div class="wrapper">
  <!-- Sidebar Holder -->
  <nav id="sidebar">
    <div id="dismiss">
      <i class="glyphicon glyphicon-arrow-left"></i>
    </div>

    <div class="sidebar-header">
      <h3>Bootstrap Sidebar</h3>
    </div>

    <ul class="list-unstyled components">
      <p>Dummy Heading</p>
      <li class="active">
        <a href="#homeSubmenu" data-toggle="collapse" aria-expanded="false">Home</a>
        <ul class="collapse list-unstyled" id="homeSubmenu">
          <li>
            <a href="#">Home 1</a>
          </li>
          <li>
            <a href="#">Home 2</a>
          </li>
          <li>
            <a href="#">Home 3</a>
          </li>
        </ul>
      </li>
      <li>
        <a href="#">About</a>
        <a href="#pageSubmenu" data-toggle="collapse" aria-expanded="false">Pages</a>
        <ul class="collapse list-unstyled" id="pageSubmenu">
          <li>
            <a href="#">Page 1</a>
          </li>
          <li>
            <a href="#">Page 2</a>
          </li>
          <li>
            <a href="#">Page 3</a>
          </li>
        </ul>
      </li>
      <li>
        <a href="#">Portfolio</a>
      </li>
      <li>
        <a href="#">Contact</a>
      </li>
    </ul>

    <ul class="list-unstyled CTAs">
      <li>
        <a href="https://bootstrapious.com/tutorial/files/sidebar.zip" class="download">Download source</a>
      </li>
      <li>
        <a href="https://bootstrapious.com/p/bootstrap-sidebar" class="article">Back to article</a>
      </li>
    </ul>
  </nav>

  <!-- Page Content Holder -->
  <div id="content">

    <nav class="navbar navbar-default">
      <div class="container-fluid">

        <div class="navbar-header">
          <button type="button" id="sidebarCollapse" class="btn btn-info navbar-btn">
                            <i class="glyphicon glyphicon-align-left"></i>
                            <span>Open Sidebar</span>
                        </button>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav navbar-right">
            <li>
              <a href="#">Page</a>
            </li>
            <li>
              <a href="#">Page</a>
            </li>
            <li>
              <a href="#">Page</a>
            </li>
            <li>
              <a href="#">Page</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <div id="map"></div>
  </div>
</div>

<div class="overlay"></div>

最佳答案

您的 navdiv class="map" 一起被包裹在 div class="content" 中。浏览器将尝试使 navmap 彼此相邻,因为它们是同一个 flex-container 的一部分。将您的 nav 移出 div class="content" 应该按照您的意愿行事。

关于html - 我无法在页面顶部设置导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521749/

上一篇:html - 我怎样才能像表格一样设置定义列表的样式?

下一篇:javascript - 当打印页面从顶部 html 给出 div 边距时

相关文章:

javascript - 将 getElementById 返回值视为数组

css - Chrome/Safari无法填充Flex父级的100%高度

javascript - 使用 flex-wrap 属性添加更多元素时元素不会被包裹

javascript - 如何获取嵌套跨度 HTML 元素的文本?

html - 如何旋转图像并在光标悬停时保持旋转?

javascript - 如何在设置为显示 : none 后更改显示属性的值

javascript - 如何在 react 组件中使用样式组件

html - 试图使背景图像可缩放,但它仍然在顶部显示可滚动版本

html - Css 表单进度条样式问题

javascript - 多个弹出窗口但只有一个有效...为什么?