Learn CSS: Class and Id Selector | Day 12 | Combining Selector and Body Selector in CSS
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;
}
-
13:23
NorbertBM
1 year ago $0.02 earnedMaster CSS has Selector with Examples
126 -
9:13
NorbertBM
1 year agoMaster the CSS is() Selector in 9 Minutes
105 -
6:51
LeeMorganIO
2 years agoCSS 002: Selectors
18 -
11:19
LearnCode.academy
1 year agoCSS SELECTORS MADE EASY - HTML CSS Tutorial for Beginners
5 -
46:48
Landon Schlangen
1 year agoLearn CSS Pseudo Selectors by Building a Balance Sheet | FreeCodeCamp
222 -
11:18
LearnCode.academy
1 year agoJavascript Selectors - Javascript Tutorial for Beginners With Examples
2 -
3:25
XoaX.net
2 years ago $0.01 earnedCSS Lesson 1: Inline Styles
201 -
3:00
AHMEDKAZEKA97
2 years agoFREE FULL COURSE Understanding HTML and CSS
49 -
6:23
XoaX.net
2 years agoCSS Lesson 2: Color Specifications
502 -
3:28
XoaX.net
2 years agoCSS Lesson 0: What is CSS?
148