gpt4 book ai didi

html - 下拉菜单向一侧倾斜?

转载 作者:行者123 更新时间:2023-11-28 05:16:10 26 4
gpt4 key购买 nike

我对 HTML 有点陌生,因为我的类(class)最近开始等等。在我们的任务中,我们必须提交一个具有不同特征的网站,其中之一是下拉菜单。

我几乎成功了,但是当我试图将鼠标悬停在其中一个导航菜单标签上时,下拉菜单有点偏左,就像它是一个列表对象一样?

Here is how it shows up

This is how it looks when I hover over About

我不知道我做错了什么!我试图做一些研究,最终将导航菜单居中,但我只能做到这一点!我需要做什么才能使其与“关于”标签保持一致?

p {
font-size: 44px;
color: white;
font-family: verdana;
background-color: #333;
text-align: center;
}
p2 {
font-size: 12px;
color: white;
font-family: verdana;
}


body {
background-image: url(background.jpg);
}


div {
margin-left: 400px;
margin-right: 400px;
margin-top: 30px;
border: solid white 3px;
background-color: #333;
}


div2 {
margin-left: auto;
margin-right: auto;
width: 6em;
}

#nav {
text-align:center;
}


#nav ul {
display:inline-block;
}




#nav a {
float:left;
text-decoration:none;
padding:0 30px;
}


ul {
background-color: #333;
font-family: verdana;


}


ul li {

display: inline;
background-color: #333;
font-size: 24px;
height: 47px;
width: 200px;
line-height: 47px;
text-align: center;
float: left;
list-style: none;
}


ul li a {
color: white;
text-decoration: none;
display: block;
}


ul li a:hover {
background-color: white;
}


ul li a:active{
background-color: #333;
color: #333
}


ul li ul li {
display: none;
}


ul li:hover ul li{
display: block;
}


.active {
background-color: white;
}
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<link rel="stylesheet" href="stylesheet.css">
<title>Sleepless hub</title>
</head>

<body>

<div> <!-- Denne er med for å kun skape en fin designer strek ;) -->
</div>

<div>
<p>There is nothing like a good nut </p>
</div>

<div> <!-- Samme her. -->
</div>
<div id="nav">
<ul>
<li> <a href="#">Home</a></li>
<li> <a href="#">About</a>
<ul>
<li> <a href="#">Wikipedia</li>
</ul>
</li>
<li> <a href="#">Projects</a></li>
<li> <a href="#">Contact me</a></li>
<li> <a href="#">Fun Room</a></li>
</div>
<div>
<p>Okay</p>
</div>
</body>

</html>

最佳答案

问题是因为您对子 ul 和 anchor 标记进行了填充。如果您删除它们,它将正确对齐。尝试将类添加到子元素,因为这不是编写 css 的正确方法。

p {
font-size: 44px;
color: white;
font-family: verdana;
background-color: #333;
text-align: center;
}
p2 {
font-size: 12px;
color: white;
font-family: verdana;
}


body {
background-image: url(background.jpg);
}


div {
margin-left: 400px;
margin-right: 400px;
margin-top: 30px;
border: solid white 3px;
background-color: #333;
}


div2 {
margin-left: auto;
margin-right: auto;
width: 6em;
}

#nav {
text-align:center;
}


#nav ul {
display:inline-block;
}




#nav a {
float:left;
text-decoration:none;
padding:0 30px;
}


ul {
background-color: #333;
font-family: verdana;


}


ul li {

display: inline;
background-color: #333;
font-size: 24px;
height: 47px;
width: 200px;
line-height: 47px;
text-align: center;
float: left;
list-style: none;
}


ul li a {
color: white;
text-decoration: none;
display: block;
}


ul li a:hover {
border: 1px solid red;
}


ul li a:active{
background-color: #333;
color: #333
}


ul li ul li {
display: none;
}

ul li ul{
padding-left: 0;
}


ul li:hover ul li{
display: block;
}


.active {
background-color: white;
}
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<link rel="stylesheet" href="stylesheet.css">
<title>Sleepless hub</title>
</head>

<body>

<div> <!-- Denne er med for å kun skape en fin designer strek ;) -->
</div>

<div>
<p>There is nothing like a good nut </p>
</div>

<div> <!-- Samme her. -->
</div>
<div id="nav">
<ul>
<li> <a href="#">Home</a></li>
<li> <a href="#">About</a>
<ul>
<li> <a href="#">Wikipedia</li>
</ul>
</li>
<li> <a href="#">Projects</a></li>
<li> <a href="#">Contact me</a></li>
<li> <a href="#">Fun Room</a></li>
</div>
<div>
<p>Okay</p>
</div>
</body>

</html>

关于html - 下拉菜单向一侧倾斜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39302690/

26 4 0