I’m not sure how to describe this, but you may have seen it in action in a sidebar. When you hover over a text link a colored background appears behind the text. It’s easy to accomplish, and I think it looks clean. So how do you do it? Here’s how.
First of all, you need a properly coded html list like so:
- <ul>
- <li><a href="#">list item 1</a></li>
- <li><a href="#">list item 2</a></li>
- <li><a href="#">list item 3</a></li>
- </ul>
<ul>
<li><a href="#">list item 1</a></li>
<li><a href="#">list item 2</a></li>
<li><a href="#">list item 3</a></li>
</ul>The ul is the tag for an unordered list, the li is the tag for list item and the a href is an anchor tag.
And here is the css:
You will need to add styles to each of these – plus the hover state to the anchor tag – to make it work. I’ve commented the most important parts. Don’t forget to set the anchor tag to a block element. If you don’t, the color will only extend the width of the text and not the entire width of the ul.
- ul {
- width: 300px; /*overall width of the list */
- padding: 0; /*zero out the padding*/
- margin: 0; /*zero out the margin*/
- }
- li {
- list-style: none; /*takes the bullet off the list item*/
- padding: 5px 0px;
- width: 300px;
- border-bottom: 1px solid #707070;
- height: 20px; /*have to give a height here and on the li a */
- margin-bottom: 5px;
- }
- li a {
- font-size: 120%;
- line-height: 130%;
- font-family: "Century Gothic", Verdana, "Myriad Pro", sans-serif;
- display: block; /*very important. Setting this to block makes sure your background expands the entire 300px and not just the width of the text */
- height: 10px; /* height is need here also */
- width: 300px;
- padding: 5px 0 5px 5px;
- }
- a:hover {
- background: #9bb716; /*makes the background green on hover*/
- text-decoration: none;
- color: #fff;
- width: 300px;
- }
ul {
width: 300px; /*overall width of the list */
padding: 0; /*zero out the padding*/
margin: 0; /*zero out the margin*/
}
li {
list-style: none; /*takes the bullet off the list item*/
padding: 5px 0px;
width: 300px;
border-bottom: 1px solid #707070;
height: 20px; /*have to give a height here and on the li a */
margin-bottom: 5px;
}
li a {
font-size: 120%;
line-height: 130%;
font-family: "Century Gothic", Verdana, "Myriad Pro", sans-serif;
display: block; /*very important. Setting this to block makes sure your background expands the entire 300px and not just the width of the text */
height: 10px; /* height is need here also */
width: 300px;
padding: 5px 0 5px 5px;
}
a:hover {
background: #9bb716; /*makes the background green on hover*/
text-decoration: none;
color: #fff;
width: 300px;
}