Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Is it important to specify the exact path for a particular element for speed/accuracy?

When defining CSS for a particular element does specifying exact path make any difference when we are talking about speed/accuracy/processing of a web page?

for e.g. if I have text inputs ONLY in 3rd column of my table, which is better for speed, accuracy, processing and other parameters?

OPTION 1:

table input[type="text"]
{
    background:yellow;
}

OPTION 2:

table td:nth-child(3) input[type="text"]
{
    background:yellow;
}
like image 396
Nikunj Madhogaria Avatar asked Sep 16 '14 12:09

Nikunj Madhogaria


People also ask

What is CSS specificity and why is it important?

What is Specificity? If there are two or more conflicting CSS rules that point to the same element, the browser follows some rules to determine which one is most specific and therefore wins out. Think of specificity as a score/rank that determines which style declarations are ultimately applied to an element.

What is the CSS “important” rule?

The CSS !important rule adds more authority than any other property. In CSS, the !important means "this is important". All the consequent rules must be ignored, and the !important rule should be applied. This rule must be set at the end of the line, directly before the semicolon.

What is the best way to improve CSS specificity?

Another thing is to always put elements (e.g. “p, a, h1”) and generic classes (e.g. “last”) before the other rules, it’s easier to understand what will be overwritten.. This is one of my favorite articles about CSS specificity.

Which CSS property specifies whether the text is written in the vertical?

Explanation: The writing-mode CSS property specifies whether the text is written in the vertical or horizontal direction. This property sets whether the lines of text are laid out vertically or horizontally. It specifies the direction in which the content flows on the page.


1 Answers

No, adding more compound selectors just gives the browser more elements to check and thus more work to do.

If the inputs will only ever appear in a specific place and you can guarantee that they will never appear anywhere else in a given page, then however precise you make your selector beyond input[type="text"] is irrelevant because it will always target the same set of elements anyway. Any extra checks you add then become redundant.

But the real question is whether performance even matters here. Unless you have tens of thousands of such elements, chances are the answer is no. Be as specific as you feel comfortable with. If you need the contextual selectors to make sure you don't accidentally target the wrong elements, there is no harm in putting them in.

like image 153
BoltClock Avatar answered Nov 10 '22 00:11

BoltClock