I have the following HTML Code:
body {
margin: 0;
}
header {
background: #999;
color: white;
padding: 15px 15px 0 15px;
}
header h1 {
margin: 0;
display: inline;
}
nav ul{
margin: 0px;
display: inline;
}
nav ul li{
background: black;
color: white;
display: inline-block;
list-style-type: none;
padding: 5px 15px;
margin: 0;
}
nav ul li a{
color:white;
}
<!doctype html>
<html>
<head>
<title>CSS Layouts</title>
<link rel="stylesheet" href="styles/main.css">
</head>
<body>
<header>
<h1>My Page</h1>
<nav>
<ul>
<li><a href="#"></a>Home</li>
<li><a href="#"></a>Blog</li>
<li><a href="#"></a>About</li>
<li><a href="#"></a>Contact</li>
<li><a href="#"></a>Links</li>
</ul>
</nav>
</header>
<div class="row">
<div class="col">col1</div>
<div class="col">col2</div>
<div class="col">col3</div>
</div>
<footer>2016 My Site</footer>
</body>
</html>
我的挑战是按照CSS中的要求使“My Page”标题与其他内容水平对齐。
header h1 {
margin:0;
display: inline;
}
为了使h1标题“我的页面”与无序列表一行显示,我需要将HTML中的h1移动到
<nav>
开放标签之后(而不是现在在头部开放标签下面),但我无法弄清楚为什么当我这样做时它会一行显示,但如果保持目前的状态它就不会。据我的理解,在CSS中,如果您具有以下内容:
header h1{
some styling here;
}
在我的代码中,header下面的任何h1都不会受到影响,但实际情况并非如此。
nav
是block
。那是浏览器默认设置。你应该将其改为inline
。 - pol