我已经试着让navbar响应,但不知怎么的,它没有工作。它保持不变。你有什么推荐吗?我需要使用@media。
还有,你有什么提示,当做现有的代码响应,任何网站等?
nav {
display: flex;
justify-content: space-around;
align-items: center;
min-height: 8vh;
background-color: #5D4954;
font-family: 'Poppins', sans-serif;
}
.logo {
color: rgb(226, 226, 226);
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}
.nav-links {
display: flex;
justify-content: space-around;
width: 70%;
}
.nav-links li {
list-style: none;
}
.nav-links a {
color: rgb(226, 226, 226);
text-decoration: none;
letter-spacing: 1px;
font-weight: bold;
font-size: 14px;
border-radius: 3px;
text-transformation: uppercase;
cursor: pointer;
}
a.active, a:hover {
background: black;
transition: .5s;
}
我将使用@media和JS for菜单图标,当您调整页面大小时,它将具有您的navbar。
null
.topnav {
overflow: hidden;
background-color: lightgrey ;
}
.topnav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 12px;
}
.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;
}
<div class="topnav" id="myTopnav">
<a href="#">Test1</a>
<a href="#">Test2</a>
<a href="#">Test3</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>
如果您对使用display flex没有信心,我建议您不要尝试它。您可以使用just display none/block和float属性。
还要确保您使用ul和li标记创建nav项。
在响应式页面中,必须使用下面的元标记来在移动设备中正确地呈现页面。
null
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
padding: 0;
margin: 0;
}
nav {
background-color: #5D4954;
font-family: 'Poppins', sans-serif;
overflow: hidden;
}
nav ul {
margin: 0;
padding: 0;
}
.logo {
color: rgb(226, 226, 226);
text-transform: uppercase;
letter-spacing: 5px;
font-size: 20px;
}
.nav-links {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.nav-links li {
list-style: none;
}
.nav-links a {
color: rgb(226, 226, 226);
text-decoration: none;
letter-spacing: 1px;
font-weight: bold;
font-size: 14px;
border-radius: 3px;
text-transform: uppercase;
cursor: pointer;
}
a.active,
a:hover {
background: black;
transition: .5s;
}
.topnav .icon {
display: none;
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
@media screen and (max-width: 600px) {
.topnav li:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
.topnav.responsive {
position: relative;
}
.topnav.responsive li {
float: none;
display: block;
text-align: left;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a.icon {
float: none;
display: block;
text-align: left;
}
}
</style>
</head>
<body>
<nav class="topnav" id="myTopnav">
<ul>
<li class="nav-links"><a href="#home" class="active">Home</a></li>
<li class="nav-links"><a href="#news">News</a></li>
<li class="nav-links"><a href="#contact">Contact</a></li>
<li class="nav-links"><a href="#about">About</a></li>
</ul>
<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
</nav>
<div style="padding-left:16px">
<h2>Responsive Topnav with Dropdown</h2>
<p>Resize the browser window to see how it works.</p>
<p>Hover over the dropdown button to open the dropdown menu.</p>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>