尝试使用复选框在 NavBar 中为图标创建切换

Trying to create toggle for icon in NavBar using a checkbox

当屏幕处于移动视图时,我正在尝试使用复选框让我的超赞字体图标触发菜单图标以打开和关闭菜单(这是我希望它在设计上明智的方式)。

在此先感谢您的帮助。

/*** General ***/

* {
  font-family: 'Noto Serif SC', serif;
  margin: 0;
  padding: 0;
}

.bluepic {
  padding-top: 10px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 400px;
  height: auto;
}

.contents1 {
  margin-top: 5vh;
  margin-bottom: 10vh;
}

a {
  text-decoration: none;
  color: black;
  margin: none;
  padding: none;
}


/*** Header ***/

.header {
  margin-top: 20px;
  margin-bottom: 20px;
}

.logo {
  text-align: center;
  font-size: 30px;
}


/*** Navigation ***/

.fa-bars {
  display: none;
}

.nav-links {
  width: 100%;
  float: left;
  position: sticky;
  align-items: center;
  justify-content: center;
  padding: 0;
  background-color: #f7f7f7;
  border-bottom: 1px solid rgb(228, 224, 224);
  display: flex;
}

.nav-links li {
  list-style: none;
  text-align: center;
  font-size: 18px;
  float: left;
  padding: 14px 16px;
}

.nav-links li a {
  text-decoration: none;
  text-align: center;
  float: left;
}


/*** Footer ***/

.footer {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}

.footer2 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}


/*** Media Queries ***/

@media only screen and (max-width: 800px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 375px;
    height: auto;
  }
}

@media only screen and (max-width: 700px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 350px;
    height: auto;
  }
}

@media only screen and (max-width: 600px) {
  .fa-bars {
    font-size: 20px;
    display: block;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
  .logo {
    text-align: left;
    font-size: 25px;
  }
  .nav-toggle {
    display: none;
  }
  .nav-links,
  ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    text-align: center;
    background-color: #f7f7f7;
    border-bottom: 1px solid rgb(107, 76, 76);
    top: 80px;
    left: 0;
    flex-direction: column;
    display: none;
  }
  .nav-toggle:checked+.nav-links,
  ul {
    display: flex;
  }
  .nav-links li {
    display: block;
  }
  .contents1 {
    margin-top: 5vh;
  }
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

<div class="header">
  <div>
    <div class="logo">
      <h4>The Blue Lion <i class="fas fa-bars" id="nav-toggle"></i></h4>
        <input type="checkbox" id="nav-toggle" class="nav-toggle">
    </div>
  </div>

  <label for="nav-toggle" class="nav-toggle-label">
    <span></span>
  </label>
</div>

<div class="navbar">
  <div class="nav-links" id="myTopnav">
    <ul>
      <li a href="#">Food</li>
      <li a href="#">Drink</li>
      <li a href="#">Covid-19</li>
      <li a href="#">News</li>
      <li a href="#">Join the Team</li>
    </ul>
  </div>
</div>

<div class="contents1">
  <img src="https://mcdn.wallpapersafari.com/medium/84/3/pQuUz4.jpg" class="bluepic" alt="Blue">
</div>

<div class="footer">
  <div class="info">
    Leave us a review:
    <i class="fab fa-tripadvisor"></i>
  </div>

  <div class="socialiconsf">
    <i class="fab fa-facebook-f"></i>
    <i class="fab fa-twitter"></i>
    <i class="fab fa-instagram"></i>
  </div>
</div>

<div class="footer2">
  <p>
    © The Blue Lion 2021
  </p>
</div>

在 codepen 中查看此回复。 https://codepen.io/threefingers/pen/PojZMpx

在你的::before元素图标中添加你所在州的代码图标。

.mydiv{
  background-color: blue;
  height: 50px;
  width: 50px;
}

input + .mydiv::before{
  content: 'code icon 1';
}

input:checked + .mydiv::before{
  content: 'code icon 2';
}
<input type="checkbox" value="Car"> I have a car 
<div class="mydiv"></div>

这样做:

* {
  font-family: 'Noto Serif SC', serif;
  margin: 0;
  padding: 0;
}

.bluepic {
  padding-top: 10px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 400px;
  height: auto;
}

.contents1 {
  margin-top: 5vh;
  margin-bottom: 10vh;
}

a {
  text-decoration: none;
  color: black;
  margin: none;
  padding: none;
}


/*** Header ***/

.header {
  margin-top: 20px;
  margin-bottom: 20px;
}

.logo {
  text-align: center;
  font-size: 30px;
}


/*** Navigation ***/

.fa-bars {
  display: none;
}

.nav-links {
  width: 100%;
  float: left;
  position: sticky;
  align-items: center;
  justify-content: center;
  padding: 0;
  background-color: #f7f7f7;
  border-bottom: 1px solid rgb(228, 224, 224);
  display: flex;
}

.nav-links li {
  list-style: none;
  text-align: center;
  font-size: 18px;
  float: left;
  padding: 14px 16px;
}

.nav-links li a {
  text-decoration: none;
  text-align: center;
  float: left;
}


/*** Footer ***/

.footer {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}

.footer2 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}


