响应式网站不是真的响应式

时间:2018-09-15 12:45:17

标签: responsive

我刚开始进行Web开发,并使用徽标和导航栏制作了一个简单的网站标题。问题是网站应该响应并更改布局(将其大小调整为580px),以两列显示菜单,但事实并非如此。当网站完全扩展时,应该在同一行显示菜单,但菜单也很混乱。您可以看一下代码,然后告诉我什么地方错了吗?

您在这里:

HTML:

<!DOCTYPE html>
<html lange="en">
<head>
<meta charset="utf-8" />
<title>Painting with responsive menu</title>    
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel = "stylesheet"
   type = "text/css"
   href = "stylesheet6.css" />
</head>

<body>
    <div class="wrapper">
        <header>
            <a href="#"><img src="http://www.w3newbie.com/wp-content/uploads/paintinglogo.png"></a>
        </header>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Business</a></li>
                <li><a href="#">Photos</a></li>
                <li><a href="#">Video</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </div>
</body> 

</html>

CSS:

.wrapper{
    margin: 0 auto;
    max-width: 1220px;
    width: 98%;
    background-color: #2F3038;
    padding: 2%;
    border-radius: 2px;
}
header{
    width: 98%;
    min-height: 100px;
    padding: 5px;
    margin-bottom: -40px;
    text-align: center;
}
img{
    text-align: center;
    max-width: 100%;
    height: auto;
    width: auto;
}
nav {
    width: 90%;
    margin: 5% auto;
    overflow: hidden;
}
nav ul{
    list-style: none;
    overflow: hidden;   

}
nav ul li a {
    background-color: #737373;
    border: 1px solid #2F3038;
    color: #2F3038;
    display: block;
    float: left;
    font: 16px, Arial, Helvitica;
    padding: 10px;
    text-align: center;
    text-decoration: none;
    width: 16.5%;

    -webkit-transition: background 0.5s ease;
    -moz-transition: background 0.5s ease;
    -o-transition: background 0.5s ease;
    -ms-transition: background 0.5s ease;
    transition: background 0.5s ease;
}
nav li a:hover{
    background: #DADADA;
}

/*-------------------Media Queries------------*/

@media only screen and (max-width: 580px){
    nav li a{
        width: 50%;
        font: 13px Arial, Helvitica;
        padding-top: 12px;
        padding-bottom: 12px;
        border-bottom: 1px solid #2F3038;

    }
    nav li:nth-child(even) a{
        border-right: none;
    }
}

谢谢你们!

1 个答案:

答案 0 :(得分:0)

您有几个错误:

  • 不使用box-sizing: border-box会导致nav ul li a的宽度计算错误,因为宽度 16.5%不包含填充,因此会强制换行
  • 您使用的工具不正确(浮动)-更好地将Flexbox与包装一起使用

  *
  {
    box-sizing: border-box;
  }
   .wrapper{
    margin: 0 auto;
    max-width: 1220px;
    width: 98%;
    background-color: #2F3038;
    padding: 2%;
    border-radius: 2px;
  }
  header{
    width: 98%;
    min-height: 100px;
    padding: 5px;
    margin-bottom: -40px;
    text-align: center;
  }
  img{
    text-align: center;
    max-width: 100%;
    height: auto;
    width: auto;
  }
  nav {
    width: 90%;
    margin: 5% auto;
    overflow: hidden;
}
nav ul{
    list-style: none;
    overflow: hidden;   
    padding-left: 0;
    display: flex;
    flex-wrap: wrap;
}
nav ul li
{
  flex: 1 1 0;
  display: block;
}
nav ul li a {
    background-color: #737373;
    border: 1px solid #2F3038;
    color: #2F3038;
    display: block;
    font: 16px, Arial, Helvitica;
    padding: 10px;
    text-align: center;
    text-decoration: none;

    -webkit-transition: background 0.5s ease;
    -moz-transition: background 0.5s ease;
    -o-transition: background 0.5s ease;
    -ms-transition: background 0.5s ease;
    transition: background 0.5s ease;
}
nav li a:hover{
    background: #DADADA;
}

@media only screen and (max-width: 580px){
    nav ul li
    {
      flex: 0 0 33.333333%;
    }
    nav li a{
        font: 13px Arial, Helvitica;
        padding-top: 12px;
        padding-bottom: 12px;
        border-bottom: 1px solid #2F3038;

    }
    nav li:nth-child(even) a{
        border-right: none;
    }
}

@media only screen and (max-width: 325px){
    nav ul li
    {
      flex: 0 0 50%;
    }
}