How does hierarchy elements in css work? [duplicate]

4

I would like to know how the hierarchy works in css, I explain:

Let's say in my style.css I set to h1, h2, h3, h4, etc. by default font-size: 12px; , so far beauty ...

Now, if for example I insert inside a class defining that the element h1 of that class has {font-size: 18px; color: #fff;} , the element should not display font-size: 18px; ? Because a number of times it happened that% w /% got the size 12 instead of 18.

I hope I have been clear in my doubt (and that it is not duplicate). I think the answer is very simple rsrs

Ty

    
asked by anonymous 19.04.2018 / 19:06

2 answers

5

Second the developer.mozilla.org documentation:

The provenance is defined in 3 levels

1. Importance

Styles with !important precede all others and have higher priority. Following are the inline style defined, that is, in the element itself, within style :

<div style="border: none"><div>

Then the priority is: Selector by ID : #id {...}
Selector by name of class : .classe {...}
Selector by tag name: div {...}

2. Specificity It is measured based on how specific a selector is - how many elements it can match / achieve. There is a certain "calculation" to be determined, to understand see the link: Specificity

But a simple way to understand it is: the more specific a selector the higher its priority. Here is an example of the link above:

HTML :

<div id="test">
  <span>Text</span>
</div>

CSS :

div#test span { color: green; }
div span { color: blue; }
span { color: red; }

In this example, the color will be green, since it is the most specific selector ( span nested with div , with ID="test" ), regardless of the order the selectors were declared.

3. Order In a basic way, if the above criteria do not prioritize the rule, it follows the order they were declared

Another great source here: www.w3.org

    
19.04.2018 / 19:25
1

In CSS , the priority is thus

First comes any line of css that has !important , which will always put first

The second factor is Inline Styles or Estilos em Linha which are those edited in the own line and finally are all classes defined by outside (documents .css ) and definition of classes in tag <head>

    
19.04.2018 / 19:15