gpt4 book ai didi

html - 如何在不更改主菜单项宽度的情况下制作下拉菜单?

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:53 25 4
gpt4 key购买 nike

我是 HTML 和 CSS 的新手。

我正在尝试制作一个带有下拉菜单的导航栏。

我创建了导航栏。但是主菜单项的宽度随着子菜单项的变化而变化。

如何停止更改主菜单项的宽度。这里的主菜单项是“学校”。

我不想改变 SCHOOLING 的宽度。

变更前

enter image description here

变更后

enter image description here

导航栏的html代码

<!DOCTYPE html>
<html>
<head>
<title>Navigation</title>
<link rel="stylesheet" type="text/css" href="nav.css">
</head>
<body>
<nav id="navbar" class="bold">
<ul>
<li style="border-top-left-radius:10px;border-bottom-left-radius:10px;"><a href="index.html" style="padding-left:21px;">HOME</a></li>
<li><a href="subject.html" >SCHOOLING</a>
<ul>
<li><a href="#">HIGH SCHOOL</a></li>
<li><a href="#">HIGHER SECONDARY</a></li>
</ul>
</li>

<li><a href="subject.html">ENGINEERING</a></li>
<li><a href="subject.html">UG | PG</a></li>
<li><a href="subject.html">SPECIAL CLASSES</a></li>
<li><a href="about.html">ABOUT</a></li>
<li style="border-right:none;"><a href="contact.html">CONTACT US</a></li>
</ul>
</nav>
</body>
</html>

导航栏的 CSS 代码

* {
margin: 0;
padding: 0;
font-family: Courier New, monospace;
}

.fleft {
float: left;
}
.fright {
float: right;
}
.clear {
clear: both;
}

.bold {
font-weight: bold;
}

/* NAV BAR */

#navbar {
background-color: #333;
margin:10px;
height: 40px;
color: white;
border-radius: 10px;
}

#navbar ul {
list-style: none;
overflow: hidden;

}

#navbar ul li {
float: left;
border-right: 2px solid #dede0e;
font-size: 1.5em;

}
#navbar ul li a {
color: white;
display: block;
text-decoration: none;
padding: 6px 10px;

}
#navbar ul li:hover {
background-color: #000000;


}

/* DROP DOWN MENU */

#navbar ul ul {
display: none;
list-style: none;

color: blueviolet;
background: #a80000;

}
#navbar ul ul li {
float: none;
font-size: 1em;
border: none;
position: relative;
top: 100%;
left: 0;
}
#navbar ul ul li a {
border-right: none;
padding: 0 20px;
}
#navbar ul li:hover > ul
{
display: block;
}

最佳答案

您可以通过将下拉菜单的位置设置为绝对位置来实现这一点。

#navbar ul ul {
display: none;
list-style: none;
color: blueviolet;
background: #a80000;
position: absolute;
}

关于html - 如何在不更改主菜单项宽度的情况下制作下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729677/

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