Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recommended css notation

Tags:

css

I have a div with an ID:

<div id="main">

What's the correct (or difference) between

div#main {

and

#main {

Regards,

like image 586
Teson Avatar asked May 28 '12 22:05

Teson


People also ask

What is the correct CSS format?

CSS text formatting properties is used to format text and style text. Text-color property is used to set the color of the text. Text-color can be set by using the name “red”, hex value “#ff0000” or by its RGB value“rgb(255, 0, 0). Text alignment property is used to set the horizontal alignment of the text.

Which referencing method is recommended CSS?

The best method for attaching your CSS style sheets is to use external styles. With this method, you will write all your CSS in a separate file with a . css extension. You can then link to the CSS file from each of your HTML pages.

What are the 3 CSS rules?

Inheritance, the Cascade, and Specificity are the big three. Understanding these concepts will allow you to write very powerful stylesheets and also save time by writing fewer CSS rules.

What is a good CSS file size?

For comparison, a good size CSS should be under 150KiB, perhaps 200KiB maximum. In the case that your CSS is over that, you may have some optimizations you can do. A few points of optimization: Unused CSS.


2 Answers

There is a great doco on using efficient CSS selectors, focus on rules with overly qualified selectors:

ID selectors are unique by definition. Including tag or class qualifiers just adds redundant information that needs to be evaluated needlessly.

Instead of just applying the style to an element with id main, your selector will re-qualify the element by checking whether or not it's also a div (in that order). To clarify: css selectors are evaluated right to left, unlike same selector syntax when used in jQuery etc.

Re pixelistik's suggestion that div#main is more specific than #main - yes, that is technically correct, however if you have to resort to this to raise a rule's specificity, chances are the structure of CSS you're working on is not as thought through as it should be.

like image 125
Oleg Avatar answered Sep 25 '22 01:09

Oleg


#main matches everything with ID 'main', whereas div#main matches only <div> elements with ID main.

Ideally, you should never have two elements with the same ID, so realistically the two don't make a difference, but there's probably performance related issues regarding whether specifying div makes it find the result faster.

like image 24
Death Avatar answered Sep 24 '22 01:09

Death