I am extremely new to HTML and CSS coding so there are many things that I do not understand. One thing that perplexes me is the redundancy I see in certain CSS selectors. For example, if I wanted to change the color of a heading I could input h1 {color: orange;} or I could add a class (let's call it "title" in this scenario) into the h1 element tag under the HTML code and change the color by inputting into the CSS code .title{color: orange;}. To me, this appears to do the same thing yet adding and calling a class seems redundant and makes more sense to call the tag itself. Could someone help explain what the difference between these two is if there is any?
With the element selector all H1 elements will be styled, but with the CLASS selector only elements with that CLASS are styled:
The first case, when we stick to H1, demonstrates specificity that is a very important part of CSS. Just using the element name (h1) has low specificity. Using the classname (.title) has higher. Using both (h1.title) even higher.
So even if the two first selectors above come after the one with both element and classname, the latter will win.
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)