无需 JavaScript 的响应式导航栏
HTML -
CSS -
Codepen -
大家好!今天我将演示如何制作一个无需 JavaScript 的响应式导航栏,并使其具有下拉效果,适用于移动设备。主要的 CSS 部分将先讲解,其余代码将在本博客末尾的代码库中提供。
让我们开始吧...
我将使用 :has() 选择器来实现切换效果;您可以在这里查看哪些浏览器支持它。https
://caniuse.com/?search =has
HTML -
<header>
<div class="logo">
<h1 class="logo-text">LOGO</h1>
<button class="hamburger-icon">
<label for="dropdown">
<i class="fa-solid fa-bars"></i>
<i class="fa-solid fa-x"></i>
</label>
<input type="checkbox" id="dropdown" />
</button>
</div>
<ul class="navigation">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>
- 我在按钮内部放置了一个复选框字段,以创建切换效果。条形和十字图标也会通过此复选框进行切换。
CSS -
/* hiding the hamburger icon for the desktop view */
.hamburger-icon{
display:none;
border:none;
background-color:transparent;
}
/* Hiding the checkbox default styling */
.hamburger-icon input[type="checkbox"] {
appearance: none;
}
.
.
.
/* targeting the element for the
viewport less than 600px*/
@media screen and (max-width: 600px) {
/* Hiding the nav links with height and overflow */
.navigation {
height:0;
overflow:hidden;
flex-direction: column;
align-items: center;
transition:all 0.5s ease-in-out;
}
/* It will put the Logo text at left end
and Hamburger at the right end of the header */
.logo {
display: flex;
justify-content: space-between;
}
}
/* Making the hamburger button visible */
.hamburger-icon {
display: block;
}
/* Initially hiding the cross icon */
.fa-x{
display:none;
}
/*determining whether the header contains a checkbox input that is checked before expanding the navigation's height to 100 pixels to make it visible.*/
header:has(.hamburger-icon input[type="checkbox"]:checked) .navigation {
margin-top: 1rem;
height:100px;
}
/* Hiding the bars icon on checkbox checked state */
header:has(.hamburger-icon input[type="checkbox"]:checked) .fa-bars {
display:none;
}
/* Showing the cross icon on checkbox checked state */
header:has(.hamburger-icon input[type="checkbox"]:checked) .fa-x {
display:inline-block;
}
Codepen -
感谢您查看此帖子
您可以通过以下方式联系我 -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
电子邮件 - shubhmtiwri00@gmail.com
^^您可以通过以下链接捐款来帮助我谢谢👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--
另请查看这些帖子
https://dev.to/shubhamtiwari909/css-iswherehas-and-not-2afd
https://dev.to/shubhamtiwari909/theme-changer-with-html-and-css-only-4144
https://dev.to/shubhamtiwari909/swiperjs-3802
https://dev.to/shubhamtiwari909/going-deep-in-array-sort-js-2n90
文章来源:https://dev.to/shubhamtiwari909/responsive-navbar-without-javascript-3p7o