
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to apply CSS style to the different elements having the same class name in HTML?
HTML classes are global attributes that are used by HTML tags to specify a list of classes that are case-sensitive in nature. These classes are then used by CSS, to apply styling to that particular tag that has the class, and by Javascript, to manipulate the HTML element’s behavior, interactivity, or styles.
Approach 1; Using the dot(.) selector
In this approach, we will simply use the dot (.) selector to select multiple elements having the same class names, and apply the same set of styles to them using CSS.
Example
In this example, we will select a “p” tag and a “span” HTML tag using their class, and give them a text color “red” using CSS.
<!DOCTYPE html> <html lang="en"> <head> <title>How to apply CSS style to the different elements having the same class name in HTML?</title> <style> .sample { color: red; } </style> </head> <body> <p class="sample">This is p tag content!</p> <span class="sample">This is span tag content!</span> </body> </html>
Approach 2: Using “p” tage and “span” HTML tag
In this approach, we will apply a different set of styles to the elements that have the same class names using CSS. For this, we will use the HTML tag name, followed by the dot (.) selector to select a particular element and give the desired CSS styles to it.
Example
In this example, we will select a “p” tag and a “span” HTML tag using their class, and give them a different text color using CSS.
<!DOCTYPE html> <html lang="en"> <head> <title>How to apply CSS style to the different elements having the same class name in HTML?</title> <style> p.sample { color: red; } span.sample { color: green; } </style> </head> <body> <p class="sample">This is p tag content!</p> <span class="sample">This is span tag content!</span> </body> </html>
Conclusion
In this article, we learned about how to select HTML elements from their class names, and how to apply the same as well as different CSS styles to them, using 2 different approaches. In the first approach, we used the dot (.) selector to select multiple elements having the same class names, and applied the same styles to them, In the second approach, we used an HTML tag name followed by the dot (.) selector to apply different CSS styles to elements having the same class names.
- Related Articles
- How to apply CSS style using jQuery?
- Apply style to the parent if it has a child with CSS and HTML
- Can different HTML elements have the same ID?
- Replace whitespace in different elements with the same class in JavaScript?
- CSS Selector to Select Elements Not Having Certain Class / Attribute / Type
- How to use inline CSS (Style Sheet) in HTML?
- How to use internal CSS (Style Sheet) in HTML?
- How to use CSS style in PowerShell HTML Output?
- How to name different lines in the same plot of Matplotlib?
- How to find the row variance of columns having same name in R matrix?
- How to find the row median of columns having same name in R matrix?
- How to find the row means of columns having same name in R matrix?
- How to find the row total of columns having same name in R matrix?
- How to use inline CSS style for an element in HTML?
- Atoms of elements having the same mass number but different atomic number are called ?
