HTML / CSS Navbar乱序

时间:2017-11-20 07:30:17

标签: html css dropdown

我试图创建一个带有两个下拉菜单的导航栏,在经过下拉菜单的大量摆弄后,我发现它有点工作。但它使其余的项目无序。

https://jsfiddle.net/o5pcs1y7/



body {
  font-family: Arial;
  background-color: #FAFAFA;
}

.container {
  width: 960px;
  position: relative;
  margin-left: auto;
  margin-right: auto;
  background-color: #FAFAFA;
  padding: 10px 10px 10px;
}

.header h1 {
  border-bottom: solid 2px #2c3840;
  text-transform: uppercase;
  color: #2c3840;
}

.header img {
  float: right;
  position: relative;
  width: 90px;
  margin-top: -60px;
}

.nav {
  background-color: #2c3840;
  position: relative;
  width: 100%;
  float: left;
  height: 41px;
  margin-top: -22px;
}

.nav a {
  font-family: Arial;
  float: left;
  display: inline-block;
  color: #FAFAFA;
  text-align: center;
  padding: 10px 14px;
  text-decoration: none;
  font-size: 18px;
}

.nav a:hover {
  background-color: #6d4b4f;
}

.projects {
  position: relative;
  display: inline-block;
}

.projects-content {
  display: none;
  position: absolute;
  background-color: #FAFAFA;
  min-width: 160px;
  padding: 10px 14px;
  z-index: 1;
  text-decoration: none;
}

.projects:hover .projects-content {
  display: block;
}

.services {
  position: relative;
  display: inline-block;
}

.services-content {
  display: none;
  position: absolute;
  background-color: #FAFAFA;
  min-width: 160px;
  padding: 12px 16px;
  z-index: 1;
  text-decoration: none;
}

.services:hover .services-content {
  display: block;
}

<!doctype html>


<title>Werribee Roadworthy Centre</title>
<link rel="shortcut icon" type="image/png" href="favicon.png" />



<body>
  <div class="container">
    <div class="header">
      <h1>Werribee Roadworthy Centre</h1>
      <img src="car.png">
    </div>
    <div class="nav">
      <a href="index.html">Home</a>
      <div class="services">
        <a href="services.html">Services</a>
        <div class="services-content">
          <a href="#">1</a>
          <a href="#">2</a>
          <a href="#">3</a>
        </div>
      </div>
      <div class="projects">
        <a href="projects.html">Projects</a>
        <div class="projects-content">
          <a href="#">1</a>
          <a href="#">2</a>
          <a href="#">3</a>
        </div>
      </div>
      <a href="photos.html">Photo Gallery</a>
      <a href="conact.html">Contact Us</a>

    </div>
    <div class="section">

    </div>
    <div class="aside">

    </div>







  </div>
</body>
&#13;
&#13;
&#13;

这是代码。导航栏的顺序在HTML中是正确的,而不是在成品中。也不要介意破损的下拉。

1 个答案:

答案 0 :(得分:1)

由于父.nav可能有diva的孩子,因此您没有像div那样将a浮动到左边是什么造成了混乱:

你只需要添加:

.nav > div {
  float: left;
}

body {
  font-family: Arial;
  background-color: #FAFAFA;
}

.container {
  width: 960px;
  position: relative;
  margin-left: auto;
  margin-right: auto;
  background-color: #FAFAFA;
  padding: 10px 10px 10px;
}

.header h1 {
  border-bottom: solid 2px #2c3840;
  text-transform: uppercase;
  color: #2c3840;
}

.header img {
  float: right;
  position: relative;
  width: 90px;
  margin-top: -60px;
}

.nav {
  background-color: #2c3840;
  position: relative;
  width: 100%;
  float: left;
  height: 41px;
  margin-top: -22px;
}

.nav a {
  font-family: Arial;
  float: left;
  display: inline-block;
  color: #FAFAFA;
  text-align: center;
  padding: 10px 14px;
  text-decoration: none;
  font-size: 18px;
}
.nav > div {
  float: left;
}
.nav a:hover {
  background-color: #6d4b4f;
}

.projects {
  position: relative;
  display: inline-block;
}

.projects-content {
  display: none;
  position: absolute;
  background-color: #FAFAFA;
  min-width: 160px;
  padding: 10px 14px;
  z-index: 1;
  text-decoration: none;
}

.projects:hover .projects-content {
  display: block;
}

.services {
  position: relative;
  display: inline-block;
}

.services-content {
  display: none;
  position: absolute;
  background-color: #FAFAFA;
  min-width: 160px;
  padding: 12px 16px;
  z-index: 1;
  text-decoration: none;
}

.services:hover .services-content {
  display: block;
}
<!doctype html>


<title>Werribee Roadworthy Centre</title>
<link rel="shortcut icon" type="image/png" href="favicon.png" />



<body>
  <div class="container">
    <div class="header">
      <h1>Werribee Roadworthy Centre</h1>
      <img src="car.png">
    </div>
    <div class="nav">
      <a href="index.html">Home</a>
      <div class="services">
        <a href="services.html">Services</a>
        <div class="services-content">
          <a href="#">1</a>
          <a href="#">2</a>
          <a href="#">3</a>
        </div>
      </div>
      <div class="projects">
        <a href="projects.html">Projects</a>
        <div class="projects-content">
          <a href="#">1</a>
          <a href="#">2</a>
          <a href="#">3</a>
        </div>
      </div>
      <a href="photos.html">Photo Gallery</a>
      <a href="conact.html">Contact Us</a>

    </div>
    <div class="section">

    </div>
    <div class="aside">

    </div>







  </div>
</body>

希望这会有所帮助:)