第一个<a>元素不会在响应式导航中的 img 下方显示



我制作了一个响应式导航,除了一件烦人的小事外,它运行得很好:当我将屏幕宽度调整到1200px或以下时,菜单项应该会消失,取而代之的是一个汉堡图标,点击时会垂直显示所有消失的项目。虽然大多数链接都按预期进行,但第一个链接(主页(会将自己放在图像旁边,而不是下一行。

我计划将手机的1200像素改为800像素,但在comp上不需要调整太多屏幕大小就可以使用1200像素。

我试着在标签中制作图像,但它的填充物很乱,我不希望它可以点击。

有简单的解决方法吗?使图像具有display:block似乎并不能修复它。

我的代码:

<!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>
@import url('https://fonts.googleapis.com/css?family=Montserrat:500');
body {
margin: 0;
background-color: #393f4d;
background-image: linear-gradient(90deg, #323d46 0%, #393f4d 3% 97%, #323d46 100%);
font-family: Montserrat, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #1d1e22;
background-image: linear-gradient(105deg, #131417 0%, #1d1e22 5% 95%, #131417 100%);
padding: 15px 0px;
}
.topnav a {
float: left;
display: inline-block;
color: #feda6a;
transition: all 0.3s ease 0s;
text-align: center;
padding: 15px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
color: #c82850;
}
.topnav a.active {
color: #c85870;
}
.logo {
float: left;
display: block;
padding: 0px 16px;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 1200px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 1200px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0px;
top: 15px;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
</style>
</head>
<body>
<div class="topnav" id="myTopnav">
<img class="logo" src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQLQXYINuBRI13H7QE7ChnfwxQ50PY76Vs_KA&usqp=CAU" alt="Penguin" style="width:48px; height:48px;">
<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>
<div style="padding-left:16px">
<h2>Responsive Topnav Example</h2>
<p>Resize the browser window to see how it works.</p>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>

即使屏幕宽度低于1200px,也可以在.logo图像上设置float: left。这就是display: block不起作用的原因。只需在媒体查询中设置float: none

@media screen and (max-width: 1200px) {
.logo {
float: none;
display: block;
}
}

最新更新