如何使这个导航器100%宽?

时间:2016-02-23 17:16:26

标签: html css menu

所以我按照本教程(http://medialoot.com/blog/how-to-create-a-responsive-navigation-menu-using-only-css/)编写了一个响应式菜单。但它不是100%宽,我似乎无法做到这一点。你能告诉我如何解决它吗?

这是代码: https://jsfiddle.net/oyx9r4kh/1/

    <!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS Only Navigation Menu</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="CSS-1.css">
</head>
<body>
        <div class="nav">
            <ul id="menu">
                    <li><a href="#">Home</a></li>
                    <li>
                            <a href="#">About ↓</a>
                            <ul class="hidden">
                                    <li><a href="#">Who We Are</a></li>
                                    <li><a href="#">What We Do</a></li>
                            </ul>
                    </li>
                    <li>
                            <a href="#">Portfolio ↓</a>
                            <ul class="hidden">
                                    <li><a href="#">Photography</a></li>
                                    <li><a href="#">Web & User Interface Design</a></li>
                                    <li><a href="#">Illustration</a></li>
                            </ul>
                    </li>
                    <li><a href="#">News</a></li>
                    <li><a href="#">Contact</a></li>
            </ul>
        </div>

        <div class="hero">
            <img src="http://www.shawcontractgroup.com.au/wp-content/uploads/2013/08/environmental-certifications.jpg">
        </div>

CSS:

body {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;  
}

/*Strip the ul of padding and list styling*/
ul {
    list-style-type:none;
    margin:0;
    padding:0;
    position: absolute;
}

/*Create a horizontal list with spacing*/

.nav {
        width: 100%;
        display: block;
        float: left;
        clear: both;
        background: #2f3036;
        margin-top: 0;
        margin-bottom: 0.5%;
}

.menu {
    width: 100%;
}

li {
    display:inline-block;
    float: left;
    margin-right: 1px;
}

/*Style for menu links*/
li a {
    display:block;
        width: 100%;
    min-width:170px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #fff;
    background: #2f3036;
    text-decoration: none;
}

/*Hover state for top level links*/
li:hover a {
    background: #19c589;
}

/*Style for dropdown links*/
li:hover ul a {
    background: #f3f3f3;
    color: #2f3036;
    height: 40px;
    line-height: 40px;
}

/*Hover state for dropdown links*/
li:hover ul a:hover {
    background: #19c589;
    color: #fff;
}

/*Hide dropdown links until they are needed*/
li ul {
    display: none;
}

/*Make dropdown links vertical*/
li ul li {
    display: block;
    float: none;
        opacity: 0.9;
}

/*Prevent text wrapping*/
li ul li a {
    width: auto;
    min-width: 170px;
    padding: 0 20px;
}

/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
    display: block;
}

.hero {
    margin-top: 50px;
    display: block;
    clear: both;
    float: left;
    width: 100%;
}

.hero img {
    float: left;
    max-width: 100%;
    height: auto;
}

3 个答案:

答案 0 :(得分:0)

这是因为position:absolute;上有ul。这将它从.nav流中取出,使其高度为0.因此,您需要明确地给.nav一个高度,或者将ul设置为不同的位置类型。

答案 1 :(得分:0)

这是您的工作和清洁代码。导航位于中心位置,您可以全屏预览

<强>解释

Position: absolute应该在您的下拉导航

无需一起使用display:inline-blockfloat

始终清除最后一个浮动元素之后的浮动,以便父元素可以正确包装它。

#menu ID设置为max-width并设置margin: auto

建议:您应该使用media-queries进行自适应导航。

&#13;
&#13;
body {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.clear {
  clear: both
}
/*Strip the ul of padding and list styling*/

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
/*Create a horizontal list with spacing*/

.nav-wrap {
  background: #2f3036;
}
.nav {
  width: 100%;
  background: #2f3036;
  margin-top: 0;
  margin-bottom: 0.5%;
}
#menu {
  max-width: 855px;
  margin: auto;
}
li {
  margin-right: 1px;
  float: left;
}
/*Style for menu links*/

li a {
  display: block;
  min-width: 170px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #fff;
  background: #2f3036;
  text-decoration: none;
}
/*Hover state for top level links*/

li:hover a {
  background: #19c589;
}
/*Style for dropdown links*/

li:hover ul a {
  background: #f3f3f3;
  color: #2f3036;
  height: 40px;
  line-height: 40px;
}
/*Hover state for dropdown links*/

li:hover ul a:hover {
  background: #19c589;
  color: #fff;
}
/*Hide dropdown links until they are needed*/

li ul {
  display: none;
  position: absolute;
}
/*Make dropdown links vertical*/

li ul li {
  display: block;
  float: none;
  opacity: 0.9;
}
/*Prevent text wrapping*/

li ul li a {
  width: auto;
  min-width: 170px;
  padding: 0 20px;
}
/*Display the dropdown on hover*/

ul li a:hover + .hidden,
.hidden:hover {
  display: block;
}
.col {
  float: left;
  width: 30%;
  height: auto;
  margin: 1.5% 1.5%;
  border-radius: 10px;
  border: 1px solid #FFF;
}
.col img {
  max-width: 100%;
  height: auto;
}
.col p {
  text-align: center;
  margin: 10px;
  text-shadow: 1px 1px #ffF;
}
div.col:hover {
  background-color: #FFFDD6;
}
&#13;
<body>
  <div class='nav-wrap'>
    <div class="nav">
      <ul id="menu">
        <li><a href="#">Home</a>
        </li>
        <li>
          <a href="#">About ↓</a>
          <ul class="hidden">
            <li><a href="#">Who We Are</a>
            </li>
            <li><a href="#">What We Do</a>
            </li>
          </ul>
        </li>
        <li>
          <a href="#">Products ↓</a>
          <ul class="hidden">
            <li><a href="#">Compounds</a>
            </li>
            <li><a href="#">Oils</a>
            </li>
            <li><a href="#">Illustration</a>
            </li>
          </ul>
        </li>
        <li><a href="#">News</a>
        </li>
        <li><a href="#">Contact</a>
        </li>
        <div class='clear'></div>
      </ul>
    </div>
  </div>

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

答案 2 :(得分:0)

您需要向CSS添加媒体调用,以便在较小的屏幕尺寸上更改ul格式。

/*Responsive Styles*/

@media screen and (max-width : 760px){
    /*Make dropdown links appear inline*/
    ul {
        position: static;
    }
    /*Create vertical spacing*/
    li {
        margin-bottom: 1px;
    }
    /*Make all menu links full width*/
    ul li, li a {
        width: 100%;
    }
}