gpt4 book ai didi

html - 导航子菜单链接列出容器的全宽,而不是它们嵌套的菜单项的宽度

转载 作者:行者123 更新时间:2023-11-28 08:07:39 25 4
gpt4 key购买 nike

我正在我当前的简单导航菜单栏上创建子菜单。当我将鼠标悬停在“治疗”的导航链接上时,子菜单项很好地下降并位于上述链接的中央,但背景颜色分布在整个容器中(与完整导航栏的宽度相同)。我想要的是子菜单以与上面的“治疗”链接相同的宽度下拉到下方。我在下面添加了代码片段,供人们查看我哪里出错了。

@charset"UTF-8";

nav {
font-size: 20px;
text-transform: uppercase;
background-color: #1C75BC;
color: white;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
}
nav ul:after {
content: "";
clear: both;
display: block;
}
nav li {
float: left;
width: 20%;
}
nav a:link,
nav a:visited {
display: block;
color: white;
background-color: #1C75BC;
text-align: center;
padding: 8px;
text-decoration: none;
transition: all 0.3s ease-in;
-webkit-transition: all 0.3s ease-in;
/* Safari */
-moz-transition: all 0.3s ease-in;
/* Firefox */
-o-transition: all 0.3s ease-in;
/* Opera */
}
nav a:hover {
background-color: #4dacff;
}
nav ul ul {
display: none;
font-size: 10px;
}
nav ul li:hover > ul {
display: block;
}
nav ul ul li {
border-radius: 0px;
padding: 0;
position: absolute;
}
nav ul ul li {
float: none;
position: relative;
width: auto;
}
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" type="text/css" href="testcss.css">
</head>

<body>
<nav>
<ul>
<li><a href="http://www.leftyslaser.co.uk">Home</a>
</li>
<li><a href="http://www.leftyslaser.co.uk/faq.html">FAQ</a>
</li>
<li><a href="http://www.leftyslaser.co.uk/portfolio.html">Portfolio</a>
</li>
<li><a href="#">Treatments</a>
<ul>
<li><a href="#">Laser Tattoo Removal</a>
</li>
<li><a href="#">Carbon Facial Skin Rejuvenation</a>
</li>
</ul>
</li>
<li><a href="http://www.leftyslaser.co.uk/about.html">About Us</a>
</li>
</ul>
</nav>
</body>

最佳答案

Demo

使用inline-block 代替block。使用 display: block 使元素显示在整个浏览器中。

nav ul li:hover > ul {
display: inline-block;
}

关于html - 导航子菜单链接列出容器的全宽,而不是它们嵌套的菜单项的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29419186/

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