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;}


The HTML portion looks like this:


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

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

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



(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.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: