Can an HTML element have multiple classes?


#1

Question

Can an HTML element have multiple classes?

Answer

Yes! An HTML element can have multiple classes. To give an element multiple classes we use this syntax: class="class-one class-two class-three ...". For example, in the following code we are applying three styles from three different class selectors on the same element:
HTML:

<!DOCTYPE html>
<html>
<head>
  <title>My Site</title>
</head>
<body>
  <h1 class="greeting hello-world heading">Hello World</h1>
</body>
</html>

CSS:

.greeting {
  font-family: monospace;
}

.hello-world {
  color: blue;
}

.heading {
  font-size: 34px;
}