/*** Media Queries ***/

@media only screen and (max-width: 800px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 375px;
    height: auto;
  }
}

@media only screen and (max-width: 700px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 350px;
    height: auto;
  }
}

@media only screen and (max-width: 600px) {
  .fa-bars {
    font-size: 20px;
    display: block;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
  .logo {
    text-align: left;
    font-size: 25px;
  }
  .nav-toggle {
    display: none;
    
  }
  .nav-links,
  ul {
    width: 100%;
    height: 100vh;
    text-align: center;
    background-color: #f7f7f7;
    border-bottom: 1px solid rgb(107, 76, 76);
    top: 80px;
    left: 0;
    flex-direction: column;
    display: none;
  }
  .nav-toggle:checked+.nav-links,
  ul {
    display: flex;
  }
  .nav-links li {
    display: block;
  }
  .contents1 {
    margin-top: 5vh;
  }
}
@media (max-width:600px){
    .hamburger-menu{
    
    position: relative;
}
    .checkbox:checked~ .nav-links{
        display: block;
    }
}
<div class="header">
  <div>
    <div class="logo">
      <h4>The Blue Lion 
        
      
            </div>
        
      </h4>
    </div>
    
    <label for="nav-toggle" class="nav-toggle-label">
      <span></span>
    </label>
  </div>
</div>

    <div class="navbar">
        <input type="checkbox" id="check" class="checkbox" hidden>
        <label for="check" class="fas fa-bars"></label>
        <div class="nav-links" id="myTopnav">
          <ul>
            <li a href="#">Food</li>
            <li a href="#">Drink</li>
            <li a href="#">Covid-19</li>
            <li a href="#">News</li>
            <li a href="#">Join the Team</li>
          </ul>
        </div>
      </div>


<div class="contents1">
  <img src="bluepic1.jpg" class="bluepic" alt="Blue">
</div>

<div class="footer">
  <div class="info">
    Leave us a review:
    <i class="fab fa-tripadvisor"></i>
  </div>
  
  <div class="socialiconsf">
    <i class="fab fa-facebook-f"></i>
    <i class="fab fa-twitter"></i>
    <i class="fab fa-instagram"></i>
  </div>
</div>

<div class="footer2">
  <p>
    © The Blue Lion 2021
  </p>
</div>

CSS修改:

font-awesome 库覆盖了您的 CSS。这就是为什么您的菜单图标 (.fa-bars) 在不处于移动模式时不会隐藏的原因。

我对您的 CSS 进行了以下更正:

  1. 正在将.fa-bars更改为.fas.fa-bars
  2. 在移动模式下添加!important
    (总是在媒体断点时显示 (... and (max-width: 600px))
/*** Navigation ***/
.fas.fa-bars {
  display: none;
}

@media only screen and (max-width: 600px) {
  .fas.fa-bars {
    font-size: 20px;
    display: block !important;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
}

复选框上的单击事件处理程序:

此外,如果您想要复选框的功能,那么您必须在 JS 中添加事件处理程序并在 HTML 中添加 onClick=function()(并更正多个问题如评论中所述分配 id:我将 id="nav-toggle" 替换为 id="nav-toggle-bars" 用于 <i class="fas fa-bars" 元素)。

下面是完整的代码 运行 代码片段 如果这是您想要的结果请发表评论(请告知)。

展开为“整页”并更改浏览器宽度以查看效果
低于媒体断点 (... and (max-width: 600px))

const menuToggle = document.getElementById("nav-toggle-bars");
let menuCheckBox;

function showHideNavToggle(event) {
  menuCheckBox = event.target;
  if (menuCheckBox.checked) {
    menuToggle.style.display = "block";
  } else {
    menuToggle.style.display = "none";
  }
}
/*** General ***/

* {
  font-family: 'Noto Serif SC', serif;
  margin: 0;
  padding: 0;
}

.bluepic {
  padding-top: 10px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 100%;
  max-width: 400px;
  height: auto;
}

.contents1 {
  margin-top: 5vh;
  margin-bottom: 10vh;
}

a {
  text-decoration: none;
  color: black;
  margin: none;
  padding: none;
}


/*** Header ***/

.header {
  margin-top: 20px;
  margin-bottom: 20px;
}

.logo {
  text-align: center;
  font-size: 30px;
}


/*** Navigation ***/

.fas.fa-bars {
  display: none;
}

.nav-links {
  width: 100%;
  float: left;
  position: sticky;
  align-items: center;
  justify-content: center;
  padding: 0;
  background-color: #f7f7f7;
  border-bottom: 1px solid rgb(228, 224, 224);
  display: flex;
}

.nav-links li {
  list-style: none;
  text-align: center;
  font-size: 18px;
  float: left;
  padding: 14px 16px;
}

.nav-links li a {
  text-decoration: none;
  text-align: center;
  float: left;
}


/*** Footer ***/

.footer {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}

.footer2 {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
  text-align: center;
}


/*** Media Queries ***/

@media only screen and (max-width: 800px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 375px;
    height: auto;
  }
}

@media only screen and (max-width: 700px) {
  .bluepic {
    padding-top: 10px;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
    max-width: 350px;
    height: auto;
  }
}

@media only screen and (max-width: 600px) {
  .fas.fa-bars {
    font-size: 20px;
    display: block !important;
    float: right;
    margin-top: 10px;
    margin-right: 40px;
    cursor: pointer;
  }
  .logo {
    text-align: left;
    font-size: 25px;
  }
  .nav-toggle {
    display: none;
  }
  .nav-links,
  ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    text-align: center;
    background-color: #f7f7f7;
    border-bottom: 1px solid rgb(107, 76, 76);
    top: 80px;
    left: 0;
    flex-direction: column;
    display: none;
  }
  .nav-toggle:checked+.nav-links,
  ul {
    display: flex;
  }
  .nav-links li {
    display: block;
  }
  .contents1 {
    margin-top: 5vh;
  }
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

<div class="header">
  <div>
    <div class="logo">
      <h4>The Blue Lion <i class="fas fa-bars" id="nav-toggle-bars"></i></h4>
        <input type="checkbox" onclick="showHideNavToggle(event)" id="nav-toggle" class="nav-toggle">
    </div>
  </div>

  <label for="nav-toggle" class="nav-toggle-label">
    <span></span>
  </label>
</div>

<div class="navbar">
  <div class="nav-links" id="myTopnav">
    <ul>
      <li a href="#">Food</li>
      <li a href="#">Drink</li>
      <li a href="#">Covid-19</li>
      <li a href="#">News</li>
      <li a href="#">Join the Team</li>
    </ul>
  </div>
</div>

<div class="contents1">
  <img src="https://mcdn.wallpapersafari.com/medium/84/3/pQuUz4.jpg" class="bluepic" alt="Blue">
</div>

<div class="footer">
  <div class="info">
    Leave us a review:
    <i class="fab fa-tripadvisor"></i>
  </div>

  <div class="socialiconsf">
    <i class="fab fa-facebook-f"></i>
    <i class="fab fa-twitter"></i>
    <i class="fab fa-instagram"></i>
  </div>
</div>

<div class="footer2">
  <p>
    © The Blue Lion 2021
  </p>
</div>

注:
我还更正了 2 个 html 结束标签(一个 </div> 被删除,</h4> 结束标签更正并将 img 源替换为 link 到通用蓝色背景)