Home > database >  Why backslash after a .class in css?
Why backslash after a .class in css?

Time:09-14

I have seen the following CSS in an example -

.hover\:bg-gray-800:hover {
    background-color: #2d3748;
 }

My question is what is the usage of backslash here?

CodePudding user response:

The colon in CSS has a special meaning; pseudo class selectors start with this special char, like :hover (which your code example actually has at the end), :active, :first-child.

The \ tells CSS to treat the following character not as a character with a special CSS meaning. This is generally called escaping.

This allows for usage of CSS class names containing a : in HTML:

.foo\:hover { color: orange; padding: 3em; }

.foo\:hover:hover { background-color: orange; color: white; }
<div >foo bar here, hover me</div>

  •  Tags:  
  • css
  • Related