skip to Main Content

I am trying to alter the css on a div that doesn’t have a unique class name or id and don’t have the ability to give it an id as it is part of a library. This is the layout of the item I am trying to alter.

I am wanting to add some CSS to <div class='thirdClass' />. Can you use nth-of-type? I couldn’t get that to work.

<button id='myUsefulBtn'>
  <div class='firstClass' />
  <div class='secondClass' />
  <div class='thirdClass' />
</button>

I was able to get it working with this. Not sure if there is a better way though…

#myUsefulBtn div:nth-child(3) {
  color: red;
}

2

Answers


  1. You could use the following to access that div:

    #myUsefulBtn .thirdClass {
        color: red;
    }
    
    Login or Signup to reply.
  2. Well, to target <div class='thirdClass' /> without a unique class or ID, you can use nth-child as valid approach if the structure remains consistent. Meaning, it is the best way if this is the case.

    #myUsefulBtn div:nth-child(3) {
      color: red;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search