1

I need to apply a style only for the first child of the entire page, so for example if i have a class '.my-icon' that appears in multiple other divs - then i want them to not be affected so only the first-child "ever" will be affected, but without using id or jquery.

    <div>
      <div>
        <i class="my-icon">*</i> // only this element will be affected.
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
      <div>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
      <div>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
    </div>
RoyBarOn
  • 919
  • 3
  • 24
  • 63
  • I believe this answer may be of some help to you. You'll obviously have to change class names and such to get it to work for your situation: https://stackoverflow.com/a/8539107/2437278 – HighHopes Jun 17 '19 at 05:49

5 Answers5

3
<style>
div:nth-of-type(1) >.my-icon:first-child {
  color: lime;
  background-color: black; 
  padding: 5px;
}
</style>
<div>
  <div>
    <i class="my-icon">*</i> // only this element will be affected.
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
  <div>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
  <div>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
</div>
2

.my-icon:nth-of-type(1)
{
    border:5px solid red;
}
<div>
  <div>
    <i class="my-icon">*</i> // only this element will be affected.
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
  <div>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
  <div>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
</div>

div:nth-of-type(1) >.my-icon:first-child {
  border:5px solid red;
}
<div>
  <div>
    <i class="my-icon">*</i> // only this element will be affected.
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
  <div>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
  <div>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
    <i class="my-icon">*</i>
  </div>
</div>
shivani
  • 980
  • 1
  • 8
  • 29
1

It's best if you can add another selector but if not then you can try the following snippet:

div:first-of-type > div:first-of-type > .my-icon:first-of-type {
  color: red;
}
<div>
      <div>
        <i class="my-icon">*</i> // only this element will be affected.
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
      <div>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
      <div>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
    </div>
DSCH
  • 2,146
  • 1
  • 18
  • 29
1

It's simple, use :first-child for your div and your icon

div>div:first-child .my-icon:first-child{
  color:red;
}
<div>
      <div>
        <i class="my-icon">*</i> // only this element will be affected.
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
      <div>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
      <div>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
        <i class="my-icon">*</i>
      </div>
    </div>
Itay Gal
  • 10,706
  • 6
  • 36
  • 75
1
div:first-child > .my-icon:first-child {
  /*your code here*/
}

It's simple, You can find the first div then find the first class .my-icon in it.

Varun Raval
  • 134
  • 6