我试图用Only CSS制作一个移动导航栏,但是即使checkbox起作用了,它也不起作用。 (我更改了
#main-navigation label { display: none;}
到
display: block;
看盒子是否可用。)我用
#toggle
代替
#main-navigation input
但是它没有用...我实际上使用了很多东西,但是我做错了或者我丢失了。请帮助
#nav-bg {
background-color: gray;
width: 50%;
height: 76px;
right: 0;
position: absolute;
}
#main-navigation {
float: right;
}
#main-navigation a {
text-decoration: none;
background-color: gray;
color: black;
font-size: 16px; /* (default) */
padding: 29px;
line-height: 76px;
margin-left: -3px;
border-right: solid thin;
border-right-color: darkgray;
transition: 800ms;
}
#main-navigation a:hover {
background-color: black;
color: ghostwhite;
transition: 800ms;
}
.navigation-active {
background-color: black !important;
color: ghostwhite !important;
}
.navigation-active:hover {
color: gray !important;
}
/* |||------------------------||| */
#main-navigation label {
display: none;
}
#main-navigation input {
display: block;
}
@media only screen and (max-width: 950px) {
#title-bg{
width: 50%;
}
/* {sourced */
#main-navigation label {
display: block;
cursor: pointer;
float: right;
font-size: 26px;
margin: 18px 31.5px;
}
#main-navigation {
display: block;
}
#main-navigation nav {
display: none;
margin-top: 76px;
transition: 800ms;
}
#main-navigation a {
display: block;
padding: 3px;
font-size: 14.5px;
text-align: center;
clear: right;
}
#main-navigation input:checked + #main-navigation nav {
display: block;
}
<div id="nav-bg">
<div id="main-navigation">
<!-- sourced --->
<input type="checkbox" id="toggle">
<label for="toggle">☰</label>
<!-- /sourced -->
<nav>
<a href="#" class="navigation-active">HOME</a>
<a href="#">QUESTS</a>
<a href="#">CONTACT</a>
<a href="#">ABOUT</a>
</nav>
</div>
</div>
答案 0 :(得分:0)
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
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;
}
}
</style>
</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);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
答案 1 :(得分:0)
所以您只需将导航设置为不显示,然后添加此输入[type = checkbox]:checked〜nav { 显示:块; }
#nav-bg {
background-color: gray;
width: 50%;
height: 76px;
right: 0;
position: absolute;
}
#main-navigation {
float: right;
}
#main-navigation a {
text-decoration: none;
background-color: gray;
color: black;
font-size: 16px; /* (default) */
padding: 29px;
line-height: 76px;
margin-left: -3px;
border-right: solid thin;
border-right-color: darkgray;
transition: 800ms;
}
#main-navigation a:hover {
background-color: black;
color: ghostwhite;
transition: 800ms;
}
.navigation-active {
background-color: black !important;
color: ghostwhite !important;
}
.navigation-active:hover {
color: gray !important;
}
/* |||------------------------||| */
#main-navigation label {
display: none;
}
#main-navigation input {
display: block;
}
@media only screen and (max-width: 950px) {
#title-bg{
width: 50%;
}
/* {sourced */
#main-navigation label {
display: block;
cursor: pointer;
float: right;
font-size: 26px;
margin: 18px 31.5px;
}
#main-navigation {
display: block;
}
nav {
display: none;
margin-top: 76px;
transition: 800ms;
}
#main-navigation a {
display: block;
padding: 3px;
font-size: 14.5px;
text-align: center;
clear: right;
}
input[type=checkbox]:checked~nav {
display: block;
}
<div id="nav-bg">
<div id="main-navigation">
<!-- sourced --->
<input type="checkbox" id="toggle">
<label for="toggle">☰</label>
<!-- /sourced -->
<nav>
<a href="#" class="navigation-active">HOME</a>
<a href="#">QUESTS</a>
<a href="#">CONTACT</a>
<a href="#">ABOUT</a>
</nav>
</div>
</div>