How not to select an element in css?

Asked

Viewed 160 times

-4

I want to select all the elements that are inside a div, but there’s one specific that I don’t want to be selected.

How can I do this "descent"?

  • Friend there are several ways for this, if you do not put at least the HTML of the situation you have there is difficult to answer you. Please edit the question and put the code, even if this code is a practical example closer to what you have.

  • Ask the question an example of code and what you do not want to be selected.

  • Possible duplicate of CSS selector for tables

2 answers

2

You can use the not

Let me give you an example below:

.lista *:not(:nth-child(2)) {
  color: red;
}
<ul class="lista">
  <li>Um</li>
  <li>Dois</li>
  <li>Três</li>
</ul>

The :not it will not apply the style to the element I selected, which in this case is the second li, picking on nth-child(2)

0


Hugo, the ideal would be for you to put the code in your question so that it can be answered.

There are many ways to achieve the desired effect.

Below is a way to do this.

p:not(.no-bg) {
   background-color: red;
}
<!DOCTYPE html>
<html>
  <body>
    <div>
      <p>parágrafo 1</p>
      <p>parágrafo 2</p>
      <p>parágrafo 3</p>
      <p>parágrafo 4</p>
      <p class="no-bg">parágrafo 5</p>
      <p>parágrafo 6</p>
      <p>parágrafo 7</p>
      <p>parágrafo 8</p>
      <p>parágrafo 9</p>
      <p>parágrafo 10</p>
    </div>
  </body>
</html>

In the above code a red background has been applied in all paragraphs of div. For this, a rule was created CSS to be applied to the elements "P". In this rule, the selector has also been added ":Not", so that in the element with the class "no-bg" the background rule was not applied.

I believe that with this example, you can adapt the code to your need, remembering that, there are many other ways to do this besides the above.

Browser other questions tagged

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