Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Classes in HTML?

When you first start learning programming, you may have come across the term "classes." In this blog post, we'll explore what classes are in the context of HTML and how to use them to make your web development journey more manageable and enjoyable. We'll try to avoid jargon as much as possible, but if we do use any, we'll make sure to explain it.

First, let's talk about HTML. HTML, or HyperText Markup Language, is the standard language for creating web pages. It allows you to create the structure of your website using elements like headings, paragraphs, links, images, and more. These elements are represented by tags, which are surrounded by angle brackets, like <p> for a paragraph or <h1> for a heading.

Now, let's dive into classes. Classes in HTML are a way to group and style similar elements on a web page. They are not to be confused with classes in other programming languages such as Python or Java, which are used to create objects and define their behavior. In HTML, classes are used to apply CSS (Cascading Style Sheets) rules to multiple elements at once. This makes it easier to maintain a consistent design throughout your website and makes your code more accessible.

To better understand the concept of classes, let's use an analogy. Imagine you're organizing a party, and you have different types of guests attending: friends, family, colleagues, etc. You want to assign a specific color to each group so that it's easier to identify them. In this case, the guest groups are like classes, and the colors are like the styles applied to them.

Let's see how we can create and use classes in HTML with actual code examples.

Creating a class

To create a class in HTML, you need to assign a unique name to the class attribute of the elements you want to group together. For example, let's say we want to create a class for all the headings in our website. We can do this by adding the class attribute to the <h1> tags, like this:

<h1 class="main-heading">Welcome to my website</h1>
<h1 class="main-heading">About me</h1>
<h1 class="main-heading">My projects</h1>

In this example, we've created a class called main-heading and assigned it to three <h1> elements. The class name can be anything you like, as long as it follows a few simple rules:

  • It must start with a letter or an underscore (_)
  • It can contain letters, digits, hyphens (-), and underscores (_)
  • It must not contain spaces

Applying styles to a class

Now that we've created a class, let's apply some styles to it. To do this, we'll use CSS. If you're not familiar with CSS, think of it as a way to describe how HTML elements should look. You can write CSS rules in a separate file (with a .css extension) or directly in your HTML file, inside a <style> tag.

Let's create a simple CSS rule to change the color and font size of our main-heading class:

.main-heading {
  color: blue;
  font-size: 32px;
}

Here, we've created a CSS rule targeting the .main-heading class (notice the dot . before the class name). This means that all elements with the main-heading class will have their text color set to blue and their font size set to 32 pixels.

To apply this CSS rule to our HTML file, we need to include it inside a <style> tag:

<!DOCTYPE html>
<html>
<head>
  <style>
    .main-heading {
      color: blue;
      font-size: 32px;
    }
  </style>
</head>
<body>
  <h1 class="main-heading">Welcome to my website</h1>
  <h1 class="main-heading">About me</h1>
  <h1 class="main-heading">My projects</h1>
</body>
</html>

Now, if you open this HTML file in your browser, you'll see that all the headings with the main-heading class have a blue color and a font size of 32 pixels.

Using multiple classes

You can also assign multiple classes to a single element by separating the class names with spaces. This is useful when you want to apply different styles to an element based on different conditions or when you want to combine styles from multiple classes.

For example, let's say we want to create a new class called highlight to emphasize specific headings:

<h1 class="main-heading highlight">Welcome to my website</h1>
<h1 class="main-heading">About me</h1>
<h1 class="main-heading">My projects</h1>

We can create a new CSS rule for the highlight class and apply it in addition to the main-heading class:

.highlight {
  font-weight: bold;
}

Now, the first heading will have the styles from both the main-heading and highlight classes, making it blue, 32 pixels in font size, and bold.

Conclusion

In this blog post, we've learned about classes in HTML and how to use them to group and style similar elements on a web page. We've seen how to create classes, apply styles to them using CSS, and how to use multiple classes on a single element.

Classes are a powerful feature in HTML and CSS that can help you create more maintainable and consistent designs for your websites. As you continue your web development journey, you'll find that using classes effectively can save you time and effort, allowing you to focus on creating great content and experiences for your users.