Change color of <div> using onMouseover

Harish R picture Harish R · Jan 19, 2014 · Viewed 10.6k times · Source

Im trying to change the color of the div using omMouse ove Event handler. From grey to black But it doesnt work.

Whats wrong here ? Also how to use onHover and onMouseout ?

<html>

<head>
    <title></title>
    <link type="text/css" href="" />
    <script type="text/javascript" src="" ></script>    
    <style>
        div{
            height: 100px;
            width: 100px;
            background-color: grey;
        }
    </style>

</head>

<body>
    <div onMouseover="this.bgColor = '#FFFFFF'">

    </div>
</body>

<script>

</script>
</html>

Answer

Muhammad Rashid picture Muhammad Rashid · Jan 19, 2014

You can do by doing this

By tag

div:hover {
    background-color: red;
}

By div Class

.divclass:hover {
        background-color: red;
    }

By div Id

#divclass:hover {
        background-color: red;
    }