Search code examples
htmlcssmargincenteringauto

Centering The Nav in the middle


I want to the main-nav that includes the links to be in the middle in the phone media I use the margin: auto; but that didn't work. And I use the ::before for some effects when hovering.

Here is the HTML Code:

        <div class="header">
            <div class="container">
                <a href="#" class="logo">Abuissa</a>
                <nav>
                    <ul class="main-nav">
                        <li><a href="#article">Article</a></li>
                        <li><a href="#gallery">Gallery</a></li>
                        <li><a href="#feauters">Feauters</a></li>
                        <li><a href="#">Other Skills</a></li>
                    </ul>
                </nav>
            </div>
        </div>

Here is the CSS Code:

/* Start Header */
.header {
    background-color: white;
    box-shadow: 0 0 10px #ddd;
    -webkit-box-shadow: 0 0 10px #ddd;
    -mox-box-shadow: 0 0 10px #ddd;
    position:relative; 
}
.header .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    position: relative;
}
.header .logo {
    color: var(--main-color);
    font-size: 26px;
    font-weight: bold;
    height: 72px;
    display: flex;
    justify-content: center;
    align-items: center;
}
@media (max-width:767px) {
    .header .logo {
        width: 100%;
        height: 50px;
    }
}
.header .main-nav {
    display: flex;
}
@media (max-width: 767px) {
    .heade .main-nav {
        margin: auto;
    }
}
.header .main-nav > li > a {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 72px;
    position: relative;
    color: black;
    padding: 30px;
    transition: var(--main-transition);
    overflow: hidden;
}
@media (max-width:767px) {
    .header .main-nav > li > a {
        padding: 10px;
        font-size: 14px;
        height: 40px;
    }
}
.header .main-nav > li > a::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 3px;
    background-color: var(--main-color);
    top: 0;
    left: -100%;
    transition: var(--main-transition);
}
.header .main-nav > li > a:hover {
    color: var(--main-color);
    background-color: #fafafa;
}
.header .main-nav > li > a:hover::before {
    left: 0%;
}
/* End Header */

Here what I got (https://i.sstatic.net/B8kci.png)

What I want (https://i.sstatic.net/rgxpS.png)


Solution

  • You need to set the container from space-between to center. So in media query:

    @media (max-width: 767px) {
    .header .main-nav {
        margin: auto;
    }
    .header .container {
    justify-content: center;
    }
    

    }