1

Is there anyway we can target the container of an element? I seems can't find a way, here is an example:

The HTML:

<div class="wrapper">
   <div class="content" id="TopSection">
      <span>Box A</span>
   </div>
</div>

<div class="wrapper">
   <div class="content" id="BottomSection">
      <span>Box B</span>
   </div>
</div>

The wrapper and the content are both has styling class, for example:

.wrapper {
    background-color: black;
 }

 .content {
    padding: 10px 20px;
 }

How to change the background color of the wrapper on the BottomSection only, without adding anther wrapper class, i.e wrapperB? so can we target the wrapper that contains that ID only?

j08691
  • 204,283
  • 31
  • 260
  • 272

2 Answers2

1

Try this-

.wrapper {
background-color: black;
}

.wrapper:nth-child(2) {
background-color: orange;
}

.content {
padding: 10px 20px;
}
Arif Khan
  • 67
  • 1
  • 12
1

You can target parents with jQuery, to add a class that you could then target with CSS:

$('#BottomSection').closest('.wrapper').addClass('changeMe');
Steve Ray
  • 158
  • 8