我希望我的导航background: rgba(0,0,0,.5)
以全宽显示。
我已将导航重新定位到右侧。但是,顶部不透明的黑色背景未显示在左侧。请检查此代码的问题:
body {
font-size: 1em;
font-family: sans-serif;
color: #000000;
margin: 0;
padding: 0;
}
h1 {
margin-bottom: 10px;
}
nav {
background: rgba(0, 0, 0, .5);
}
nav > ul {
float: right;
background: rgba(0, 0, 0, .5);
}
nav ul > li {
display: inline-block;
padding: 10px;
margin: 10px;
}
nav ul li:hover {
background-color: #587058;
}
nav ul li > a {
text-decoration: none;
color: #ffd800;
}
nav ul::after {
content: '';
display: block;
clear: right;
}
header {
height: 500px;
background: url("1.jpg") no-repeat center;
background-size: cover;
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CSS Menus</title>
<link rel="stylesheet" type="text/css" href="assets/menus1.css">
</head>
<body>
<header id="the_header">
<a class="logo" href="#"></a>
<nav id="the_nav">
<ul>
<li><a href="">Home</a>
</li>
<li><a href="">Skills</a>
</li>
<li><a href="" aria-haspopup="true">Projects</a>
</li>
<li><a href="">Contact Me</a>
</li>
</ul>
</nav>
</header>
</body>
</html>
答案 0 :(得分:2)
浮动会导致ul
折叠到其子元素的宽度,因此请从ul
移除浮动并改为使用text-align:right
。
body {
font-size: 1em;
font-family: sans-serif;
color: #000000;
margin: 0;
padding: 0;
}
h1 {
margin-bottom: 10px;
}
nav {
background: rgba(0, 0, 0, .5);
}
nav > ul {
text-align: right;
background: rgba(0, 0, 0, .5);
}
nav ul > li {
display: inline-block;
padding: 10px;
margin: 10px;
}
nav ul li:hover {
background-color: #587058;
}
nav ul li > a {
text-decoration: none;
color: #ffd800;
}
nav ul::after {
content: '';
display: block;
clear: right;
}
header {
height: 500px;
background: url("1.jpg") no-repeat center;
background-size: cover;
}
&#13;
<header id="the_header">
<a class="logo" href="#"></a>
<nav id="the_nav">
<ul>
<li><a href="">Home</a>
</li>
<li><a href="">Skills</a>
</li>
<li><a href="" aria-haspopup="true">Projects</a>
</li>
<li><a href="">Contact Me</a>
</li>
</ul>
</nav>
</header>
&#13;
答案 1 :(得分:0)
你必须将float右移到text-align。
body {
font-size: 1em;
font-family: sans-serif;
color: #000000;
margin: 0;
padding: 0;
}
h1 {
margin-bottom: 10px;
}
nav {
background: rgba(0,0,0,.5);
}
nav > ul {
text-align:right; /*change*/
background: rgba(0,0,0,.5);
}
nav ul > li {
display: inline-block;
padding: 10px;
margin: 10px;
}
nav ul li:hover {
background-color: #587058;
}
nav ul li > a{
text-decoration: none;
color: #ffd800;
}
nav ul::after {
content:'';
display: block;
clear: right;
}
header {
height: 500px;
background: url("1.jpg") no-repeat center;
background-size: cover;
}