gpt4 book ai didi

html - CSS flex 盒 : Navigation is not stacking (becoming one column)

转载 作者:行者123 更新时间:2023-11-28 16:58:04 25 4
gpt4 key购买 nike

我想要导航<li>元素像现在一样并排放置。但是当屏幕尺寸为 545 像素或更小时,我希望它们彼此重叠(1 列)。

当达到 545 像素时,导航不会更改为 1 列。

我尝试添加 flex-direction: column@media(max-width: 545px)nav ul

    nav {
padding-left: 0px;
}

nav ul {
display: flex;
}

nav ul li {
list-style: none;
margin-right: 50px;
}

nav ul li a {
color: #8B8B8B;
text-decoration: none;
font-weight: bold;
font-size: 17px;
text-align: center;
}

nav ul li a:hover {
color: #56B8AE;
border-bottom: 1px solid;
/* I WANT A ONE ROW STACK OF THE NAV ONCE THE SCREEN IS 545px or SMALLER */
@media (max-width: 545px) {
nav {
padding-top: 0;
padding-bottom: 30px;
margin-top: 0;
min-height: 100px;
text-align: center;
}
nav ul {
flex-direction: column;
}
nav ul li {
border-top: solid 1px #e6ecf0;
padding-bottom: 5px;
}
<head>
<link rel="stylesheet" href="style.css">
</head>
<header>
<nav>
<ul class="top-nav">
<li class="top-nav-links"><a href="#"><i class="fas fa-home"></i> Home</a></li>
<li class="top-nav-links"><a href="#"><i class="fas fa-store"></i> Shop</a></li>
<li class="top-nav-links"><a href="#"><i class="fas fa-music"></i> Music</a></li>
</ul>
</nav>
<header>

我预计它会成为 545 像素或更小的一列。但它仍然是一排。

最佳答案

您只是忘记在媒体查询之前(然后在媒体查询结束时再次)用 关闭 nav ul li a:hover 规则。如果你添加它,它会起作用:

nav {
padding-left: 0px;
}

nav ul {
display: flex;
}

nav ul li {
list-style: none;
margin-right: 50px;
}

nav ul li a {
color: #8B8B8B;
text-decoration: none;
font-weight: bold;
font-size: 17px;
text-align: center;
}

nav ul li a:hover {
color: #56B8AE;
border-bottom: 1px solid;
}


/* I WANT A ONE ROW STACK OF THE NAV ONCE THE SCREEN IS 545px or SMALLER */

@media (max-width: 545px) {
nav {
padding-top: 0;
padding-bottom: 30px;
margin-top: 0;
min-height: 100px;
text-align: center;
}
nav ul {
flex-direction: column;
}
nav ul li {
border-top: solid 1px #e6ecf0;
padding-bottom: 5px;
}
}
<head>
<link rel="stylesheet" href="style.css">
</head>
<header>
<nav>
<ul class="top-nav">
<li class="top-nav-links"><a href="#"><i class="fas fa-home"></i> Home</a></li>
<li class="top-nav-links"><a href="#"><i class="fas fa-store"></i> Shop</a></li>
<li class="top-nav-links"><a href="#"><i class="fas fa-music"></i> Music</a></li>
</ul>
</nav>
<header>

关于html - CSS flex 盒 : Navigation is not stacking (becoming one column),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55316770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com