Modify TAG that does not have such a class

Asked

Viewed 29 times

1

When we want to modify a div, and it has such a class, it’s easy.

.umaclass{
   /*Formatação*/
}

But let’s assume that I want to take a div that does not have such a class, without modifying the ones that have the class... How do I?

For example:


I have several Ivs with the same class, testing and testing :

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

<div class="teste"></div> //SOMENTE ESSA SER MODIFICADA, SEM MODIFICAR AS OUTRAS

But I want to modify only the test class but without modifying the ones that have the test class?

2 answers

3


Anderson Carlos Woss' answer will probably meet your needs, but if you want to know, there is also the :not in css, look:

.teste:not(.testando){
  background-color: red;
  height: 50px;
  width: 100px;
}
<div class="teste testando"></div>
<div class="teste testando"></div>
<div class="teste testando"></div>

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

It accepts a simple selector as argument, and selects all elements that do not fit that selector

  • Thank you, Artur, what I wanted to do, with the not worked!

  • 1

    @Lucascarvalh :)

2

CSS works from the most generic to the most specific. That is, you can style all the div with only div { ... } and then stylize .umaclass overwriting the desired properties.

Take an example:

.teste {
  display: none;
}

.testando {
  display: block;
}
<div class="teste testando">1</div>
<div class="teste testando">2</div>
<div class="teste testando">3</div>

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

Realize that the last div maintains the standard stylization, defined in .teste, while the others were stylized according to .testando.

  • But for example. I want to put a None display in the test class but that the others, who have the test class, keep showing up.

  • I got it from Arthur’s not :)

  • 1

    @Lucascarvalho Not is the best way to do this, but what he meant is that you can put the class of the None display only on the object you don’t want to appear that it will overwrite the display for you.

  • 1

    @Lucascarvalho the functioning is the same. You define the behavior of .teste as display: none and then specializes the style of .testando with display: block, so that such elements continue to appear. The term specializes is because once the elements .testando also have the class .teste, they must possess their style. That is, part of a generic style, .teste, for a more specific, .testando. I changed the answer to that example.

  • Got it, seeing here, I can do it with the same two.

Browser other questions tagged

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