name name
name name

Reputation: 37

Select all a tags that are in certain class

How do I select with css all a tags that are in a div that has class = class1

Let's say I have this structure(just an example):

 <div class="class1"> 
         <div> <a>should be selected</a> </div>
         <p><span> <a>should be selected</a> </span></p>  
         <a>should be selected</a>
 </div>
 <a>should not be selected</a>

How do I select all a elements that are in div with class1 and not somewhere else. I tried to do .class1 > a { } but it doesn't select all a tags I need

Upvotes: 3

Views: 6325

Answers (2)

Erick Martinez Sr.
Erick Martinez Sr.

Reputation: 37

Does this only work for a tags? Would it work for selecting a div

Upvotes: -1

DaniP
DaniP

Reputation: 38252

Your selector

.class > a

.class1 > a {
  color:red;
  }
<div class="class1">
  <div> <a>should be selected</a> 
  </div>
  <p><span> <a>should be selected</a> </span>
  </p>
  <a>Just this will be selected</a>
</div>

Just target the direct descendents that are a tags inside .class . Just travel one level deep.

With

.class a

.class1 a {
  color: red;
}
<div class="class1">
  <div> <a>should be selected</a> 
  </div>
  <p><span> <a>should be selected</a> </span>
  </p>
  <a>should be selected</a>
</div>

You select all a tags inside .class. All levels.

Upvotes: 7

Related Questions