how to use classes correctly in HTML

0

Performing a webpage I find myself with the following doubt at the moment of giving them styles in the CSS document, here a small fragment of a div that I have made:

 <div class="row" data-toggle="modal" data-target="#modalAddDir" data-backdrop="static" (click)="addDir()" id="new_dir">
      <div class="col" id="cir_mas">
        <div id="mas">+</div>
      </div>
      <div class="col" id="con_nu_dir" >
        <div id="nueva_dir">
          Añadir una nueva dirección
        </div>
      </div>
    </div>

As you can see I have a div with class & id.

However, they ask me to only work with classes when I give them styles in CSS, that is to say that the ids that I have placed do not take them into account. How should I write the classes when I have default classes like "row" and "col" which are sometimes nested and sometimes not, as is the case with the class "col" in my example.

How can I give them different styles when the classes are repeated in different divs and need specific styles without using IDs.

Greetings.

    
asked by Gerardo Gutiérrez 28.06.2018 в 17:07
source

4 answers

2

It does not matter that they all have the same class, you can differentiate one from another by adding a different class without harming the others. I leave you an example

.col{
  min-height: 100px;
  min-width: 50%;
  background-color: blue;
  margin: 3%;
}

.myCol{
  background-color: green;
}

.yourCol{
  background-color: yellow;
}

div[class="col theirCol"]{
background-color: brown;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="col"></div>
<div class="col yourCol"></div>
<div class="col"></div>
<div class="col myCol"></div>
<div class="col theirCol"></div>

Also if it's good for you, you can play with the selectors, watch the DIV café. Remember that in CSS, order matters more than anything else.

    
answered by 28.06.2018 / 17:29
source
2

You just have to be more specific in the hierarchy of the elements in the selector in order to apply different styles to the same classes:

.btn{
 background:#ccc;
 border:solid 1px #ccc;
 border-radius:4px;
}

.div-1 .btn{
 color:red;
}

.div-1 .div-2 .btn{
 color:blue;
}
<div class="div-1">
   <div class="div-2">
     <button class="btn">Button rojo</button>
   </div>
   <button class="btn">Button azul</button>
</div>

.div-1 .btn indicates that the style is applied to the element with class .btn that is inside an element with class .div-1 .

Whereas the second rule div-1 .div-2 .btn indicates that style is applied to the class .btn that is inside a class .div-2 that have as a parent a class .div-1 .

    
answered by 28.06.2018 в 17:23
1

Add a second class to the tag:

<div class="row row-1" data-toggle="modal" data-target="#modalAddDir" data-backdrop="static" (click)="addDir()" id="new_dir">
      <div class="col col-1" id="cir_mas">
        <div id="mas">+</div>
      </div>
      <div class="col col-2" id="con_nu_dir" >
        <div id="nueva_dir">
          Añadir una nueva dirección
        </div>
      </div>
    </div>
    
answered by 28.06.2018 в 17:29
1

By adding another option, you could even use properties like filtering the nth child where n is even (odd) or odd (odd)

.row .col:nth-child(even) {background: #CCC;}
.row .col:nth-child(odd) {background: #FFF;}
<div class="row" data-toggle="modal" data-target="#modalAddDir" data-backdrop="static" (click)="addDir()" id="new_dir">
  <div class="col" id="cir_mas">
    <div id="mas">+</div>
  </div>
  <div class="col" id="con_nu_dir" >
    <div id="nueva_dir">
      Añadir una nueva dirección
    </div>
  </div>
</div>
    
answered by 28.06.2018 в 17:39