Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would it be faster to use a CSS Child Selector?

If we wanted to target a link within a paragraph, which selector would be more efficient/faster?

p a

or

p > a
like image 975
sindiploma Avatar asked Sep 08 '14 23:09

sindiploma


People also ask

Which CSS selector is faster?

The selector abobe should be the fastest.

Which selector is best in CSS?

Class selector is the most useful common selector used by the developers. You can define the class selector using period (.) followed by the class name. It gives styling to all elements with a specified class attribute.

What are the uses of child selectors in CSS?

The CSS child selector is used to select all child elements with a particular parent element.

What is the difference between parent and child selector in CSS?

The first selector indicates the parent element. The second selector indicates the child element CSS will style. The example below selects all <p> elements that are children of the <div> element: The CSS selector using the > symbol only selects direct children.

How do I select all children of an element in CSS?

The CSS child selector has two selectors separated by a > symbol. The first selector indicates the parent element. The second selector indicates the child element CSS will style. The example below selects all <p> elements that are children of the <div> element: The CSS selector using the > symbol only selects direct children.

What are the basic CSS selectors?

This page will explain the most basic CSS selectors. The element selector selects HTML elements based on the element name. The id selector uses the id attribute of an HTML element to select a specific element.

What is a child Combinator in CSS?

The child combinator selects elements that match the second selector and are the direct children of the first selector. Operators make it easier to find elements that you want to style with CSS properties.


2 Answers

The second is (extremely) marginally faster. CSS is handled in reverse by browsers, so both your rules are tested on all a elements on the page. For the second rule it only needs to test the direct parent, for the other one it would need to test the entire descendant chain.

In practice, the execution time difference won't be statistically significant until you get tens of thousands of these on a page with just as many lines of HTML.

like image 75
Niels Keurentjes Avatar answered Oct 04 '22 09:10

Niels Keurentjes


Let me show you the efficiency order of selectors, from fastest to the slowest, that's some conclusion from google:

  1. id Selectors (#myid)
  2. class Selectors (.myclassname)
  3. tag Selectors (div,h1,p)
  4. neighbour Selectors (h1+p)
  5. children Selectors (ul>li)
  6. descendant Selectors (li a)
  7. star Selectors (*)
  8. property Selectors (a[rel="external"])
  9. pseudo class Selectors (a:hover,li:nth-child)

It might not be exactly right, and not being right for various of browsers, but still this order is available for reference. Hope it helps!

To see more about CSS performance, see: http://benfrain.com/css-performance-revisited-selectors-bloat-expensive-styles/

like image 27
Alfred Huang Avatar answered Oct 04 '22 08:10

Alfred Huang