浮动导航的问题

时间:2017-09-28 03:45:11

标签: html css web

我遇到浮动导航的一些问题。从下面的第一张图片中可以看出,我在代码中使用位置,结果是当我缩放浏览器时导航浮动在徽标上方。但我不希望它是那样的。我希望每当我缩放浏览器时,导航和徽标都会分开(就像第二张图像一样)。我该怎么办?

P / s:如果不使用浮动,还有其他方法吗?

先谢谢了! enter image description here

enter image description here

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css">
    <style>
        .wrapper{
            max-width: 1600px;
            margin: 0 auto;
        }

        header{
            background-color: lightgray;
            overflow: hidden;
            padding: 20px;
            position: relative;
        }

        header .logo{
            float: left;
        }

        header nav{
            float: right;
            position: absolute;
        }

        header li{
            float: left;
            margin: 0px 5px 0px 5px;
        }

        header a{
            text-decoration: none;
            font-weight: bold;
            color: white;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <header>
            <div class="logo">
                <img src="images\logo.png" alt="logo">
            </div>

            <nav>
                <ul>
                    <li><a href="#">HOMEPAGE</a></li>
                    <li><a href="#">INTRODUCTION</a></li>
                    <li><a href="#">PRODUCTS</a></li>
                    <li><a href="#">PRICING</a></li>
                    <li><a href="#">CONTACT</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>
</html>

5 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。这是一个:

将此添加到您的风格中:

nav {margin-left: 50px;}

答案 1 :(得分:0)

请试试这个。我也解释了其他方法。看到我的回答。

.wrapper{
  max-width: 1600px;
  margin: 0 auto;
}

header{
  background-color: lightgray;
  overflow: hidden;
  padding: 20px;
  position: relative;
}

header .logo{
  float: left;
}

header nav {
  float: right;
  position: relative;
  margin-top: 35px;  
}

header li{
  float: left;
  margin: 0px 5px 0px 5px;
}

header a{
  text-decoration: none;
  font-weight: bold;
  color: white;
  padding: 10px;
}
img {
  height: auto;
  max-height: 100%;
  max-width: 100%;
  width: auto;
}
@media screen and (max-width:1169px){
header .logo {
  width: 19%;
}
}
@media screen and (max-width:767px){
header .logo {
  width: 15%;
}
header a { 
  padding: 5px; 
  font-size: 14px;
}
header nav { 
  margin-top: 15px;  
}
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css">    
</head>
<body>
    <div class="wrapper">
        <header>
            <div class="logo">
                <img src="https://dummyimage.com/350x183/000/fff&text=logo" alt="logo">
            </div>

            <nav>
                <ul>
                    <li><a href="#">HOMEPAGE</a></li>
                    <li><a href="#">INTRODUCTION</a></li>
                    <li><a href="#">PRODUCTS</a></li>
                    <li><a href="#">PRICING</a></li>
                    <li><a href="#">CONTACT</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>
</html>

答案 2 :(得分:0)

以下是导航浮动的一个版本:jsfiddle

CSS:

        .wrapper{
            max-width: 1600px;
            margin: 0 auto;
        }

        header{
            background-color: lightgray;
            overflow: hidden;
            padding: 20px;
            position: relative;
        }

        header .logo{
            float: left;
            margin-left: 50px;
        }
  /* width is set to 80% and nav floats right, no pos absolute */
        header nav{
            float: right;
            width: 80%;
        }

        header li{
            float: left;
            margin: 0px 5px 0px 5px;
        }

        header a{
            text-decoration: none;
            font-weight: bold;
            color: white;
            padding: 10px;
        }

答案 3 :(得分:0)

这是使用.wrapper{ max-width: 1600px; margin: 0 auto; } header{ background-color: lightgray; overflow: hidden; padding: 20px; position: relative; } header { display: flex !important; justify-content: space-between; } .logo img { height: auto; max-height: 100%; max-width: 100%; width: auto; } header li{ float: left; margin: 0px 5px 0px 5px; } header a{ text-decoration: none; font-weight: bold; color: white; padding: 10px; } @media screen and (max-width:1024px){ .logo { width: 20%; } }的其他方法。

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css">    
</head>
<body>
    <div class="wrapper">
        <header>
            <div class="logo">
                <img src="https://dummyimage.com/250x150/000/fff&text=logo" alt="logo">
            </div>

            <nav>
                <ul>
                    <li><a href="#">HOMEPAGE</a></li>
                    <li><a href="#">INTRODUCTION</a></li>
                    <li><a href="#">PRODUCTS</a></li>
                    <li><a href="#">PRICING</a></li>
                    <li><a href="#">CONTACT</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>
</html>
&#13;
android:gravity="start"
&#13;
&#13;
&#13;

答案 4 :(得分:0)

试试这个。您可能需要稍微调整宽度。您也可以为徽标和导航添加max-width和min-with。希望这是你正在寻找的。

&#13;
&#13;
.wrapper{
                max-width: 1600px;
                margin: 0 auto;
            }

            header{
                background-color: lightgray;
                overflow: hidden;
                padding: 20px;
                position: relative;
            }
/* add the width and margin to your logo class*/
            header .logo{
                float: left;
                margin-right:2%;
                width:26%;
            }
/* make sure your image has a width */
            header .logo img{
                width:100%;
            }
            
/* add the width to your nav class */
            header nav{
                float: right;
                width:70%;
                padding-top:5%;
            }

            header li{
                float: left;
                margin: 0px 5px 0px 5px;
            }

            header a{
                text-decoration: none;
                font-weight: bold;
                color: white;
                padding: 10px;
            }
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css">    
</head>
<body>
    <div class="wrapper">
        <header>
            <div class="logo">
                <img src="https://dummyimage.com/350x183/000/fff&text=logo" alt="logo">
            </div>

            <nav>
                <ul>
                    <li><a href="#">HOMEPAGE</a></li>
                    <li><a href="#">INTRODUCTION</a></li>
                    <li><a href="#">PRODUCTS</a></li>
                    <li><a href="#">PRICING</a></li>
                    <li><a href="#">CONTACT</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>
</html>
&#13;
&#13;
&#13;