导航栏后面有 h1 标签,表现得很奇怪

  • 本文关键字:标签 h1 导航 html css
  • 更新时间 :
  • 英文 :


我的HTML和CSS有点麻烦。 我得到了一些代码来制作导航栏。 .HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Daicicle's Combos</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="nav">
<a href="#" class="nav-title">Website</a>
<nav>
<ul>
<li><a href="#" class="nav-link">Home</a></li>
</ul>
</nav>
</div>
</body>
</html>

.CSS:

@import url('https://fonts.googleapis.com/css?family=Roboto:400,700');
body {margin: 0; font-family: 'Roboto', sans-serif;}

.nav-title {
float: left;
color: rgb(114, 114, 114);
text-decoration: none;
padding: 1rem 1rem 1rem 1rem;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
position: relative;
padding: 1rem 1rem 1rem 1rem;
}
nav li a {
padding: 5px 5px 5px 5px;
color: rgb(114, 114, 114);
text-decoration: none;
background-color: rgb(230, 230, 230);
border-radius: 3px;
}
.title {
margin: 0;
text-align: center;
color: rgb(114, 114, 114);
font-size: 3em;
padding-top: 2vh;
}

然而,当我尝试在带有nav类的div 下方添加一个 h1 标签时,我得到这个:

https://cdn.discordapp.com/attachments/464823851151523850/496068123376615448/unknown.png

我做错了什么?

这里有一些关于浮点数的有用信息

div.nav只包含浮动元素,因此它正在失去其形状。您需要一个修复(在我的代码段中检查 css(:

body {margin: 0; font-family: 'Roboto', sans-serif;}
/* Fix for wrapping floating content */
.clearfix::after {
content: "";
clear: both;
display: table;
}
/* End of the fix */
.nav { background: #eee; }
.nav-title {
float: left;
color: rgb(114, 114, 114);
text-decoration: none;
padding: 1rem 1rem 1rem 1rem;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
position: relative;
padding: 1rem 1rem 1rem 1rem;
}
nav li a {
padding: 5px 5px 5px 5px;
color: rgb(114, 114, 114);
text-decoration: none;
background-color: rgb(230, 230, 230);
border-radius: 3px;
}
.title {
margin: 0;
text-align: center;
color: rgb(114, 114, 114);
font-size: 3em;
padding-top: 2vh;
}
<div class="nav clearfix">
<a href="#" class="nav-title">Website</a>
<nav>
<ul>
<li><a href="#" class="nav-link">Home</a></li>
</ul>
</nav>
</div>
<h1 class="title">My header</h1>

对我来说,一切都看起来很好。 看看我的JSFIDDLE。 如果你想让 H1 出现在导航栏下,你必须在 CSS 中定义.nav类。

还可以尝试width: 100%并在.navoverflow: hidden

https://jsfiddle.net/84nkfjr3/

最新更新