19

Is it possible to add more than one class in html? Here is what i've tried:

<a href="#" class="class1" class="class2">My Text</a>

Thanks! :)

A-Tech
  • 806
  • 6
  • 22
Stefan
  • 250
  • 1
  • 2
  • 9

4 Answers4

43

Yes, it is possible, but you can only declare the class attribute once per HTML element. Just separate the classes you want to apply by a space.

<a href="#" class="class1 class2">My Text</a>

If you declare the class attribute more than once, all definitions beyond the first will be ignored, so in your code .class2 will not be applied to your link.

connexo
  • 53,704
  • 14
  • 91
  • 128
3

Yes of course you can! But you do it all inside a single class attribute:

<a href="#" class="class1 class2">My Text</a>
emcas88
  • 881
  • 6
  • 15
3
<a href="#" class="class1 class2">My Text</a>
HarrisJT
  • 199
  • 10
0

You can read about the global class attribute here.

The class global attribute is a space-separated list of the classes of the element.

Use a single class attribute and separate additional classes with a space.

<a href="#" class="class1 class2 class3">My Text</a>

coderfin
  • 1,708
  • 19
  • 24