内联h3将ul移动到右侧

时间:2015-04-08 14:31:18

标签: html css navigation inline

我希望导航栏居中,文本按原样放置,但是当我添加h3-tag时,导航栏会向右移动。我怎么能这样做?

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  width: 100%;
}
#toptext {
  margin: 5px 0px 0px 10px;
  padding: 0;
  display: inline-block;
  float: left;
  font-style: bold;
  font-size: 2em;
  color: white;
}
.nav ul {
  list-style: none;
  background-color: #444;
  text-align: center;
  padding: 0;
  margin: 0;
}
.nav li {
  font-family: 'Oswald', sans-serif;
  font-size: 1.2em;
  line-height: 40px;
  text-align: left;
}
.nav a {
  text-decoration: none;
  color: #fff;
  display: block;
  padding-left: 15px;
  border-bottom: 1px solid #888;
  transition: .3s background-color;
}
.nav a:hover {
  background-color: #005f5f;
}
.nav a.active {
  background-color: #aaa;
  color: #444;
  cursor: default;
}
/* Sub Menus */

.nav li li {
  font-size: .8em;
}
/*******************************************
Style menu for larger screens

Using 650px (130px each * 5 items), but ems
or other values could be used depending on other factors
********************************************/

@media screen and (min-width: 650px) {
  .nav li {
    width: 130px;
    border-bottom: none;
    height: 50px;
    line-height: 50px;
    font-size: 1em;
    display: inline-block;
    margin-right: -4px;
  }
  .nav a {
    border-bottom: none;
  }
  .nav > ul > li {
    text-align: center;
  }
  .nav > ul > li > a {
    padding-left: 0;
  }
  /* Sub Menus */
  .nav li ul {
    position: absolute;
    display: none;
    width: inherit;
  }
  .nav li:hover ul {
    display: block;
  }
  .nav li ul li {
    display: block;
  }
}
<div class="nav">
  <h3 id="toptext">Text text text text</h3>
  <ul>
    <li class="home"><a class="active" href="#">Home</a>
    </li>
    <li class="about"><a href="About">About</a>
    </li>
    <li><a href="#">Other</a>
      <ul>
        <li><a href="site1.html">Site1</a>
        </li>
        <li><a href="site2.html">Site2</a>
        </li>
      </ul>
    </li>
    <li class="contact"><a href="contact.html">Contact</a>
    </li>
  </ul>
</div>

我使用了导航栏:http://css-snippets.com/drop-down-navigation/

2 个答案:

答案 0 :(得分:0)

文档流程中的任何内容都会影响菜单的位置。所以你必须通过绝对定位将h3从流程中取出来。

#toptext {
    margin: 5px 0px 0px 10px;
    padding: 0;
    font-style: bold;
    font-size: 2em;
    color: white;
    position: absolute;
}

这还有其他问题,但解决了您的初始问题。

&#13;
&#13;
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  width: 100%;
}
#toptext {
  margin: 5px 0px 0px 10px;
  padding: 0;
  /*
  display: inline-block;
  float: left;
    */
  font-style: bold;
  font-size: 2em;
  color: white;
  position: absolute;
}
.nav ul {
  list-style: none;
  background-color: #444;
  text-align: center;
  padding: 0;
  margin: 0;
}
.nav li {
  font-family: 'Oswald', sans-serif;
  font-size: 1.2em;
  line-height: 40px;
  text-align: left;
}
.nav a {
  text-decoration: none;
  color: #fff;
  display: block;
  padding-left: 15px;
  border-bottom: 1px solid #888;
  transition: .3s background-color;
}
.nav a:hover {
  background-color: #005f5f;
}
.nav a.active {
  background-color: #aaa;
  color: #444;
  cursor: default;
}
/* Sub Menus */

.nav li li {
  font-size: .8em;
}
/*******************************************
Style menu for larger screens

Using 650px (130px each * 5 items), but ems
or other values could be used depending on other factors
********************************************/

@media screen and (min-width: 650px) {
  .nav li {
    width: 130px;
    border-bottom: none;
    height: 50px;
    line-height: 50px;
    font-size: 1em;
    display: inline-block;
    margin-right: -4px;
  }
  .nav a {
    border-bottom: none;
  }
  .nav > ul > li {
    text-align: center;
  }
  .nav > ul > li > a {
    padding-left: 0;
  }
  /* Sub Menus */
  .nav li ul {
    position: absolute;
    display: none;
    width: inherit;
  }
  .nav li:hover ul {
    display: block;
  }
  .nav li ul li {
    display: block;
  }
}
&#13;
<div class="nav">
  <h3 id="toptext">Text text text text</h3>

  <ul>
    <li class="home"><a class="active" href="#">Home</a>

    </li>
    <li class="about"><a href="About">About</a>

    </li>
    <li><a href="#">Other</a>

      <ul>
        <li><a href="site1.html">Site1</a>

        </li>
        <li><a href="site2.html">Site2</a>

        </li>
      </ul>
    </li>
    <li class="contact"><a href="contact.html">Contact</a>

    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

因为你的h3在nav div里面。尝试使h3绝对定位并使.nav类相对定位。