Archive for October, 2014

li hover styling in HTML

To create a hover effect on <li> elements (you know, all those elements inside a <ul>), style the elements in the style block in the HTML page header, like so:

<style type=”text/css”>

     li div {height: 50px; width: 150px; background-color: green;}

     li div:hover {background-color: blue;}

</style>

The HTML portion looks like this:

<ul>

     <li><div>Room 1</div></li>

     <li><div>Room 2</div></li>

     <li><div>Room 3</div></li>

</ul>

 

(I have other styling that I have removed. If I was just using text, I wouldn’t have used a div.)

The first time I tried to add a hover effect, I had the styling on the element and not in the style block:

 

     <li><div style=”height: 50px; width: 70px; background-color: green”>Room 1</div></li>

 

This didn’t turn out right, though. The first  row looked funky when I hovered over it:

li hover1

That’s pretty ugly regardless of the color scheme. So remember, keep the styling outside of the <li> to get the hover effect working.

Leave a comment