Selectors in CSS

Arjun SharmaArjun Sharma
3 min read

Alright kiddo, imagine you have a magic wand that can pick out specific toys from your big toy box. CSS selectors are a bit like that magic wand, but for websites instead of toys.

we have five type of selectors :

  1. Tag Selector: It's like saying, "Hey wand, I want all my LEGO toys!" The wand picks out all the LEGO toys from your toy box.

    Imagine we have a bunch of animal pictures, and we want to give each one a different style.This is like saying, "Hey, I want to style all the lion pictures!"

    Here, img is the tag selector, and it adds an orange border to all the animal pictures.

  2. Class Selector: Now, let's say you have some LEGO cars and some LEGO houses. You can say, "Wand, give me all my red LEGO toys!" The wand will find all the red LEGO toys, no matter if they're cars or houses.

    Now, let's say we want to style only the pictures of lions wearing hats.In this case, .lion-hat is the class selector. We put this class on all the pictures of lions wearing hats, and it gives them a dashed blue border.

  3. ID Selector: Imagine you have a special LEGO spaceship. You can say, "Wand, bring me my special spaceship!" The wand knows exactly which one you're talking about because it's unique.

    Suppose we have a very special picture of a dancing elephant.Here, #dancing-elephant is the ID selector. It adds a dotted pink border to the picture of the dancing elephant because it's unique and has that special ID.

  4. Attribute Selector: Sometimes, you might have some toys with special tags. Like maybe you have LEGO toys with stickers. You can tell the wand, "Find me all my LEGO toys with stickers!" And it will find them for you.

    Imagine we have a zoo website with animal pictures, and some of them have special tags like "cute" or "funny".Now, we want to style these pictures differently based on their tags. Attribute Selector is like saying, "Hey, I want to style all the pictures of animals that are tagged as cute!".Here, [class="cute"] is the attribute selector. It adds a pink border to all the pictures of animals that have the "cute" tag.

  5. Universal Selectors: Alright! Let's imagine our zoo website again. This time, we want to make a change that affects every element on the page.

    In this CSS code, the * is the universal selector. It's like saying, "Hey, I want to style everything on the webpage!"

    Here's what each line does:

    1. margin: 0; - It removes all the default spacing around every element, making everything stick to the edges nicely.

    2. padding: 0; - It removes any extra space inside elements, so they fit content snugly.

    3. box-sizing: border-box; - It tells elements to include their padding and border in their total width and height, making layout calculations easier.

    4. font-family: Arial, sans-serif; - It sets the default font for all text on the webpage, making sure it looks nice and readable.

So, the universal selector * helps us make big changes that affect everything on the webpage, making sure our zoo website looks clean and consistent!

0
Subscribe to my newsletter

Read articles from Arjun Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arjun Sharma
Arjun Sharma

"Enthusiastic QA Analyst with a profound interest in backend development, driven by a passion for creating robust and reliable software systems. Committed to continuous learning and exploration of backend technologies, I find joy in unraveling complexities and optimizing performance. Beyond the code, I am dedicated to mentoring kids, sharing my knowledge, and igniting their curiosity for technology. Through insightful blog posts, I aim to inspire others while fostering a supportive community of learners. Join me on this journey as we explore the world of backend development and empower the next generation of tech enthusiasts!"