我有一个水平菜单。当鼠标悬停在子元素上时,它会消失。无法单击子元素。当鼠标悬停在它上面时,我希望子元素保持不变。
.header {
color: #FFFFFF;
height: 60px;
width: 100%;
margin: auto;
}
.header_logo {
width: 40%;
height: 100%;
float: left;
}
#logo {
height: 100%;
top: 0;
left: 0;
width: 50%;
}
.header_title {
width: 60%;
float: left;
}
#titles {
position: absolute;
top: 20px;
font-family: "Times New Roman", Times, serif, Georgia;
font-size: 97%;
color: #B8B8B8;
}
ul {
list-style-type: none;
}
li {
display: inline-block;
}
a {
text-decoration: none;
color: inherit;
padding: 21px 10px;
}
li a:hover {
background-color: #666699;
}
ul li ul {
display: none;
}
ul li:hover ul {
display: block;
position: absolute;
width: 100%;
top: 70px;
left: 0;
white-space: nowrap;
}
* {
border: 0;
margin: 0;
padding: 0;
}
.content {
height: 800px;
margin: auto;
background-color: #C0C0C0;
}
.footer {
margin: auto;
background-color: #000000;
}
<div class="header">
<div class="header_logo">
<img id="logo" src="civic-logo.jpg">
</div>
<div class="header_title">
<div id="titles">
<ul>
<li><a href="#">PRODUCTS</a>
<ul>
<li><a href="#">CEMENT</a></li>
<li><a href="#">STEEL</a></li>
<li><a href="#">BRICKS</a></li>
<li><a href="#">SAND</a></li>
</ul>
</li>
<li><a href="#">CONTACT US</a> </li>
</ul>
</div>
</div>
</div>
<div class="content">
</div>
<div class="footer">
</div>
答案 0 :(得分:2)
这是因为您已完全定位了子菜单,并且距离您的父级别太远,因此您的鼠标在到达子菜单之前会离开父菜单。我已经添加了边框来向您展示。
.header {
color: #FFFFFF;
height: 60px;
width: 100%;
margin: auto;
}
.header_logo {
width: 40%;
height: 100%;
float: left;
}
#logo {
height: 100%;
top: 0;
left: 0;
width: 50%;
}
.header_title {
width: 60%;
float: left;
}
#titles {
position: absolute;
top: 20px;
font-family: "Times New Roman", Times, serif, Georgia;
font-size: 97%;
color: #B8B8B8;
}
ul {
list-style-type: none;
}
li {
display: inline-block;
border: 1px solid blue;
}
a {
text-decoration: none;
color: inherit;
padding: 21px 10px;
}
li a:hover {
background-color: #666699;
}
ul li ul {
display: none;
}
ul li:hover ul {
display: block;
position: absolute;
width: 100%;
top: 70px;
left: 0;
white-space: nowrap;
}
* {
border: 0;
margin: 0;
padding: 0;
}
.content {
height: 800px;
margin: auto;
background-color: #C0C0C0;
}
.footer {
margin: auto;
background-color: #000000;
}
&#13;
<div class="header">
<div class="header_logo">
<img id="logo" src="civic-logo.jpg">
</div>
<div class="header_title">
<div id="titles">
<ul>
<li><a href="#">PRODUCTS</a>
<ul>
<li><a href="#">CEMENT</a></li>
<li><a href="#">STEEL</a></li>
<li><a href="#">BRICKS</a></li>
<li><a href="#">SAND</a></li>
</ul>
</li>
<li><a href="#">CONTACT US</a> </li>
</ul>
</div>
</div>
</div>
<div class="content">
</div>
<div class="footer">
</div>
&#13;
您有填充,但它位于a
元素上,而是需要位于li
元素上。在您的子菜单li
元素中添加padding-top或调整其top
位置,以便它们直接位于父元素下面(也就是&#34;触及&#34;)。
以下是将菜单移至top: 40px
并将填充添加到子菜单li
元素的代码:
.header {
color: #FFFFFF;
height: 60px;
width: 100%;
margin: auto;
}
.header_logo {
width: 40%;
height: 100%;
float: left;
}
#logo {
height: 100%;
top: 0;
left: 0;
width: 50%;
}
.header_title {
width: 60%;
float: left;
}
#titles {
position: absolute;
top: 20px;
font-family: "Times New Roman", Times, serif, Georgia;
font-size: 97%;
color: #B8B8B8;
}
ul {
list-style-type: none;
}
li {
display: inline-block;
}
a {
text-decoration: none;
color: inherit;
padding: 21px 10px;
}
li ul li {
padding: 21px 10px;
}
li a:hover {
background-color: #666699;
}
ul li ul {
display: none;
}
ul li:hover ul {
display: block;
position: absolute;
width: 100%;
top: 40px;
left: 0;
white-space: nowrap;
}
* {
border: 0;
margin: 0;
padding: 0;
}
.content {
height: 800px;
margin: auto;
background-color: #C0C0C0;
}
.footer {
margin: auto;
background-color: #000000;
}
&#13;
<div class="header">
<div class="header_logo">
<img id="logo" src="civic-logo.jpg">
</div>
<div class="header_title">
<div id="titles">
<ul>
<li><a href="#">PRODUCTS</a>
<ul>
<li><a href="#">CEMENT</a></li>
<li><a href="#">STEEL</a></li>
<li><a href="#">BRICKS</a></li>
<li><a href="#">SAND</a></li>
</ul>
</li>
<li><a href="#">CONTACT US</a> </li>
</ul>
</div>
</div>
</div>
<div class="content">
</div>
<div class="footer">
</div>
&#13;