Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS space before selector

Tags:

html

css

I was struggling to understand why this simple CSS class selector was not being picked by my HTML element.

.label-hi :before {
    color:green;
    content: "Hi ! ";
}
like image 588
Hugo Hilário Avatar asked Oct 23 '25 15:10

Hugo Hilário


2 Answers

The space between simple selectors is a descendant combinator in CSS. If it were two ordinary selectors separated with space, it would mean 'element matching the second selector, placed anywhere inside the element matching the first selector'. Since the second selector is a pseudo element, the whole selector is equivalent to .label-hi *:before, potentially inserting something into any element inside the element with class label-hi.

like image 150
Ilya Streltsyn Avatar answered Oct 27 '25 05:10

Ilya Streltsyn


So I realized that for this specific selectors I cannot have any space between the class name and the : This means that removing the empty space my CSS class is picked :

.label-hi:before {
    color:green;
    content: "Hi ! ";
}

In regular classes that space does not make any difference.

Sample here : http://jsfiddle.net/GtTcn/

like image 24
Hugo Hilário Avatar answered Oct 27 '25 03:10

Hugo Hilário



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!