Beginner's Guide: Using CSS Selectors
Why CSS Selectors are Needed?
Basically, selectors are the "pointing finger." Without them, if you tell the browser "make text red," every single word on your page will turn red and you don’t want it right. It would look like a crime scene. Selectors let you be specific and keep your code clean instead of making a total mess of your HTML.
The Basic Selectors
Element Selector
This is where you just select name the tag and all tags of that name selected.
How it looks:
p { color: blue; }Every single paragraph on your site becomes blue.
Like saying "All boys in the school will wear white shirts." Simple but applies to everyone.
CSS
/* All paragraphs will have this style */
p {
font-family: 'Arial';
color: #333;
}
Class Selector
Classes are for groups. You can give the same class name to many elements.
.special-text { font-weight: bold; }(Always use the dot)Any element where you write
class="special-text"becomes bold.Like a "Cricket Team" jersey. Anyone wearing it is part of the gang.
CSS
/* Notice the dot! */
.hot-item {
color: red;
font-weight: bold;
text-transform: uppercase;
}
ID Selector
IDs are unique. One ID should only be used for one element.
How it looks:
#main-boss { color: gold; }(Always use the hashtag!)Only that one specific element gets the style.
Like your Aadhaar Card or Roll Number. Only one person has it.
CSS
/* Notice the hashtag! */
#main-branch-address {
background-color: yellow;
border: 2px solid black;
padding: 10px;
}
Group Selectors
Why write the same code 10 times? If you want h1, h2, and p to all be centered, just use a comma.
Example:
h1, h2, p { text-align: center; }Benefit: Saves effort and keeps the file small.
CSS
/* Comma is the secret ingredient here */
h1, h2, h3 {
color: darkblue;
text-align: center;
}
Descendant Selectors
This is for targeting something inside something else.
Example:
div p { color: green; }Meaning: "Only find those paragraphs that are sitting inside a div."
Analogy: Like saying "Only the students who are currently inside the Canteen." The ones in the classroom are safe.
CSS
/* Find the <span> that is sitting inside a <div> */
div span {
background-color: lightgreen;
}
Priority (Who Wins the Fight?)
Sometimes, you give two different rules to the same element. CSS has a "Ranking" system:
ID (#) is the Principal (Highest Power)
Class (.) is the Teacher
Element is the Student (Lowest Power)
If an ID says "be blue" and a Class says "be red," the element will be Blue because the ID is the bigger boss.
Beginner’s Problem Time:
"If you not using selectors properly, your website looking like a garbage. Many beginner is putting same ID on 100 places, but that is very much wrong for your code healthiness. Please use classes for groups and ID only for the special ones."
so, if you are here you might completed the blog hope this explanation give you a little confident. Don’t worry if you didn’t get much of it. Only if you got a upper view this is more than enough because this thing only cleared when you implements it by your own. when i was a beginner then i also took time to grab them then, i use it for my self and i had the clear vision of these tags and know where to use them.
so, for now go to your code editor and use them by yourself and create some little projects. just keep doing because if a intelligent person will do a work 10 time and a dumb person do it 100 times then, the dumb person will become the smartest one in the game. think what if you work 100 time and you are intelligent too. "just do it”.