IDs Vs Classes: Which Should You Use to Target HTML Elements?

Dan Gold

Written by Dan

When it comes to CSS, there are a few different ways that you can target HTML elements. You can use element selectors, class selectors or ID selectors.

// Element selector
p {
  display: block;
}

// Class selector
.text {
  text-align: center;
}

// Element selector
#id {
  position: relative;
}

But which one should you use? In this blog post, we’ll take a look at the pros and cons of each so that you can make an informed decision about which one is right for your project.

Element Selectors: The Pros and Cons

Element selectors are the most basic type of CSS selector. To use an element selector, you simply specify the name of the element you want to style.

For example, if you wanted to style all of the <p> elements on your website, you would use the following CSS:

p {
  /* Styles go here */
}

The main advantage of using element selectors is that they are easy to understand and use. Even if you’re new to CSS, you should be able to figure out how to use element selectors with ease.

However, there are also some disadvantages to using element selectors.

The main downside is that they are not very flexible. Once you define a style for an element using an element selector, that style will be applied to every instance of that element on the page—whether you want it to be or not.

For example, if you defined a style for all <p> elements using an element selector and then later added a <p> element to your page that should not have that style applied to it, you would have to override the style using a more specific selector (which we’ll talk about next).

This can sometimes make your CSS code more bloated and difficult to maintain.

Class Selectors: The Pros and Cons

Class selectors are more specific than element selectors, but less specific than ID selectors. To create a class selector, you first need to add a class attribute to the HTML element(s) you want to target. For example:

<p class="intro">This is my introductory paragraph.</p>
<p class="body-copy">This is some body copy.</p>

Once you’ve added the class attribute(s), you can then create your class selector(s) in your CSS file like so:

.intro {
  /* Styles go here */
}

.body-copy {
  /* Styles go here */
}

One advantage of using class selectors is that they give you much more control over which elements on your page get styled than element selectors do.

This is because you can add multiple classes to a single HTML element and then decide which styles should be applied based on those classes. For example:

<p class="intro body-copy">This paragraph has two classes.</p>

In this case, both the .intro and .body-copy styles would be applied to the <p> element since it has both of those classes.

This allows for much more flexibility when styling your pages since you’re not restricted by HTML tags when deciding which styles should be applied.

Additionally, if there are certain styles that are only supposed to be applied in certain situations (e.g., when a user hovers over an element), using classes gives you an easy way to toggle those styles on and off as needed without having to write additional CSS rules.

ID selectors: The Pros and Cons

ID selectors are the most specific type of CSS selector. To create an ID selector, you simply add a # symbol before the name of the element you want to target. For example:

<p id="intro">This is my introductory paragraph.</p>

Once you’ve added the ID attribute to the HTML element(s) you want to target, you can then create your ID selector(s) in your CSS file like so:

#intro {
  /* Styles go here */
}

Since ID selectors are more specific than class selectors, they have a few advantages over them.

First and foremost, ID selectors are much more flexible than class selectors. This is because you can only have one ID selector per HTML element, whereas you can add multiple classes to a single HTML element.

This allows for much more granular control over which styles are applied to which elements on your page.

However, there are also a few disadvantages to using ID selectors.

Because you can only use one ID selector at a time, your CSS might not be flexible if you leverage some type of CSS framework or style library. Even if you use your own, you might be boxing yourself in to inflexible styles.

Last updated

November 10th, 2022