Learn CSS: Class and Id Selector | Day 12 | Combining Selector and Body Selector in CSS

6 months ago
14

Class and ID Selectors in CSS
Class and ID selectors are fundamental building blocks of CSS styling. They allow you to target specific elements in your HTML document and apply styles to them. Understanding their differences is crucial for writing efficient and maintainable CSS code.

Class Selector
Syntax: .class-name
Selection: Applies styles to all elements with the specified class attribute.
Use case: When you want to style multiple elements with the same characteristics, like buttons, headings, or specific sections.
Example:
CSS
.button {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
}
Use code with caution. Learn more
This code will apply the specified styles (background color, color, padding, border, and border radius) to all elements in your HTML document with the class "button."

ID Selector
Syntax: #id-name
Selection: Applies styles to a single unique element with the specified ID attribute.
Use case: When you want to style a specific element only once on your page, like the navigation bar, header, or footer.
Example:
CSS
#header {
background-color: #333;
color: #fff;
padding: 20px;
}

Loading comments...