CSS:扩展悬停

时间:2015-12-10 19:30:24

标签: html css css3 menu hover

我的网站旁边有一个菜单栏,我希望悬停跨越菜单栏的宽度,到目前为止只覆盖文本。

我希望它看起来像这样,并让它们全部结束菜单结束

Image 1

但它看起来像这样

Image 2

HTML:



#menu {
    width: 21.4vw;
    font-family: Tahoma, Geneva, sans-serif;
    font-weight: normal;
	 font-size: 1vw;
    text-align: left;
    margin-left: 1vw;
    background-color: #CA2A2A;
        border-radius: 0px;
        max-width: 96vw;
    
}
#menu ul {
    height: auto;
    padding: 0.5vw 0vw;
    margin: 0px;
}
#menu li { 
    display: block; 

}
#menu a {
    text-decoration: none;
    color: #FFFFFF;
    padding: 0.5vw 0.8vw 0.5vw 0.8vw;
	font-size: 3vw;
    
}
#menu a:hover {
    color: #000000;
    background-color: #FFFFFF;

<!DOCTYPE html>
<html>
  <head>
    </head>
  <body>
<div id="menu">
<ul>
<li><a href="website.html">Home</a>
<li><a href="breakingnews.html">Breaking News</a></li>
<li><a href="Sport.html">Sport</a></li>
<li><a href="Hulltoday.html">Hull Today</a></li>
<li><a href="Property.html">Property</a></li>
<li><a href="Social.html">Social Media</a></li>
<li><a href="Music.html">Music</a></li>
<li><a href="Reviews.html">Reviews</a></li>
<li><a href="Movies.html">Movies</a></li>
<li><a href="Weather.html">Weather</a></li>
</ul>
</div>
    
  </body>
  </html>
&#13;
&#13;
&#13;

我是HTML和CSS的新手,所以任何帮助或建议都非常适合:)

4 个答案:

答案 0 :(得分:0)

您需要将display: block;添加到#menu a

&#13;
&#13;
#menu {
    width: 21.4vw;
    font-family: Tahoma, Geneva, sans-serif;
    font-weight: normal;
	 font-size: 1vw;
    text-align: left;
    margin-left: 1vw;
    background-color: #CA2A2A;
        border-radius: 0px;
        max-width: 96vw;
    
}
#menu ul {
    height: auto;
    padding: 0.5vw 0vw;
    margin: 0px;
}
#menu li { 
    display: block; 

}
#menu a {
    text-decoration: none;
    color: #FFFFFF;
    padding: 0.5vw 0.8vw 0.5vw 0.8vw;
	font-size: 3vw;
    display: block;
}
#menu a:hover {
    color: #000000;
    background-color: #FFFFFF;
&#13;
<!DOCTYPE html>
<html>
  <head>
    </head>
  <body>
<div id="menu">
<ul>
<li><a href="website.html">Home</a>
<li><a href="breakingnews.html">Breaking News</a></li>
<li><a href="Sport.html">Sport</a></li>
<li><a href="Hulltoday.html">Hull Today</a></li>
<li><a href="Property.html">Property</a></li>
<li><a href="Social.html">Social Media</a></li>
<li><a href="Music.html">Music</a></li>
<li><a href="Reviews.html">Reviews</a></li>
<li><a href="Movies.html">Movies</a></li>
<li><a href="Weather.html">Weather</a></li>
</ul>
</div>
    
  </body>
  </html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

a元素是内联元素,与其内容相关,这就是白色区域仅与文本一样宽的原因。

要解决此问题,请将链接显示为块:

#menu a { display: block; }

默认情况下,块元素填充其父元素内的可用宽度。

答案 2 :(得分:0)

尝试添加

display: block;

#menu选择器。如果这不起作用,请提供菜单的HTML。

答案 3 :(得分:0)

您可以将悬停伪类添加到li元素而不是元素。检查以下代码。这样,突出显示的背景将对li的大小生效。

#menu {
    width: 21.4vw;
    font-family: Tahoma, Geneva, sans-serif;
    font-weight: normal;
	 font-size: 1vw;
    text-align: left;
    margin-left: 1vw;
    background-color: #CA2A2A;
        border-radius: 0px;
        max-width: 96vw;
    
}
#menu ul {
    height: auto;
    padding: 0.5vw 0vw;
    margin: 0px;
}
#menu li { 
    display: block; 

}
#menu a {
    text-decoration: none;
    color: #FFFFFF;
    padding: 0.5vw 0.8vw 0.5vw 0.8vw;
	font-size: 3vw;
    
}
#menu li:hover {
    
    background-color: #FFFFFF;}

#menu li:hover a {
    
    color: #000;}
<!DOCTYPE html>
<html>
  <head>
    </head>
  <body>
<div id="menu">
<ul>
<li><a href="website.html">Home</a>
<li><a href="breakingnews.html">Breaking News</a></li>
<li><a href="Sport.html">Sport</a></li>
<li><a href="Hulltoday.html">Hull Today</a></li>
<li><a href="Property.html">Property</a></li>
<li><a href="Social.html">Social Media</a></li>
<li><a href="Music.html">Music</a></li>
<li><a href="Reviews.html">Reviews</a></li>
<li><a href="Movies.html">Movies</a></li>
<li><a href="Weather.html">Weather</a></li>
</ul>
</div>
    
  </body>
  </html>