屏幕尺寸缩小时,导航栏没有响应

时间:2019-03-02 16:39:59

标签: html css responsive-design

1。)当屏幕变小时,导航栏不会像应该的那样对彼此的叠加进行响应,下面显示的手机屏幕大小大致如下图:

Either side of menu nav bar there is white space which I don't want

Nav Bar when viewed on a phone screen size

2。)另一件事是菜单导航栏没有延伸到屏幕的尽头,我已尝试将其宽度设置为100%,请有人可以给我任何建议吗?

<header>
    <div class="row">
        <div class="grid-6">
            <div class="navbar">
                <a class="active" href="home.php"><i class="fas fa-home"></i> Home</a> 
                <a href="Gallery.html"><i class="fas fa-images"></i> Gallery</a>
                <div class="dropdown">
                    <button class="dropbtn"><i class="fas fa-award"></i> Competitions</button>
                    <div class="dropdown-content">
                        <a href="#">Link 1</a>
                        <a href="#">Link 2</a>
                        <a href="#">Link 3</a>
                    </div>
                </div> 
                <a href="Blog.html"><i class="fas fa-blog"></i> Blog</a>
                <a href="Contact.html"><i class="fas fa-envelope"></i> Contact</a> 
                <a href="Signup.php"><i class="fas fa-check-square"></i> Signup</a>
                <a href="Login.php"><i class="fas fa-sign-in-alt"></i> Login</a>
                <a href="Logout.inc.php"><i class="fas fa-sign-out-alt"></i> Logout</a>                                 
            </div>
        </div>
    </div>
</header>

CSS:

/* Add responsiveness - will automatically display the navbar vertically instead of horizontally on screens less than 500 pixels */
@media screen and (max-width: 500px) {
  .navbar a {
    float: none;
    display: block;
  }
}

.navbar {
  width: 100%;
  background-color: #555;
  overflow: auto;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}

1 个答案:

答案 0 :(得分:1)

您必须在css文件的底部声明媒体查询,以确保样式的顺畅级联。

下面的解决方案。

.navbar {
  width: 100%;
  background-color: #555;
  overflow: auto;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}


/* Add responsiveness - will automatically display the navbar vertically instead of horizontally on screens less than 500 pixels */

@media only screen and (max-width: 500px) {
  .navbar a,
  .dropdown {
    float: none;
    display: block;
    text-align: center;
  }
}
<header>
  <div class="row">
    <div class="grid-6">
      <div class="navbar">
        <a class="active" href="home.php"><i class="fas fa-home"></i> Home</a>
        <a href="Gallery.html"><i class="fas fa-images"></i> Gallery</a>
        <div class="dropdown">
          <button class="dropbtn"><i class="fas fa-award"></i> Competitions</button>
          <div class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
            <a href="#">Link 3</a>
          </div>
        </div>
        <a href="Blog.html"><i class="fas fa-blog"></i> Blog</a>
        <a href="Contact.html"><i class="fas fa-envelope"></i> Contact</a>
        <a href="Signup.php"><i class="fas fa-check-square"></i> Signup</a>
        <a href="Login.php"><i class="fas fa-sign-in-alt"></i> Login</a>
        <a href="Logout.inc.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
      </div>
    </div>
  </div>
</header>

link to jsFiddle