首页 > 解决方案 > 导航栏菜单响应式

问题描述

大家好,我遵循了 youtube 上的导航栏教程,它运行良好,除了当我减小屏幕尺寸以测试移动视图时,导航栏没有变成切换菜单,这是我的整个 HTML CSS 代码,请告诉我什么问题以及如何解决它。教程所有者告诉我查看@media 标签,但我是新手 idk 这甚至意味着什么

    *{
        padding:0;
        margin: 0;
        text-decloration: none;
        list-style: none;
        box-sizing: border-box;
    
    }
    
    body{
        font-family:montserrat;
    
    }
    nav{
        background: #0082e6;
        height: 80px;
        width: 100%;
    }
    footer{
        opacity: 0.5;
    }
    
    label.logo{
        color: white;
        font-size: 35px;
    
        line-height: 80px;
        padding: 0 100px;
        font-weight: bold;
    }
    
    nav ul{
        float: right;
        margin-right: 20px;
    }
    nav ul li{
        display: inline-block;
        line-height: 80px;
        margin: 0 5px;
    }
    nav ul li a{
        color:black;
        font-size: 17px;
        text-transform: uppercase;
        padding: 7px 13px;
        border-radius: 3px;
    }
    
    a.active,a:hover{
        background: #1b9bff;
        transition: .5s;
    }
    
    .checkbtn{
        font-size: 30px;
        color: white;
        float: right;
        line-height: 80px;
        margin-right: 40px;
        cursor: pointer;
        display: none;
    }
    #check{
        display: none;
    }
    @media (max-width: 952px){
        label.logo{
            font-size: 30px;
            padding-left: 50px;
        }
        nav ul li a{
            font-size: 16px;
        }
    }
    
    @media (max-width: 858px){
        .checkbtn{
            display:block;
        }
        ul{
            position: fixed;
            width: 100%;
            height: 100vh;
            top: 80px;
            left: -100%;
            text-align: center;
            transition: all .5s;
        }
        nav ul li{
            display: block;
            margin: 50px 0;
            line-height: 30px;
        }
        nav ul li a{
            font-size: 20px;
        }
        a:hover,a.active{
            background: none;
            color: #0082e6;
    
        }
        #check:checked ~ ul{
            left: 0;
        }
        section{
            background: url(bgl.jpg) no-repeat
            background-size: coover;
            height: calc(100vh - 80px):
        }
    }
 

    
        <!Doctype HTML> 
    <html> 
    <head> 
    <footer> </footer>
    <title> 
    EgyptPost/Latest news about Egypt
    </title>
    
    
    
    </head>
    
    
    
    <body>
    <nav> 
    <input type="checkbox" id"check">
    <label for="check" class="checkbtn">
    
    <i class="fas fa-bars" > </i>
    
    
    </label>
    <label class="logo">EgyptPost</label>
    
    <ul>
    <li><a class="active" href="#">Home </a> </li>
    <li><a href="#">Health</a> </li>
    <li><a href="#">News </a> </li>
    <li><a href="#">About Us </a> </li>
    <li><a href="#">Contact Us/FeedBack </a> </li>
    
    
    </ul>
    
    
    </nav>
    
    <section>
    
    
    
    </section>
    
    
    
    <img  src="WordpressLogo.jpg" width="70px" height="70px" alt="EgyptPost logo">
    
    <h1> Egypt is expected to be the 7th largest economy in the world said the minister of finance Mohammed Maait.</h1>
    <img src="download.jpg" width="600px" height="100px" alt="photo of egypt's 2030 vision">
    <p>Egypt is a <b>stong growing economy</b> that has an overall ranking of 33th in the world, and the first in africa, egypt will outpreform countires nearby and will dominate</p> 
    
    
    <footer>End of the article </footer>
    
    </body>
    
    </html>
    

标签: htmlcss

解决方案


实际上,您想要实现的是一个没有 Javascript 的响应式导航栏,只使用 HTML 和 CSS。因此,他们在您提供的代码中存在如此多的错误,我已经修复了所有问题,现在您可以将此代码用于您的构建。

body {
    padding: 0;
    margin: 0;
}
nav {
    position: fixed;
    z-index: 10;
    left: 0;
    right: 0;
    top: 0;
    font-family: 'Montserrat', sans-serif;
    padding: 0 5%;
    height: 50px;
    background-color: #3e65da;
}
nav .logo {
    float: left;
    width: 40%;
    height: 100%;
    display: flex;
    align-items: center;
    font-size: 24px;
    color: #fff;
}
nav .links {
    float: right;
    padding: 0;
    margin: 0;
    width: 60%;
    height: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
}
nav .links li {
    list-style: none;
}
nav .links a {
    display: block;
    padding: 1em;
    font-size: 16px;
    font-weight: bold;
    color: #fff;
    text-decoration: none;
}
#nav-toggle {
    position: absolute;
    top: -100px;
}
nav .icon-burger {
    display: none;
    position: absolute;
    right: 5%;
    top: 50%;
    transform: translateY(-50%);
}
nav .icon-burger .line {
    width: 30px;
    height: 3px;
    background-color: #fff;
    margin: 5px;
    border-radius: 3px;
    cursor:pointer;
    transition: all .3s ease-in-out;
}
section{
    margin:70px 20px 30px 20px;;
    font-size:15px;
    text-align:center;
}
.wrap{
    display:flex;
}
.wrap p{
    flex:2;
    padding:0px 20px;
    font-size:20px;
    text-align:justify;
}
@media screen and (max-width: 768px) {
    nav .logo {
        width: auto;
        justify-content: center;
    }
    nav .links {
        float: none;
        position: fixed;
        z-index: 9;
        left: 0;
        right: 0;
        top: 50px;
        bottom: 100%;
        width: auto;
        height: auto;
        flex-direction: column;
        justify-content: space-evenly;
        background-color: rgba(0,0,0,1);
        overflow: hidden;
        box-sizing: border-box;
        transition: all .5s ease-in-out;
    }
    nav .links a {
        font-size: 20px;
    }
    nav :checked ~ .links {
        bottom: 0;
    }
    nav .icon-burger {
        display: block;
    }
    nav :checked ~ .icon-burger .line:nth-child(1) {
        transform: translateY(10px) rotate(225deg);
    }
    nav :checked ~ .icon-burger .line:nth-child(3) {
        transform: translateY(-10px) rotate(-225deg);
    }
    nav :checked ~ .icon-burger .line:nth-child(2) {
        opacity: 0;
    }
    
}
<nav>
    <input id="nav-toggle" type="checkbox">
    <div class="logo"><strong>EgyptPost</strong></div>
    <ul class="links">
        <li><a href="#home">Home</a></li>
        <li><a href="#about">Health</a></li>
        <li><a href="#work">News</a></li>
        <li><a href="#projects">About Us</a></li>
        <li><a href="#contact">Contact Us/FeedBack</a></li>
    </ul>
    <label for="nav-toggle" class="icon-burger">
        <div class="line"></div>
        <div class="line"></div>
        <div class="line"></div>
    </label>
</nav>

<section>
    <h1> Egypt is expected to be the 7th largest economy in the world said the minister of finance Mohammed Maait.</h1>
    <div class="wrap">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSzEq1WfAEiCsDvkQshIy0-P-0OL2AFfwL_-A&usqp=CAU" alt="" />
        <p>Egypt is a <b>stong growing economy</b> that has an overall ranking of 33th in the world, and the first in africa, egypt will outpreform countires nearby and will dominate</p> 
    </div>
</section>


推荐阅读