html - 导航栏重新定位的问题
问题描述
我正在创建我的网站,并注意到带有下拉列表的导航栏的位置比我想要的低很多。我立即尝试使用一些不同的方法,例如边距和填充、位置:固定和位置:绝对以及设置与顶部的距离,但这几乎删除了我的下拉列表。我很想知道为什么会发生这种情况以及我可以做些什么来修复我的代码。
h1 {
font-size: 54px;
font-family: 'Kalam', cursive;
margin: 10px;
color: white;
}
body {
background: url("flowers.jpg") no-repeat fixed;
}
.navbar {
overflow: hidden;
background-color: #333;
font-family: 'Poiret One', cursive;
background-color: rgba(249, 197, 249, 0.5);
width: 100%;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: rgb(249, 197, 249);
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: rgb(249, 197, 249);
}
.dropdown:hover .dropdown-content {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="Japan.css">
<link href="https://fonts.googleapis.com/css?family=Kalam" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
<title>
Japan
</title>
</head>
<body>
<h1>Japan: Land of the Rising Sun</h1>
<div style="margin-top: 110px"class="navbar">
<a href="">Intro</a>
<a href="">WEIRD Facts</a>
<div class="dropdown">
<button class="dropbtn">Nature
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="Fauna.htm">Fauna</a>
<a href="Flora.htm">Flora</a>
<a href="Geography.htm">Geography</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Culture
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="Cuisine.html">Cuisine</a>
<a href="Clothing.html">Clothing</a>
<a href="Traditions.html">Traditions</a>
<a href="Sports.html">Sports</a>
<a href="Holidays.html">Holidays</a>
</div>
</div>
</div>
</body>
</html>
解决方案
由于h1
上面不需要的边距和标签,您有间距navbar
所以你需要从课堂上删除边距navbar
并移到h1
下面navbar
见下面的演示
h1 {
font-size: 54px;
font-family: 'Kalam', cursive;
margin: 10px;
color: #333;
}
body {
background: url("flowers.jpg") no-repeat fixed;
margin: 0;
}
.navbar {
overflow: hidden;
background-color: #333;
font-family: 'Poiret One', cursive;
background-color: rgba(249, 197, 249, 0.5);
width: 100%;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: rgb(249, 197, 249);
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: rgb(249, 197, 249);
}
.dropdown:hover .dropdown-content {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="Japan.css">
<link href="https://fonts.googleapis.com/css?family=Kalam" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
<title>
Japan
</title>
</head>
<body>
<h1>Japan: Land of the Rising Sun</h1>
<div class="navbar">
<a href="">Intro</a>
<a href="">WEIRD Facts</a>
<div class="dropdown">
<button class="dropbtn">Nature
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="Fauna.htm">Fauna</a>
<a href="Flora.htm">Flora</a>
<a href="Geography.htm">Geography</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Culture
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="Cuisine.html">Cuisine</a>
<a href="Clothing.html">Clothing</a>
<a href="Traditions.html">Traditions</a>
<a href="Sports.html">Sports</a>
<a href="Holidays.html">Holidays</a>
</div>
</div>
</div>
</body>
</html>
推荐阅读
- java - Hibernate @ManyToMany 关系的唯一约束违反
- node.js - 如何在不手动撤消续集迁移的情况下部署以前版本的节点服务器
- java - tomcat显示空白页
- google-cloud-platform - 将大于 5TB 的对象上传到谷歌云存储桶
- python - 无法导入 python3-requests,尽管它已安装
- python - AttributeError:模块“张量流”没有属性“日志”
- c++ - 在 C++ 中具有多线程的 OpenCV
- android - 加快 FFmpeg 处理时间
- android - 将单选按钮的文本放在其中
- python - 我正在尝试使用来自覆盆子的 IP 地址在 Windows Server 12 上编写 .txt