...

CSS Classes Explained: The Beginner’s Guide

CSS classes are the foundation of web design. Whether you’re styling a simple button or crafting an intricate webpage layout, mastering CSS classes is essential.

Probably one of the first things to learn about CSS are CSS classes. Simply put, you can’t style a page in CSS without understanding classes, how they work and how to work with them.

In this article, we’ll explain what are CSS classes, why they’re important, and how to use them. We’ll also include practical examples to help you get started.

You can also watch the video below:

What are CSS classes?

A CSS class is a reusable label used to target one or multiple elements in HTML that share the same properties.

CSS classes are used to style those elements in CSS. When we say ‘style’ or ‘format’, we mean to assign a color, size, font, position and layout, effects like shadows etc.

You can think of a CSS class as of a set of instructions that tells the browser how specific elements should look. If you’re familiar with Illustrator or InDesign, it is something like a character or paragraph style, but it can be applied to any element, not just text.

You can assign a class to an image, a box or a container, header, footer and any other element and style every single property of that element.

For instance, with a CSS class, you can:

✅ Add colors, fonts and effects to text
✅ Style containers, images, buttons and more (for example add rounded corners and outlines)
✅ Create different layouts and adjust spacing, alignment and position of elements

How to Define and Use a CSS Class?

Defining and applying CSS classes is simple. Here’s how:

Option 1: Define a class in HTML and CSS

If you are using HTML to build your webpage, you can assign a class to a specific element using a class attribute.

For example, let’s assign a class “header-main” to a container (or commonly referred to as “div”) with a line of text that says “Welcome to My Website” in HTML:

<div class="header-main">Welcome to My Website</div>

We will see later some naming rules for classes. For now, let’s focus on how to define this class “header-main” in CSS:

how-to-write-css-classes-gif
  1. A class is called out with a dot in CSS (‘.’) followed by class name
  2. Curvy brackets open and close the code for styling that particular class
  3. Inside these curvy brackets, you then define the class properties, such as color, font-size, padding etc.
  4. First you declare a property, followed by a colon (“:”) and then define the value
  5. You then close the value or that line with a semi-colon (“;”)
  6. Repeat for as many lines and properties as necessary

Here is an example of a CSS class formatted in CSS:

.header-main {
    color: #333333;
    font-size: 24px;
    padding: 10px;
}

How to Create Class Names

Class names should be meaningful and descriptive and shouldn’t contain spaces.

Instead of spaces, you can use a dash (‘-‘) or an underscore (_). For example:

header-main
button-cta
header main

Option 2: CSS Classes in Webpage Builders

Even if you’re using tools like Elementor or WordPress, CSS classes remain incredibly useful. These platforms often assign default classes to elements, which you can find using browser developer tools like Inspect Element.

For example:

  1. Right-click an element on your page and select Inspect.
  2. Look for the class name in the highlighted HTML.
  3. Use that class in your CSS to modify the element’s style.

Alternatively, you can assign your own classes in the webpage builder and style them globally.

For example, Elementor gives you an option to assign your own custom class to any widget element. You can then go to your CSS sheet (for example using a free plugin Simple Custom JS and CSS) and style that class however you want.

This is where you can add a CSS class to any widget in Elementor:

screenshot-adding-css-class-elementor-widget

And here is where you can add a CSS class in WordPress to any block after selecting it (for example in a blog post):

screenshot-adding-css-class-wordpress-block

Try it yourself!

Below is an interactive exercise where you can examine the HTML code where we assigned a class to an HTML element and then styled it in CSS.

Click on the ‘CSS’ tab below and then change any of the values. For example, you can change the font-size, button color from blue to green or the padding and border-radius in pixels to see the result!

For example, we assigned the same class to both buttons in the preview. So, if you change the background color within that class, the change will affect both buttons. Or in other words, all the buttons and elements within that class!

See the Pen Blog post CSS classes exercise by Anait (@velvetmade) on CodePen.

Why are CSS classes useful?

CSS classes are essential because they make your code cleaner, flexible, more efficient, and easier to maintain and update.

Here’s why:

✅ Style Multiple Elements

Apply the same styles to multiple elements without duplicating code for each element.

For example, you can create a class called “.image-rounded” and you can assign this class to ALL the images on your website that you want to have rounded corners. You can then define a property for these rounded corners in CSS for this specific class.

Or, like in the example below, you can assign the same class “button-primary” in HTML to two different button elements and style them in the same way.

Here is how that structure would look like in HTML:

<div class="button-primary">Click Me</div>
<div class="button-primary">Learn More</div>

You could then use that class “button-primary” and define it in CSS as follows to format your two buttons:

.button-primary {
    background-color: #007BFF;
    color: white;
    padding: 10px 20px;
    border-radius: 5px;
    text-align: center;
}

✅ Make Global Updates From A Single Place

Change the appearance of all elements with a single class from one place in your CSS file.

For instance, updating the .button-primary color will affect every button using that class.

That way, you can apply global changes to all the elements within a class without going and individually adjusting each and every one of them.

✅ Override Default Styles

Classes allow for precise customization, overriding broader styles set by tags or other rules.

✅ Organize Your Code

Using well-named classes keeps your CSS and HTML structured, easy to understand and reusable.

Conclusion: Why CSS Classes Are Essential

Understanding CSS classes is a crucial step in your journey to mastering web design and development. By using classes effectively, you can make your code more organized, scalable, and reusable.

Even when using webpage builders, classes allow you to save time, apply global updates across all pages, and create highly customized designs. And of course, to create beautiful, custom designs where you can adjust virtually anything on your website.

Blog

Code Snippets

General Instructions

Step One: What I will be making

First click on the green ‘What I will be making’ button. This is how your end result should look like.

Step Two: What I need

Next, click on the ‘What I need’ button to get some basic parameters to use in the code. Such as the font family, colors, image URLs etc. These are just recommendations. Use your own judgement for the rest of the values, such as paddings, margins, spacing and size.

Step Three: Read the given HTML code

You don’t need to know how to write HTML for these exercises. However, you WILL need to take a look at the ‘HTML’ code in the exercise. You can switch from HTML to CSS tab in the Codepen screen. Then read the built HTML code for each exercise to understand the structure and find the classes that are defined for the elements that you will be editing. Use those predefined classes to style them in CSS.

Step Four: Click on the ‘CSS’ tab and start coding

Click on the ‘CSS’ tab in the Codepen screen and start typing your CSS code here, just below the ‘Write CSS code here’ comment. You can also delete this comment if you wish.

For example, if a ‘div’ has a class “text-content” in HTML, you can then style this class in CSS tab. Just don’t forget to add a dot before calling a class out (.text-content)

You should see a live preview of the CSS code that you are editing in the ‘Result’ tab to the right of the Codepen screen.

Step Five: Check the solution with the CSS code

If you are stuck, click on the ‘Show me the code’ button. This will reveal the code that you can also completely copy and paste in the CSS tab of the exercise to see the result.

Or you can just go back to the lessons and watch them again.

Have fun and feel free to reach out!

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.