Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Apply CSS to first type and other conditions together

I got a few divs with class A, then I want to check that whether the first A has class B or not, if yes then apply some styles. Would like to use CSS only.

Update: the first div should be something else.
For example,

Condition 1:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="container">    
  <div class="C"></div>
  <div class="A"></div> <!-- don't apply style -->
  <div class="A B"></div>
  <div class="A"></div>
  <div class="A B"></div>
</div>

Condition 2:

<div class="container">
  <div class="C"></div>    
  <div class="A B"></div> <!--apply style -->
  <div class="A"></div>
  <div class="A"></div>
  <div class="A"></div>
</div>

>Solution :

EDIT

As Christian Vincenzo Traina suggested in comment, select the second nth-of-type A that has the class B with .A:nth-of-type(2).B.

body {
  display: flex;
  justify-content: space-around;
}

.container {
  margin-bottom: 50px;
}

.A,
.C {
  width: 20px;
  height: 20px;
  border: 1px solid black;
}

.A:nth-of-type(2).B {
  background-color: orange;
}
<div class="container">
  <div class="C"></div>
  <div class="A"></div>
  <!-- don't apply style -->
  <div class="A B"></div>
  <div class="A"></div>
  <div class="A B"></div>
</div>
<div class="container">
  <div class="C"></div>
  <div class="A B"></div>
  <!--apply style -->
  <div class="A"></div>
  <div class="A"></div>
  <div class="A"></div>
</div>

OLDER ANSWER

note : the older question asked without the div.C class

Select the first-of-type A that has the class B with .A:first-of-type.B

body {
  display: flex;
  justify-content: space-around;
}

.container {
  margin-bottom: 50px;
}

.A {
  width: 20px;
  height: 20px;
  border: 1px solid black;
}

.A:first-of-type.B {
  background-color: orange;
}
<div class="container">
  <div class="A"></div>
  <!-- don't apply style -->
  <div class="A B"></div>
  <div class="A"></div>
  <div class="A B"></div>
</div>
<div class="container">
  <div class="A B"></div>
  <!--apply style -->
  <div class="A"></div>
  <div class="A"></div>
  <div class="A"></div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading