CSS导航 - 突出显示全宽

时间:2016-09-14 10:14:21

标签: css navigation

我希望我的导航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>

enter image description here

2 个答案:

答案 0 :(得分:2)

浮动会导致ul折叠到其子元素的宽度,因此请从ul移除浮动并改为使用text-align:right

&#13;
&#13;
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;
&#13;
&#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; 


}