响应式标头问题

时间:2018-02-21 05:03:00

标签: html5 css3 flexbox



* {
  box-sizing: border-box;
  font-family: sans-serif;
  margin: 0;
  padding: 0;
}

html,
body {
  font-size: 14px;
}

header {
  width: 100vw;
  height: 50px;
  background-color: #222;
  margin: 0 auto;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}


/* Container for the Left and Right nav sections*/

header>div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1140px;
  width: 100%;
  padding: 0 20px;
}


/* adjust width to set size */

nav {
  width: 70%;
  display: inline-flex;
  justify-content: space-between;
}

nav ul {
  list-style-type: none;
  display: flex;
  padding: 0 20px;
}

nav a {
  padding: .8rem 1rem;
}

header>div>a {
  font-size: 18px;
  border-bottom:
}

.a-tag-header {
  text-decoration: none;
  color: #999;
  display: block;
}

nav a:hover {
  color: #fff;
}


/* -- Menu Toggle -- */

.MenuToggle {
  display: none;
}

@media screen and (max-width: 890px) {
  /* Header */
  header {
    height: auto;
  }
  header>div {
    flex-direction: column;
    padding: 0;
  }
  nav {
    display: flex;
    width: 100%;
    flex-direction: column;
  }
  nav ul {
    flex-direction: column;
    text-align: center;
    align-items: center;
    padding: 0;
  }
  nav ul li {
    height: 50px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  nav ul li:hover {
    background-color: aqua;
  }
  header>div>a {
    padding: .8rem 0;
  }
  header>div>a:hover {
    background-image: #eee;
  }
  /* -- Menu Toggle -- */
  .MenuToggle {
    display: inline-block;
    color: #F0F0F0;
    font-size: 22px;
    cursor: pointer;
    position: absolute;
  }
}

<header>
  <div>
    <a class="a-tag-header">Chemical Finger Print Analysis</a>
    <div class="MenuToggle">
      <span id="MenuToggleButton" onclick="NavToggle()">&#9776;</span>
    </div>
    <nav>
      <ul>
        <li><a href="#" class="a-tag-header">Home</a></li>
        <li><a href="#" class="a-tag-header">About</a></li>
        <li><a href="#" class="a-tag-header">Contact</a></li>
        <li><a href="#" class="a-tag-header">Data</a></li>
        <li><a href="#" class="a-tag-header">Reports</a></li>
      </ul>
      <ul>
        <li><a href="#" class="a-tag-header">Register</a></li>
        <li><a href="#" class="a-tag-header">Login</a></li>
      </ul>
    </nav>

    <div class="MenuToggle">
      <span id="MenuToggleButton" onclick="NavToggle()">&#9776;</span>
    </div>
  </div>
</header>
&#13;
&#13;
&#13;

您好我正在创建一个Flexbox导航栏(桌面优先),它包含一个标题和一个带有两个单独标签的导航。我已经获得了nav和ul标签以响应方式行动但是当屏幕小于850px时,我似乎无法弄清楚如何获得标题和菜单切换以响应共享顶部容器,我&#39已经尝试了一些修复,但我似乎无法弄明白。请帮忙

1 个答案:

答案 0 :(得分:0)

试试这个

function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>





</body>
</html>