如何为基本的CSS粘性导航栏添加汉堡菜单?

epggiuax  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(141)

我已经在页面顶部制作了一个基本的导航栏,但我承认我在理解如何使用CSS创建一个小屏幕的汉堡菜单方面遇到了困难(我对JavaScript还是个新手,对如何创建导航栏脚本一点概念都没有)我只想至少把它变成一个专栏,我知道使用复选框可以通过CSS实现这一点,但是我自己在实现它时遇到了麻烦。

* {box-sizing: border-box;}

body {font-family: Helvetica, Arial, sans-serif;
      margin: 0;
      padding: 0;}

.navbar {background-color: rgba(50, 150, 220, 1);
         display: flex;
         align-items: center;
         justify-content: space-between;
         width: 100%;
         height: 50px;
         position: fixed;
         top: 0;
         overflow: hidden;
         color: white;
         padding: 25px 25px;}

.navbar ul {display: flex;}

.navbar ul li {list-style-type: none;}

.navbar ul li a {display: block;
                 text-decoration: none;
                 color: white;
                 padding: 1rem;}

.navbar ul li a:hover {background-color: rgb(200, 200, 200);}

main {height: 1500px;}
<header>
  <nav class="navbar">
    <h2>Site name</h2>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Settings</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>

我尝试将媒体查询中的navbar类设置为"flex-direction:列;"运气不好。

7cwmlq89

7cwmlq891#

这是下面的代码,复选框切换打开和关闭html Hemberger。

* {box-sizing: border-box;}

body {font-family: Helvetica, Arial, sans-serif;
      margin: 0;
      padding: 0;}

.navbar {background-color: rgba(50, 150, 220, 1);
         display: flex;
         align-items: center;
         justify-content: space-between;
         width: 100%;
         height: 50px;
         position: fixed;
         top: 0;
         overflow: hidden;
         color: white;
         padding: 25px 25px;}

.navbar ul {display: flex;}

.navbar ul li {list-style-type: none;}

.navbar ul li a {display: block;
                 text-decoration: none;
                 color: white;
                 padding: 1rem;}

.navbar ul li a:hover {background-color: rgb(200, 200, 200);}

main {height: 1500px;}

/* New Code */
/* for large device */
.navbar #bar,
.navbar [for='bar']{
  display: none;
}

/* for small device */
@media(max-width: 500px){
  .navbar{
    position: relative;
    overflow: visible;
    
    
  }
  .navbar ul{
    display: none; 
  }

  .navbar [for='bar']{
  display: block;
  cursor: pointer;
  }
 
  

 
  
 
  
  .navbar input:checked ~ ul{
    display: block;
    position: absolute;
    top: 100%;
    background:red;
    border: solid;
    left: 0px;
    width: 100%;
    margin: 0px;
    padding: 0px;
    text-align: center;
    font-size: 17px;
  }
}
<header>
  <nav class="navbar">
    <h2>Site name</h2>
    <input type="checkbox" id="bar"/>
    <label for="bar"> 
      <svg fill="#fff" viewBox="0 0 100 80" width="40" height="40">
        <rect width="100" height="20"></rect>
        <rect y="30" width="100" height="20"></rect>
        <rect y="60" width="100" height="20"></rect>
      </svg>
    </label>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Settings</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>

相关问题