Whether or not to "div" in the CSS

Asked

Viewed 196 times

6

Should I or should I not inform the div in the CSS? Example:

.teste {blablabla}
div.teste {blablabla}

HTML:

<div class="teste">teste</div>

I never inform, only that I was seeing the code of a site which he was informed.

1 answer

8


Inform or not div as a class prefix, in css, it depends on the type of element you want the rule to be applied to, because a css rule can work in the following ways:

div { blabla } //aplica a todas as div's do documento.
span { blabla } //aplica a todos os span do documento.
.teste { blabla } //aplica a todos os elementos que tiverem classe .teste
div.teste { blabla } //aplica APENAS AS DIVS que contem a classe .teste
span.teste { blabla } //aplica APENAS OS SPANS que contem a classe .teste

So even if the rule is named as the class .teste will only apply to all elements containing the class .teste if there is no type specification before, for example div.teste there would be all the Divs which contains the class .teste and not all elements

Elements are all types of existing html elements.

Divs are all tags <div> that exist in your html.

Important remark:

Don’t confuse div .teste with div.teste for are very different things as an example below:

div .teste { blabla } //aplica a todos os elementos que conterem classe .teste que forem filhos de uma DIV
div.teste { blabla } //aplica a todas as divs que conterem a classe .teste

When you give a space in the rule name, you are saying that the next element will be the child of the first element (the one on the left)

Examples of Use:

div div {
  margin: 10px; /* apenas para dar um espaço */  
}

div, span {
  width:   150px; /* apenas para dar forma aos elementos */
  height:  50px;  /* apenas para dar forma aos elementos */
  margin:  10px;  /* apenas para dar um certo espaço */
  display: block; /* apenas para dar forma aos elementos */
  background-color: cyan;
}

.teste {
  background-color: blue;
}

div.teste {
  background-color: yellow;
}

span.teste {
  background-color: red;
}

div .teste {
  background-color: green;
}

.teste div {
  background-color: black;
  color: #fff;
}

.teste span {
  background-color: orange;  
}
<div class=teste>div.teste</div>

<span class=teste>span.teste</span>

<div>
  <div class=teste>div .teste</div>
</div>

<div>
  <span class=teste>div .teste</span>
</div>

<div class=teste>
  <div>.teste div</div>
</div>

<div class=teste>
  <span>.teste span</span>
</div>

  • 2

    +1 for the remark on the difference of div .teste and div.teste.

  • 1

    Perfect explanation. You could post an example of div .teste { blabla }? wanted to better understand how these children work.

  • 1

    i put the examples for you to see @Felipestoker

  • Thank you very much, perfect explanation! I get it now.

  • I just want to add that unless strictly necessary, you should not specify a tag and a class or id in the same CSS rule as this causes the rule processing to be slower. Source: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS

  • But that’s more of how he’s willing html, As you can see, a lot of inheritance slows him down too, so the html must be well-classified and identified for this to work. Apart from that properties that must be applied to an entire set of elements it is strictly necessary to use tag, not to mention that this difference in performance must be insignificant to the user, in most cases.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.