【问题标题】:Dynamically causing a vertical nav bar to become horizontal on screen resize动态导致垂直导航栏在屏幕调整大小时变为水平
【发布时间】:2017-10-06 19:16:22
【问题描述】:

我正在尝试创建一个动态导航栏。默认情况下,我希望导航栏在跨越整个页面高度的 div 中垂直显示,但是在调整大小时,我希望导航栏变成水平的。这是我想要实现的目标的图片:

我试图通过将display:block; 添加到我的ul 来根据W3Schools suggestions 塑造我的方法,但这并没有改变任何东西。我对媒体查询的理解不是最好的,但我也尝试过改变左侧 div 的宽度和高度(深灰色):

@media screen and (max-width: 200px) {
    .nav-container{
        width: 100%;
        height: 200px;
        background-color: #333;
        border-bottom: 0.5px solid #333;
    }
}

实现这一目标的最佳方法是什么?

编辑

html,
body {
  height: 100%;
  background-color: #fff;
}

body {
  background-color: #fff;
  text-align: center;
  margin: 0;
  font-family: "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  display: block;
}

.site-wrapper {
  display: table;
  width: 100%;
  height: 100%;
  min-height: 100%;
  -webkit-box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5);
  box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5);
}

* {
  margin: 0;
  padding: 0;
}

.site-wrapper {
  display: flex;
  flex-wrap: wrap;
}

.nav-container {
  height: 100%;
  width: 200px;
  background-color: #333;
  display: flex;
  justify-content: center;
  align-items: center;
}

.logo-holder {
  position: absolute;
  top: 0;
  text-align: center;
}

.logo-holder img {
  height: auto;
}

#navigation-div {
  margin-top: -300px;
  width: 100%;
}

.nav-ul li a {
  display: block;
}

.nav-link {
  width: 100%;
  display: block;
  text-align: left;
  color: #fff;
  text-decoration: none;
  margin-left: 0px;
  padding-left: 15px;
}

.nav-link:hover {
  background-color: #fff;
  color: #333;
}

.nav ul {
  width: 100%;
  padding-left: 0px;
}

.nav ul li {
  list-style-type: none;
  width: 100%;
}

.nav ul li a {
  text-align: left;
  padding: 5px;
  color: #fff;
  text-decoration: none;
  margin-left: 15px;
}

.nav li:hover {
  background-color: #fff;
}

.nav li:hover a {
  color: #333;
}

@media screen and (max-width: 540px) {
  .nav-container {
    width: 100%;
    height: 160px;
    background-color: #333;
    border-bottom: 0.5px solid #333;
  }
  .nav-container nav,
  .nav-container nav ul,
  .nav-container nav ul li,
  .logo-holder {
    display: inline;
  }
  .logo-holder {
    position: absolute;
    left: 0;
  }
  #navigation-div {
    float: left;
  }
}
<div class="site-wrapper">
  <div class="nav-container">
    <div class="logo-holder">
      <img src="#" alt="Logo" />
    </div>
    <div id="navigation-div">
      <nav class="nav left-nav-bar">
        <ul class="nav-ul">
          <a class="nav-link active" href="">
            <li>Home</li>
          </a>
          <a class="nav-link" href="">
            <li>Blog</li>
          </a>
          <a class="nav-link" href="">
            <li>Store</li>
          </a>
          <a class="nav-link" href="">
            <li>Contact</li>
          </a>
        </ul>
      </nav>
    </div>
  </div>
</div>

【问题讨论】:

  • 在您的方法中没有媒体查询。只需从您的代码中添加媒体查询 + float: none;
  • 寻求代码帮助的问题必须包含重现它所需的最短代码在问题本身中最好在Stack Snippet中。

标签: html css navbar


【解决方案1】:

首先,您的媒体查询可能根本不会被调用,因为最大宽度非常小,只有 200 像素。

在您的媒体查询中,您需要将导航元素和徽标的显示属性设置为内联,以便它们并排显示。 (您也可以考虑为此使用 flexbox)您还需要重置一些定义在较高位置的属性,例如 h3 徽标元素的 line-height。

@media screen and (max-width: 480px) {
  .nav-container{
      width: 100%;
      height: auto;
      background-color: #333;
      border-bottom: 0.5px solid #333;
   }
  .nav-container nav,
  .nav-container nav ul,
  .nav-container nav ul li,
  .logo-holder,
  .logo-holder h3 {
      display: inline;
  }
  .logo-holder h3 {
      line-height: 1;
  }
}

【讨论】:

  • 嗨,安德里亚,感谢您的回答。我已经根据您的方法塑造了我的代码,但导航链接似乎没有出现。你能不能看看我编辑的问题,看看我在哪里错误地实施了你的方法?
  • 嗨 Freddy,您似乎在您的代码中使用了很多布局技术(浮动、flexbox、不同的显示值),如果您只是去跟踪一切如何协同工作会更容易与一个。您看不到导航链接的原因是,#navigation-div 的上边距为 -300px,因此它位于屏幕顶部上方。您使用的是 Chrome devtools 还是类似的东西?如果没有,我强烈推荐它!它使您更容易弄清楚您的元素发生了什么。
  • Here's 一个jsbin,你的代码中有一些cmets,希望它有用
  • 嗨,安德里亚,感谢您的帮助! cmets 对帮助我理解我自己造成的问题非常有用。为了清楚起见,我的导航栏现在可以正常工作了! :)
猜你喜欢
  • 2019-11-12
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
相关资源
最近更新 更多