When to use div + selector in CSS

4

When I'm developing a front-end for a website, I put the name of the selector directly, for example .seletor1 , .seletor2 , and so on.

I recently had to work on the classes of a plugin I downloaded, and I saw that it contained: div.seletor

Excuse my ignorance, but I have six months front-end and have never used it like this.

    
asked by anonymous 25.06.2014 / 14:04

2 answers

5

I see two reasons for this:

  • You use the seletor class in more than one element type, and you want to create a CSS rule for DIVs only.

  • You need to give more weight to this rule in the CSS cascade. For example:

    .seletor1 { color: red; }
    .seletor2 { color: blue; }
    

    Consider a <div class="seletor1 seletor2>... . It will turn blue (both rules have the same weight, but the second rule prefers to come later in the source code). So if you use

    div.seletor1 { color: red; }
    

    You can force the red color on this div.

  • 25.06.2014 / 14:14
    3

    When preceded by "dot" refers to a class and when preceded by "sharp", the famous "#" refers to an id.

    The class should always be used when you want to set properties for a group of elements and the id for a single element.

    When you set div.classe {} you can set properties for this class exclusively in this div.

        
    25.06.2014 / 14:22