Does the css class always use the best idea to get rid of styling issues?

Is using css .class

instead of #id

always better idea to get rid of styling and iimportant override problems?

#content ul li a {font-size:10px}

#content .demo ul li a {font-size:15px}

      

+2


a source to share


3 answers


id

is an identifier and ideally should only be used for one element, whereas it class

can be used multiple times for multiple elements.



CM: Id vs Class

+2


a source


The class will not always override the ID - given two conflicting rules for the same element, the browser will calculate the score for each rule based on the specifics of the rule. Each part of the rule scores points, then the rule with the most applicable points.

  • html selectors (div, p, etc.) - +1 Class selectors
  • (.class) - +10
  • id selectors (#id) +100

So for your example:



#content ul li a {font-size:10px} = 100 + 1 + 1 + 1 = 103
#content .demo ul li a {font-size:15px} = 100 + 10 + 1 + 1 + 1 = 113

      

Hope it helps

+1


a source


No, I think not. IDs should only be used for unique elements to be resolved using JavaScript IMO.

They have higher specificity than classes when the value of a CSS rule is evaluated, but this is not an argument to use them.

0


a source







All Articles