html - 使导航栏响应
问题描述
我在使导航栏响应时遇到问题 - 如果我更改 topnav 类中导航栏的默认高度并调整屏幕大小,其他菜单选项会在小屏幕视图中消失。我需要帮助来解决这个问题 -
下面是我的代码:
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
<style>body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #001a33;
height: 2.5rem;
}
.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;
}
.topnav a.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>
<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>
而且我还想在导航栏总高度的中心对齐文本(现在它正在触摸栏的底部)
我需要帮助来解决这两个问题。提前致谢 !!
解决方案
我已经提供了答案,但如果您不知道如何更改此处的更改位置,这里是您的完整代码以及新的更改。您可以使用此代码。:)
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color:#001a33;
height: auto;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: inline-block;}
.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>
推荐阅读
- angularjs - $sce.trustAsResourceUrl 不适用于 iframe 上的 Twitter URL
- javascript - TypeScript 2.9.1 中私有变量的意外标记
- java - HQL 中“~*”运算符的模拟
- file - 检查资产是否存在
- angular5 - Angular 5/6 项目查询参数加密
- c - 具有多个目标文件的动态库,在加载过程中失败
- android - 每次在 Fragment 中的 onSaveInstanceState null
- selenium-webdriver - 硒 - 无法编辑文本字段
- css - AngularJs Material(或者一般来说可能只是css)-如何在行之间对齐列?
- c++ - 为多线程 unif_rand() 播种