无法将下拉菜单与“服务”链接对齐。每当我悬停到“服务”链接时,它就会停留在左侧。我曾尝试在nav ul ul上使用函数"left“,它在某种程度上起到了作用,但每当我最小化窗口时,它都没有响应。它只适用于我更改的窗口大小。在这一点上,我对这一点感到困惑。

/* Basic */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
font-family: 'Acme', sans-serif;
background: white;
line-height: 1.6;
}
ul{
list-style: none;
}
a{
text-decoration: none;
}
/* Navbar */
.navbar{
display: flex;
background: #004C66;
align-items: center;
justify-content: space-between;
width: 100%;
position: fixed;
padding: 0 30px;
}
/* To Keep Control of the Logo */
.navbar .logo img{
width: 100%;
margin-top: 10px;
}
/* To Make the Desktop Nav Buttons Inline */
.navbar ul li{
display: inline;
}
.navbar ul li .nav-link{
padding: 10px 25px;
color: #fff;
font-size: 30px;
transition-duration: 500ms;
}
.navbar .nav-link:hover{
color: black;
}
/* NAVBAR DROP DOWNS */
/* Hide Dropdowns By Default */
nav ul ul {
display: none;
position: absolute;
top: auto; /* the height of the main nav */
text-align: center;
justify-items: center;
border-radius: 10%;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:flex;
background-color: teal;
flex-direction: column;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:auto;
top: 0px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers */
nav ul ul ul li {
position: relative;
}<header class="navbar">
<a href="" class="logo"><img src="/img/logoS.png" alt=""></a>
<nav class="desktop-view">
<ul>
<li><a class="nav-link" href="#">Home</a></li>
<li><a class="nav-link services" href="#">Services</a>
<!-- First Tier Drop Down -->
<ul>
<li><a class="nav-link" href="#">Tile</a></li>
<li><a class="nav-link"href="#">Mexican Tile</a></li>
<li><a class="nav-link"href="#">Marble Floor</a></li>
<li><a class="nav-link"href="#">Shower Regrout</a></li>
</ul>
</li>
<li><a class="nav-link" href="#">Contact</a></li>
</ul>
</nav>
</header>
发布于 2020-10-09 15:00:24
将.navbar ul li position设置为relative
然后使用left: 50%和transform: translateX(-50%)将下拉菜单移到中间
/* Basic */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
font-family: 'Acme', sans-serif;
background: white;
line-height: 1.6;
}
ul{
list-style: none;
}
a{
text-decoration: none;
}
/* Navbar */
.navbar{
display: flex;
background: #004C66;
align-items: center;
justify-content: space-between;
width: 100%;
position: fixed;
padding: 0 30px;
}
/* To Keep Control of the Logo */
.navbar .logo img{
width: 100%;
margin-top: 10px;
}
/* To Make the Desktop Nav Buttons Inline */
.navbar ul li{
display: inline;
position: relative;
}
.navbar ul li .nav-link{
padding: 10px 25px;
color: #fff;
font-size: 30px;
transition-duration: 500ms;
}
.navbar .nav-link:hover{
color: black;
}
/* NAVBAR DROP DOWNS */
/* Hide Dropdowns By Default */
nav ul ul {
display: none;
position: absolute;
text-align: center;
border-radius: 10%;
left: 50%;
width: 300px;
transform: translateX(-50%);
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:flex;
background-color: teal;
flex-direction: column;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:auto;
top: 0px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers */
nav ul ul ul li {
position: relative;
}<header class="navbar">
<a href="" class="logo"><img src="/img/logoS.png" alt=""></a>
<nav class="desktop-view">
<ul>
<li><a class="nav-link" href="#">Home</a></li>
<li><a class="nav-link services" href="#">Services</a>
<!-- First Tier Drop Down -->
<ul>
<li><a class="nav-link" href="#">Tile</a></li>
<li><a class="nav-link"href="#">Mexican Tile</a></li>
<li><a class="nav-link"href="#">Marble Floor</a></li>
<li><a class="nav-link"href="#">Shower Regrout</a></li>
</ul>
</li>
<li><a class="nav-link" href="#">Contact</a></li>
</ul>
</nav>
</header>
发布于 2020-10-09 14:36:47
绝对定位的元素相对于第一个定位的父元素进行定位,因此具有position: absolute的导航ul ul将相对于返回的具有位置设置的第一个元素进行定位。
在本例中,要求下拉列表与其父li对齐,因此将该元素设置为position: relative
发布于 2020-10-09 15:36:14
如果你想要下拉菜单的这种外观,那么在你的CSS代码中进行以下更改:
ul{
list-style: none;
display: flex;
position: relative;
}https://stackoverflow.com/questions/64274492
复制相似问题