Home > Software design >  Nav bar menu Responsive
Nav bar menu Responsive

Time:09-30

Hello guys i followed a nav bar tutorial on youtube and it worked well except when I reduced the screen size to test for mobile view the nav bar didn't turn into a toggled menu, this is my whole HTML CSS code, please tell me whats the problem and how to fix it.the tutorial owner told me to see the @media tag but I'm a newbie idk what that even means

    <!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>

<style> 
*{
    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):
    }
}

</styles>

</html>

 

CodePudding user response:

Actually what you trying to achieve is a responsive Navbar without Javascript using only HTML and CSS. So their so many mistakes in your provided code I have fix everything, now you can use this code to your build.

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>

CodePudding user response:

You need to specify what it should be like in smaller resolutions and you should do that with media queries. Check the following link https://www.w3schools.com/css/css_rwd_mediaqueries.asp

  • Related