How To Hover In Inline Css

In this blog post, we will explore how to create hover effects using inline CSS. Typically, it’s not possible to apply a hover effect directly in inline CSS since it requires a pseudo-class, which cannot be used in inline styles. However, we’ll discuss a workaround to achieve the desired hover effect by using JavaScript.

Creating a hover effect with JavaScript

To create a hover effect using JavaScript, we’ll need to add event listeners on the desired HTML element for the mouseover and mouseout events. These events will be used to change the inline CSS of the element when the mouse pointer is over it and reset the style when the pointer is moved away.

Here’s an example of how to create a hover effect on a <div> element using JavaScript:

        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Hover Effect with Inline CSS and JavaScript</title>
        <div id="hoverDiv" style="width: 200px; height: 50px; background-color: red; text-align: center; padding-top: 10px;" onmouseover="hoverEffect(this)" onmouseout="resetEffect(this)">
            Hover over me!
            function hoverEffect(element) {
       = 'blue';
       = 'white';

            function resetEffect(element) {
       = 'red';
       = 'black';

In the example above, we have a <div> element with an inline CSS style defining its dimensions, background color, text alignment, and padding. The element also has onmouseover and onmouseout event attributes pointing to two JavaScript functions: hoverEffect() and resetEffect().

When the mouse pointer is moved over the element, the hoverEffect() function is called, changing the background color to blue and the text color to white. When the mouse pointer is moved away from the element, the resetEffect() function is called, resetting the background color to red and the text color to black.


While it’s not possible to use the CSS :hover pseudo-class directly in inline CSS, we can still create hover effects in inline styles using JavaScript. This workaround allows us to modify the inline style of an element when the mouse pointer is over it and reset the style when the pointer is moved away.