html - 导航栏重新定位的问题

标签 html css navigation position navbar

我在创建我的网站时注意到带有下拉列表的导航栏的位置比我想要的低很多。我立即尝试使用几种不同的方法,例如边距和填充、position: fixed 和 position: absolute 以及设置与顶部的距离,但这几乎删除了我的下拉列表。我很想知道为什么会这样,以及我可以做些什么来修复我的代码。

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>

最佳答案

尝试添加一个 normalize.css 或 reset.css,这为您提供了一个开始每个元素的点,在每个浏览器中具有相同的样式:

https://necolas.github.io/normalize.css/

关于html - 导航栏重新定位的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54747908/

相关文章:

html - 在特定位置显示一个 div

css - 用于移动优化的媒体查询

仅适用于黑莓的 Css?

html - Css 导航栏 (css&html) 问题

Android:抽屉导航标题 TextView 未以编程方式设置

html - 如果子 div 更大,则拉伸(stretch)父 div

css - 如何让此列表水平呈现?

javascript - Button.value 没有更改按钮的文本值

javascript - 为什么使用CSSOM的insertRule后样式不可见,样式标签不能重新追加

xamarin.forms - 使用 MvvmCross 6.2.1 和 Xamarin.Forms 清除导航堆栈