将导航居中对齐。

huangapple go评论78阅读模式
英文:

Centering The Nav in the middle

问题

我想要主导航栏在手机媒体中居中显示,我使用margin: 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代码:

/* 开始头部样式 */
.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) {
    .header .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%;
}
/* 结束头部样式 */

你期望的效果如下图所示:链接到图片

英文:

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:

        &lt;div class=&quot;header&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;a href=&quot;#&quot; class=&quot;logo&quot;&gt;Abuissa&lt;/a&gt;
&lt;nav&gt;
&lt;ul class=&quot;main-nav&quot;&gt;
&lt;li&gt;&lt;a href=&quot;#article&quot;&gt;Article&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#gallery&quot;&gt;Gallery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#feauters&quot;&gt;Feauters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Other Skills&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;/div&gt;

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 &gt; li &gt; 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 &gt; li &gt; a {
padding: 10px;
font-size: 14px;
height: 40px;
}
}
.header .main-nav &gt; li &gt; a::before {
content: &quot;&quot;;
position: absolute;
width: 100%;
height: 3px;
background-color: var(--main-color);
top: 0;
left: -100%;
transition: var(--main-transition);
}
.header .main-nav &gt; li &gt; a:hover {
color: var(--main-color);
background-color: #fafafa;
}
.header .main-nav &gt; li &gt; a:hover::before {
left: 0%;
}
/* End Header */

Here what I got (https://i.stack.imgur.com/B8kci.png)

What I want (https://i.stack.imgur.com/rgxpS.png)

答案1

得分: 1

将容器的设置从 space-between 更改为 center。在媒体查询中:

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

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

}

huangapple
  • 本文由 发表于 2023年2月27日 11:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定