fac1b5c50e63ab148385
·2 min read

Difference between `.parent:hover` and `.parent :hover` in CSS

css
pseudo-class
4d5fd787ac74e0caa4f7

Sohan R. Emon

Developer, Learner, Tech Enthusiast

In CSS, the difference between .parent:hover and .parent :hover lies in how they target and select elements in the HTML document. Let's break down the differences:

  1. .parent:hover:

    • This selector targets the .parent element when it is in the "hover" state. In other words, it selects the .parent element itself when a user hovers their mouse over it.
  2. .parent :hover:

    • This selector targets any element that is a descendant of the .parent element and is in the "hover" state. It selects any element inside the .parent element that is being hovered over. It does not select the .parent element itself.

Here's an example to illustrate the difference:

HTML:

html
<div class="parent">
    <p>This is a paragraph inside the parent element.</p>
</div>

CSS:

css
.parent:hover {
    background-color: lightblue;
}
.parent :hover {
    color: red;
}

In this example:

  • When you hover over the .parent element, its background will change to light blue due to .parent:hover.
  • When you hover over the paragraph inside the .parent element, the text color of the paragraph will change to red due to .parent :hover.

To summarize, .parent:hover targets the parent element itself when it's hovered, while .parent :hover targets descendants of the parent element when they are hovered.

Found this useful?