0

My question is the CSS syntax. I want to add a CLASS(.fade) to multiple ID(titletext1 and titletext2 or more...). The way I write it does not work. What is the correct syntax if I do not want to group them together, not writing them in separate line ?

document.getElementById('titletext1').classList.add('fade');
document.getElementById('titletext2').classList.add('fade');
#titletext1.fade,
#titletext2.fade {
  opacity: 1;
  top: 5px;
}
<h1 class="titletext" id=titletext1>This is a Title!!</h1>
<h1 class="titletext" id=titletext2>A second Title!!</h1>

Thanks

catcattt
  • 77
  • 9

1 Answers1

0

Simply join the two classes together with a . separator as #{id}.{class}.{class}:

#titletext1.titletext.fade,
#titletext2.titletext.fade {
  color: red;
}
<h1 class="titletext fade" id=titletext1>This is a Title!!</h1>
<h1 class="titletext fade" id=titletext2>A second Title!!</h1>

<h1 class="titletext" id=titletext1>Without fade</h1>
<h1 class="titletext" id=titletext2>Without fade</h1>

<h1 class="fade" id=titletext1>Without titletext</h1>
<h1 class="fade" id=titletext2>Without titletext</h1>

Though note that without the ID, you can combine the two selectors and still target both elements:

.titletext.fade {
  color: red;
}
<h1 class="titletext fade" id=titletext1>This is a Title!!</h1>
<h1 class="titletext fade" id=titletext2>A second Title!!</h1>

<h1 class="titletext" id=titletext1>Without fade</h1>
<h1 class="titletext" id=titletext2>Without fade</h1>

<h1 class="fade" id=titletext1>Without titletext</h1>
<h1 class="fade" id=titletext2>Without titletext</h1>
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71