css实现a hover li悬停区域改变背景色

淳于思淼
2023-12-01

css实现<li>悬停区域背景色改变:

1、li中的文字要加<a>

2、在css中写 li a:hover{background: #000;  display: block;}

3、如果只要文字变色display:inline;  如果要整行li变色一定要有display:block;



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css"> 
li a:hover { 
    background-color:#000; 
    display: block; 
}
#text a:hover{
background-color:#ff0000;
display:inline;
}
</style> 


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>改变li的背景色</title>
</head>


<body>
<div>  
<ul>  
<li><a href="#">整个li的背景色都变了</a></li>  
<li id="text"><a href="#">只有文字的背景都变了</a></li>  
</ul>  
</div>


</body>
</html>

 类似资料: