css 使导航居中

toe95027  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(126)

我想主要导航,其中包括链接要在中间的手机媒体我使用的保证金:auto;但那不起作用。我在悬停时使用::before来获得一些效果。
下面是HTML代码:

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

下面是CSS代码:

/* 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 */

下面是我得到的(https://i.stack.imgur.com/B8kci.png
我想要的(https://i.stack.imgur.com/rgxpS.png

dbf7pr2w

dbf7pr2w1#

您需要将容器从 space-between 设置为 center,因此在媒体查询中:

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

}

相关问题