- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Class Selector
The class selector is one of the most powerful and frequently used tools in a web developer's toolkit. It allows you to select and style HTML elements based on a specific class attribute value. Unlike the ID selector, which targets a single unique element, the class selector is designed for reusability. You can apply the same class to as many elements as you like across your entire website.
In CSS, the class selector is identified by a period (.) followed immediately by the name of the class. Here is the basic structure:
CSS Syntax
.classname {
/* Style rules go here */
}
.main-navigation). It is the standard convention in modern web development and makes your code much easier to read.
For a practical example, imagine you are building a website and want several different buttons to look consistent. Instead of styling each button individually, you create a generic .button class:
CSS
.button {
background-color: #3498db;
color: #ffffff;
padding: 12px 24px;
border-radius: 4px;
text-decoration: none;
display: inline-block;
}
By applying class="button" to your HTML elements, they will all inherit these styles instantly:
HTML
<button class="button">Click Me</button>
<a href="#" class="button">Download Now</a>
.) inside the HTML class attribute. Remember: Use the dot in your CSS file to target the class, but leave it out in your HTML.
Correct:
<div class="container">Incorrect:
<div class=".container">
You can also combine class selectors with element selectors to be more specific. This is useful when you want a class to behave differently depending on which tag it is attached to. For example, you might want a class named .highlight to look different on a paragraph than it does on a heading:
CSS
p.highlight {
color: blue;
font-weight: bold;
}
h2.highlight {
background-color: yellow;
}
p.highlight) increases specificity. This means these rules will override more general class rules, which can sometimes make your CSS harder to debug if your stylesheet grows too complex.
One of the most effective ways to use classes is by assigning multiple classes to a single HTML element. You do this by separating the class names with a space within the same attribute. This allows you to "stack" styles, creating modular and scalable code.
HTML
<div class="card shadow rounded-corners">
This div uses three different classes to define its appearance.
</div>
In this example, the <div> will receive the styles defined in .card, .shadow, and .rounded-corners simultaneously. This "utility-first" approach is the foundation of modern CSS frameworks like Tailwind CSS.
.error-message instead of .red-text. If you decide later that errors should be orange, you won't have to rename your classes in every HTML file.