Thæ
Thæ

Reputation: 57

Navigation bar problem - when the browser reaches 768px width, list items get mixed up as seen in the provided snippet

This code doesn't work properly when I narrow down the browser. I set a line on 768px. When I narrow the browser about that width, the list items get mixed up. Where is my mistake and why does that happen.

nav {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
}

nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    width: 100%;
    background-color: darkslateblue;
}

nav ul li {
    display: inline;
    float: left;
    min-width: 10%;
    padding-right: 5px;
    width: auto;
}

nav ul li img {
    display: block;
    float: left;
    width: 39px !important;
    margin: 0px;
}

nav ul li a {
    display: block;
    color: azure;
    text-decoration: none;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    padding: 8px;
    transition: 0.3s;
}

nav ul li a:hover {
    background-color: darkorange;
    color: darkslategrey;
}

.dropdown {
    position: inherit;
    display: inline-block;
    margin: -1px;
}

.dropdown button {
    background-color: darkslateblue;
    color: white;
    font-size: 20px;
    padding: 8px;
    text-align: center;
    width: 100%;
    float: left;
    border: none;
    cursor: pointer;
    transition: 0.3s;
}

.dropdown span {
    background-color: darkslateblue;
    color: azure;
    font-size: 20px;
    padding: 8px;
    text-align: center;
    width: 100%;
    display: inline-block;
    float: left;
    border: none;
    cursor: default;
    transition: 0.3s;
}

.dropdown div {
    display: none;
    position: absolute;
    background-color: darkslateblue;
    min-width: 10%;
    box-shadow: : 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    transition: 0.1s;
}

.dropdown div a {
    color:  white;
    font-size: 20px;
    padding: 8px;
    text-decoration: none;
    display: block;
}

.dropdown div a:hover {
    background-color: orange;
}

.dropdown:hover div {
    display: block;
}

.dropdown:hover button {
    background-color: darkorange;
}

.dropdown:hover span{
    background-color: darkorange;
}

@media (max-width: 768px) {
    nav ul li {
        min-width: 15% !important;
        max-width: 25%;
    }
}
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="navbar.css">
        <script src="https://use.fontawesome.com/70cfac45e9.js"></script>
    </head>
    <body>
        <nav>
            <ul>
                <li style="min-width: 5%;"><a href="#"><i class="fa fa-home"></i></a></li>
                <li><a href="#">Link-1</a></li>
                <li><a href="#">link-2</a></li>
                <li><a href="#">link-3</a></li>
                <li><a href="#">link-4</a></li>
                <li class="dropdown">
                    <button>dr-menu</button>
                    <div>
                        <a href="#">dr-link1</a>
                        <a href="#">dr-link2</a>
                    </div>
                </li>
                <li><a href="#">link-5</a></li>
                <li class="dropdown">
                    <span>dr-menu2</span>
                    <div>
                        <a href="#">dr-link1</a>
                        <a href="#">dr-link2</a>
                    </div>
                </li>
            </ul>
        </nav>
    </body>
</html>

As you can see in the code above, max width is 768px for list items and my list items mix when when I narrow down the viewport of the browser to or below 768px.

Upvotes: 1

Views: 50

Answers (1)

Nikola Pavicevic
Nikola Pavicevic

Reputation: 23490

In that case you don't need @media query. You can solve it with flexbox. Pls look at snippet below :

nav {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
}

nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    width: 100%;
    background-color: darkslateblue;
    display: flex;
    flex-wrap: wrap;
}

nav ul li {
    display: flex;
    flex-direction: row;
    padding-right: 5px;
    width: auto;
}

nav ul li img {
    display: block;
    float: left;
    width: 39px !important;
    margin: 0px;
}

nav ul li a {
    display: block;
    color: azure;
    text-decoration: none;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    padding: 8px;
    transition: 0.3s;
}

nav ul li a:hover {
    background-color: darkorange;
    color: darkslategrey;
}

.dropdown {
    position: inherit;
    display: inline-block;
   /* margin: -1px;*/
}

.dropdown button {
    background-color: darkslateblue;
    color: white;
    font-size: 20px;
    padding: 8px;
    text-align: center;
    width: 100%;
    float: left;
    border: none;
    cursor: pointer;
    transition: 0.3s;
}

.dropdown span {
    background-color: darkslateblue;
    color: azure;
    font-size: 20px;
    padding: 8px;
    text-align: center;
    width: 100%;
    display: inline-block;
    float: left;
    border: none;
    cursor: default;
    transition: 0.3s;
}

.dropdown div {
    display: none;
    position: absolute;
    background-color: darkslateblue;
    min-width: 10%;
    box-shadow: : 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    transition: 0.1s;
}

.dropdown div a {
    color:  white;
    font-size: 20px;
    padding: 8px;
    text-decoration: none;
    display: block;
}

.dropdown div a:hover {
    background-color: orange;
}

.dropdown:hover div {
    display: block;
}

.dropdown:hover button {
    background-color: darkorange;
}

.dropdown:hover span{
    background-color: darkorange;
}

/*@media (max-width: 768px) {
    nav ul li {
        min-width: 15% !important;
        max-width: 25%;
    }
}*/
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="navbar.css">
        <script src="https://use.fontawesome.com/70cfac45e9.js"></script>
    </head>
    <body>
        <nav>
            <ul>
                <li style="min-width: 5%;"><a href="#"><i class="fa fa-home"></i></a></li>
                <li><a href="#">Link-1</a></li>
                <li><a href="#">link-2</a></li>
                <li><a href="#">link-3</a></li>
                <li><a href="#">link-4</a></li>
                <li class="dropdown">
                    <button>dr-menu</button>
                    <div>
                        <a href="#">dr-link1</a>
                        <a href="#">dr-link2</a>
                    </div>
                </li>
                <li><a href="#">link-5</a></li>
                <li class="dropdown">
                    <span>dr-menu2</span>
                    <div>
                        <a href="#">dr-link1</a>
                        <a href="#">dr-link2</a>
                    </div>
                </li>
            </ul>
        </nav>
    </body>
</html>

Upvotes: 2

Related Questions