Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css pseudo-classes without any selector, is it ok?

Selector like

.some-class :first-child

which selects the first child inside .some-class works as expected in all browsers, even in the IE8.

But mobile Chrome's 'Reduce data option' removes a space between .some-class and :first-child and breaks the CSS by applying styles to .some-class element itself. In other cases like .class1 .class2 Chrome keeps the space intact. Probably, it considers the space between .some-class and :first-child to be a non-semantic space, because it thinks that there can't be a pseudo-class without some kind of a selector.

But I've checked the standard and it doesn't say anywhere that pseudo-class must have a selector. At the same time all examples with pseudo-classes include an element selector, like

p:first-child

So I'm confused. Is this CSS selector incorrect or is there a bug in Chrome's 'Reduce data usage'?

like image 666
snovity Avatar asked Jan 12 '15 09:01

snovity


People also ask

Is Pseudo a class selector?

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, the pseudo-class :hover can be used to select a button when a user's pointer hovers over the button and this selected button can then be styled.

Why do we need selectors in CSS?

In CSS, selectors are used to target the HTML elements on our web pages that we want to style. There are a wide variety of CSS selectors available, allowing for fine-grained precision when selecting elements to style.

What are CSS pseudo classes useful for?

Use CSS Pseudo-classes to Highlight User's Position When a user points to an object on a web page with a mouse, it's helpful if that object responds in some way. For example, when a user hovers over a link, the color and background-color of that link could be reversed.

Can CSS classes combine with pseudo classes?

Combining pseudo classes with CSS classesIt is possible to combine pseudo classes and regular CSS classes. This combination lets you style elements that have specific classes and also react to certain external factors.


2 Answers

:first-child is valid as a standalone selector and this appears to be a bug in the mobile version of Chrome. We can confirm this with W3C's CSS Validator.

:first-child { background: #F00; }

Congratulations! No Error Found.

In fact, everything listed in the Selectors section of the Selectors Level 3 specification is valid as a standalone selector. I imagine the reason the examples for :first-child also include an element selector is to avoid confusion and to show how it can be used on that particular element.

like image 170
James Donnelly Avatar answered Sep 27 '22 16:09

James Donnelly


I would guess it's a bug. You should be able to work around it with .some-class *:first-child

like image 26
AndreasKnudsen Avatar answered Sep 27 '22 15:09

AndreasKnudsen