CSS Selectors MasterClass | Sigma Web Development Course - Tutorial #17

CodeWithHarry
12 Oct 202313:59

Summary

TLDRThis video script delves into the fundamentals of CSS selectors, essential for applying styles to web elements. It covers element, class, and ID selectors, explaining how they target specific elements or groups. The instructor also introduces child, descendant, and universal selectors, emphasizing their importance in web development. Furthermore, pseudo-selectors are explored, demonstrating how they alter styles based on user interactions like hovering or visiting links. The script promises to build on these concepts in future lessons, aiming to transition to real-world web app development.

Takeaways

  • 📌 CSS selectors are used to select HTML elements to which styles will be applied.
  • 🔑 You can select elements by their ID, class, element name, or by their relationship to other elements.
  • 🎨 The video emphasizes the importance of understanding selectors for efficient CSS styling.
  • 📝 The instructor demonstrates creating a new HTML file and applying internal CSS for direct visibility of changes.
  • 🟥 Element selectors are basic and apply styles to all elements of a certain type, which can have drawbacks if specificity is needed.
  • 🔲 Class selectors allow for styling specific elements by assigning a class name, offering more targeted styling.
  • 🎯 ID selectors are used to target a single element with a unique ID, ensuring that styles are applied only to that element.
  • 👶 Child selectors apply styles to direct children of a specified element, not to all descendants.
  • 🌐 Descendant selectors style any element that is a descendant of a specified element, regardless of its depth in the hierarchy.
  • 🌀 Universal selectors apply styles to all elements on the page, such as setting default margin and padding to zero.
  • 🔄 Pseudo-selectors add interactivity, such as styling links on different states (visited, active, hover) and targeting first child elements.
  • 📚 The script suggests further research on pseudo-classes for a deeper understanding, with additional resources provided in the description or pinned comments.

Q & A

  • What are CSS selectors used for?

    -CSS selectors are used to select HTML elements to which styles will be applied.

  • How can you select an element by its name in CSS?

    -You can select an element by its name using the element selector, which targets all elements of that type.

  • What is the purpose of using an ID selector in CSS?

    -The ID selector is used to target a specific element with a unique ID, allowing you to apply styles to that one element.

  • Can you explain the concept of a class selector in CSS?

    -A class selector in CSS is used to apply styles to multiple elements that share the same class attribute.

  • What is the drawback of using an element selector for styling?

    -The drawback of using an element selector is that it will apply the style to all elements of that type, which might not be the desired effect if you only want to target specific elements.

  • How does a child selector work in CSS?

    -A child selector in CSS targets elements that are direct children of a specified parent element, allowing for more specific styling rules.

  • What is a descendant selector and how is it different from a child selector?

    -A descendant selector targets elements that are descendants of a specified element, regardless of their depth in the hierarchy. It is different from a child selector, which only targets direct children.

  • What is the universal selector in CSS and what does it do?

    -The universal selector in CSS is denoted by an asterisk (*) and it targets all elements within the scope of the stylesheet, allowing you to apply styles to every element.

  • Can you describe what a pseudo selector is in CSS?

    -A pseudo selector in CSS is used to define a special state of an element, such as `:hover`, `:active`, `:first-child`, etc., allowing you to apply styles based on those states.

  • How does the ':first-child' pseudo selector work?

    -The ':first-child' pseudo selector targets elements that are the first child within their parent element, allowing you to apply styles specifically to those elements.

  • What is the purpose of the ':hover' pseudo selector in CSS?

    -The ':hover' pseudo selector is used to apply styles to an element when the user's mouse pointer hovers over it, providing a visual feedback to the user.

Outlines

00:00

📚 Introduction to CSS Selectors

This paragraph introduces the concept of CSS selectors, which are used to apply styles to specific elements on a webpage. The instructor discusses various types of selectors, including element, class, and ID selectors, and explains how they can be used to target elements for styling. The video emphasizes the importance of understanding selectors for efficient web development. The instructor also encourages viewers to engage by commenting and provides a brief demonstration of applying a background color to a div element using an element selector.

05:02

🎨 Exploring Advanced CSS Selectors

The second paragraph delves into more advanced CSS selectors, such as child and descendant selectors, which allow for more precise control over styling based on the relationship between elements. The instructor provides examples of how to style direct children of a div or any paragraph that is a descendant of a div. Additionally, the paragraph covers the universal selector for resetting margins and paddings, and introduces pseudo-selectors for links, demonstrating how to change styles based on the link's state, such as 'visited' or 'hover'.

10:04

🔗 Understanding Pseudo-Selectors and Their Applications

In the final paragraph, the focus is on pseudo-selectors, which are used to define the style of an element in a specific state. The instructor explains 'active', 'hover', and 'first-child' pseudo-selectors with examples, showing how they can be used to change the appearance of links or elements when interacted with or based on their position in the document structure. The paragraph concludes with an invitation for viewers to explore additional resources on selectors and pseudo-classes, and a reminder of the upcoming projects and the need to build practical skills for web development.

Mindmap

Keywords

💡Selectors

Selectors in CSS are used to identify and apply styles to specific elements on a webpage. They are fundamental to the process of styling and are essential for targeting HTML elements such as by their type, class, ID, or attributes. In the video, selectors are the main theme, with various types of selectors being discussed, including element, class, and ID selectors, to demonstrate how to apply styles to HTML elements.

💡Element Selector

An element selector in CSS targets HTML elements by their type. For instance, using 'div' as a selector will apply styles to all 'div' elements on the page. The script mentions using an element selector to make the background color of a 'div' red, to visually understand the space it occupies on the webpage.

💡Class Selector

A class selector is used to apply styles to elements that share the same class attribute. The keyword 'class' is denoted by a period (.) followed by the class name. In the script, the class selector is used to change the background color of a 'div' with class 'red', ensuring only elements with that specific class are styled.

💡ID Selector

An ID selector targets a specific element with a unique ID. It is denoted by a hashtag (#) followed by the ID name. The video script uses the ID selector to style an element with the ID 'green', changing its background color to green, demonstrating the specificity of ID selectors.

💡Child Selector

A child selector in CSS is used to select elements that are direct children of a specified element. It is represented by the '>' symbol. The script explains that if a paragraph ('p') is a direct child of a 'div', then the child selector can be used to style it differently, such as setting its background and text color.

💡Descendant Selector

The descendant selector is used to select elements that are descendants of a specified element, regardless of their level in the hierarchy. It is represented by a space between two element names. The video demonstrates that if a paragraph is within a 'div', even nested within other elements like 'article', the descendant selector will apply styles to it.

💡Universal Selector

The universal selector, denoted by '*', targets all elements on the page. The script uses the universal selector to apply margin and padding of zero to all elements, resetting the default spacing and layout properties.

💡Pseudo Selectors

Pseudo selectors in CSS are used to define the special state of an element. They are denoted by a colon (:) followed by the pseudo class name. The video discusses pseudo selectors such as ':hover', ':active', and ':first-child', which are used to style elements based on user interactions like hovering or clicking.

💡Pseudo Class

A pseudo class is a keyword added to a selector that specifies a special state of the selected element(s). The script introduces pseudo classes like ':hover', which changes the style when the mouse pointer hovers over the element, and ':first-child', which styles the first child element within its parent.

💡CSS Comments

CSS comments are used to explain the code, or to make parts of the code inactive without deleting it. They are written between '/*' and '*/'. The video script includes an example of a CSS comment to indicate that a particular line is an element selector.

💡Internal CSS

Internal CSS refers to style definitions written within an HTML document, typically within the 'style' tag in the 'head' section. The script mentions using internal CSS for demonstration purposes, allowing viewers to see the immediate effect of the styles being applied.

Highlights

Selectors in CSS are used to select an element to apply styles.

Styles can be applied to elements with an ID, class, or by their name.

Specific styling can be applied based on the type of element within another element.

The importance of understanding CSS selectors for ease of web development.

Introduction to element selectors and their basic usage.

Drawbacks of element selectors when additional elements are added.

Class selectors allow styling specific elements with the same class attribute.

ID selectors target elements with a unique ID for specific styling.

Child selectors apply styles to direct children within a specified element.

Descendant selectors style elements that are descendants of a specified element.

Universal selector resets margin and padding for all elements.

Pseudo selectors modify styles based on user actions or element states.

Explanation of the :visited pseudo selector for styling visited links.

Demonstration of :hover pseudo selector changing styles on mouseover.

Active pseudo selector changes styles during the active click state of a link.

First-child pseudo selector styles the first child element within a parent.

The rarity of using first-child pseudo selector in web development.

Encouragement to focus on main selectors for the majority of web development tasks.

Upcoming videos will cover more selectors and their practical applications.

Invitation to engage with the course by bookmarking, accessing the playlist, and following along with the source code.

Transcripts

play00:00

Guys, we talked about selectors in CSS and we said that selectors are used to select an element on which style is going to be applied.

play00:08

Now, what kind of elements can we apply style on and how can we select those elements?

play00:14

For example, element with an id, element with a class, we can target the element by its name.

play00:20

And we can also decide that if there is a particular type of element in a particular type of element, then only the styling is applied.

play00:27

I will explain the rules of applying this styling in this video and explain the selectors to you.

play00:32

If you are enjoying this course, then write sigma patch opi in the comment below.

play00:36

We have to maintain high energy throughout the course.

play00:38

I want to give you a heads up that this video is very important.

play00:41

This is a very important video of CSS.

play00:44

So increase your excitement.

play00:46

Let's go to the computer screen.

play00:48

Let's roll the intro.

play00:50

Intro

play00:52

Intro

play00:54

Intro

play00:56

Intro

play00:58

Alright guys, so I have come to my computer screen.

play01:00

And the first thing I am going to do is

play01:02

to make a folder of video 17.

play01:04

And video 17 is one of the most important video of css

play01:07

because if you understand how the selector works

play01:10

then your work will be very easy

play01:12

so I will make video 17 here

play01:15

index.html will be made here

play01:18

and I am going to use internal css here

play01:21

I will not make it in stylesheet

play01:23

so that you can see what I am doing

play01:26

And here I will do a lot of things

play01:29

I will do word wrap, screen cast mode

play01:32

So that you can see whatever button I press

play01:35

And I will write css selectors

play01:38

And I will make a div here

play01:41

I am a div

play01:44

If you have not accessed it yet, do play the playlist of sigma web development

play01:46

And the source code is getting updated

play01:49

So you don't have to worry about it

play01:52

Now what i am going to do

play01:55

I will do a simple thing, i will preview it first

play01:58

And i want to make the background of this div red

play02:01

Now you will ask why do you want to make the background of this div red

play02:04

Because i want to know how much space this div is taking

play02:06

That's why I want to make it red

play02:09

So I will simply give the background red

play02:12

So I will first put a style tag here

play02:15

I will say that the div

play02:18

Make the background color red

play02:22

Now this is our property and value

play02:26

I will tell you more about property and value

play02:29

but for now you understand this is property and value

play02:32

so what I did here was element selector

play02:35

so I will write here element selector

play02:38

and I will comment it here

play02:40

in CSS comments are written like this

play02:42

here forward slash star and star forward, whatever we write in between,

play02:46

Those are the comments in CSS. So I have put a CSS comment

play02:48

That this is an element selector, you will see here.

play02:50

So the background of this is red.

play02:52

This was our element selector.

play02:54

A very basic selector.

play02:56

Now the element selector that I have made here,

play02:58

What are its drawbacks?

play03:00

The moment I add another div here,

play03:02

So what will happen?

play03:04

The background of this background will also turn red

play03:06

and it is possible that you don't want to

play03:09

make this div's background red

play03:10

so if you don't want to make this div's background red

play03:13

then what will you do? definitely you will

play03:16

use some other type of selector

play03:18

and you will want to use a specific selector

play03:21

so I can say that do one thing, where class is red

play03:24

only make that div red, only that div

play03:26

should be red, so i have made

play03:28

class is equal to red

play03:30

and because i have made class is equal to red

play03:32

you can see here

play03:34

if i write

play03:36

.red and after

play03:38

.red if i make background

play03:40

color red, let's say i

play03:42

comment this, i don't want all div

play03:44

to be red, so i show don't want all div to be red.

play03:46

So I'll show you that only this div will be red.

play03:49

So here I used class selector.

play03:52

So this is our class selector.

play03:56

I'll write class selector.

play03:59

Next one is id selector.

play04:02

You might have guessed it as soon as you heard ID selector

play04:06

let us say I want the hashtag red ID

play04:09

or let's do one thing

play04:12

let's make an ID with hashtag green

play04:15

and if I write background color green

play04:18

then wherever I will give ID green it will be green

play04:21

you can see here I am another div

play04:24

it doesn't have any color, background color but as soon as I give it ID green, I will show you

play04:27

I will write here

play04:28

one minute, I will give it ID

play04:31

green and you can see that in green name

play04:34

I have given background color green

play04:35

its background color will be green

play04:37

so this is our ID selector

play04:39

it was very simple, right?

play04:40

yes, it is simple

play04:41

we make things simple here, okay

play04:44

now I will tell you what is child selector

play04:46

and the source code

play04:49

this will work for notes

play04:52

so what is child selector, if i want

play04:55

paragraph in div to be blue

play04:58

so i say the color of paragraph in div

play05:01

should be blue and background color should be

play05:04

background color should be let's say background color should be brown

play05:07

let's say I want this

play05:10

if I put a paragraph in any div

play05:11

I am a para inside div

play05:14

so what will happen? this selector will be applied

play05:17

child selector

play05:20

because this is a child of div

play05:23

and I have said that if any paragraph of div is child

play05:26

then what will happen?

play05:28

then its background will be brown and

play05:31

color will be blue. As simple as that.

play05:34

Now i will show you one thing. If i wrap this whole thing in div

play05:37

understand this carefully. Actually i will span this

play05:40

not div. I will

play05:43

put it in main or any other element

play05:45

Let's say I use article tag here

play05:49

So now this paragraph is not in div, now it is in article

play05:54

So this property will not be applied, I will show you

play05:57

Now this property is not applied

play05:59

Because this selector means that if it is directly in div

play06:03

Direct child, then this selector will be applied or not Means if div has a direct child Then

play06:05

This selector will be applied

play06:08

Here it's not a direct child

play06:11

It's an article and then p

play06:14

But if i want

play06:17

If paragraph is a descendant of any div

play06:20

Then this property will be applied

play06:23

Then i have another way

play06:26

i will simply

play06:29

use descendant selector

play06:32

and what is descendant selector

play06:35

we write it like this

play06:38

i will write div, space and p

play06:41

i am saying that div should have p

play06:44

no matter what it is I'll write case and then p I'm saying p should be in div

play06:46

whether it's in article, main, footer

play06:49

it should be in div

play06:52

it should be a descendant of div

play06:55

so you can see that it's applied

play06:58

but when i talk about child selector

play07:01

it should be direct child

play07:04

i hope you are getting the sense

play07:06

Now there is universal selector

play07:09

Means i want to give margin to all elements

play07:12

0 and padding 0

play07:15

I will write p0

play07:18

Means all elements have margin 0 and padding 0

play07:21

See everything is stuck here

play07:24

Earlier margin and padding 0 see all are stuck here before margin and padding was there

play07:26

by default

play07:28

but now margin and padding is gone

play07:30

like you saw

play07:32

i hope you understood

play07:34

now see there are many types of selectors

play07:36

but these are the most used selectors

play07:38

now i will tell you about more selectors

play07:40

in the coming videos

play07:42

but these are the ones with which we will do web development

play07:44

so i don't want to confuse you more

play07:46

And you guys

play07:48

Didn't learn the main selectors

play07:50

So I want you to focus on this

play07:52

This is the most important

play07:54

Through this, 95 or 99%

play07:56

Of the websites are made, so let's focus on this

play07:58

For now, I hope

play08:00

You guys understood this

play08:02

And you guys understood it very well

play08:04

There is one more selector which is also important

play08:06

And that is pseudo selectors

play08:09

Now what is this pseudo selector?

play08:12

I have a link, I will write

play08:15

Universal selector

play08:18

Universal selector

play08:21

Universal selector

play08:24

Why is there no comment ok, universal selector, what is this? why comment is not working, ok it is working

play08:27

now here what I will do, I will write

play08:30

Pseudo, Pseudo, yes I know it is called pseudo, don't write in the comment

play08:36

that it is not Pseudo, it is pseudo

play08:39

it is not pneumonia, it is pneumonia

play08:41

I know P is silent here, so why did you put silent

play08:44

tell me in the comment section, when it was silent, let it be like this I know that P is silent So why did you put it?

play08:46

Tell me in the comment section

play08:49

Tell me in the comment section

play08:52

But now i will share my knowledge with you

play08:55

And that is pseudo selectors

play08:58

Now assume i have a link

play09:01

Now i will write

play09:04

Https://www.google.com

play09:06

and i'll write go to google

play09:09

and

play09:11

it's very good, you'll go to google

play09:14

and you'll see what's new in it

play09:17

let's say it's color is white

play09:20

no not white, let's say it's color is yellow

play09:23

so i want a visited

play09:26

a visited

play09:29

what should be its color? yellow

play09:32

so i make its color yellow

play09:35

so will it be yellow? yes

play09:38

so anchor tags, if i want to see yellow color after visit

play09:41

so i can use pseudo selector

play09:44

so here you can see yellow color then you can use pseudo selector so here you can see

play09:46

the color changing, after this if you

play09:49

link a colon, means that link which

play09:52

is not visited, and suppose you want the color to be green

play09:55

so any link i will put, what will be the color

play09:58

suppose i will do it like this

play10:01

go to facebook, and yes

play10:04

let's capitalize google's g, so will it be I will go to facebook ok and yes we will capitalise google

play10:06

so will it be green

play10:08

no it will not be green

play10:10

I have already visited google

play10:12

so i have to write facebook

play10:14

ok

play10:16

so i will write facebook

play10:18

what will happen if i do facebook 2

play10:20

yes i have visited facebook

play10:22

i have visited facebook

play10:24

if you remember in initial few lectures I haven't visited it. I have visited it.

play10:26

So that's why it's showing like this.

play10:29

Now i'll tell you about 2 more links related.

play10:32

One is a active.

play10:35

If you type any pseudo selector, it shows you the image.

play10:38

If i want the background color to be red.

play10:41

So when i click on a link, it shows red.

play10:44

Active means when you are clicking.

play10:46

So here it is showing on google.com.

play10:49

And when I click, its background will turn red.

play10:52

I know that ideally you won't want to use this.

play10:55

But that was just an example.

play10:58

To show you how it works, it is easy for me to make the background red.

play11:01

Because you will know it directly.

play11:04

Now apart from anchor tag,, what do we have other than anchor tag?

play11:07

Some pseudo selectors, the answer is yes, definitely.

play11:11

One is hover, if I want that on anchor tag, anyone can hover.

play11:17

And I want its background color to be red or yellow.

play11:21

I make its background color yellow.

play11:23

So if I hover, you can see here that its background color is be yellow so if i hover then you can see

play11:26

that the background color is yellow

play11:29

as i am hovering, i have written a hover and background color is yellow

play11:32

so these were some pseudo selectors

play11:35

now i will tell you one more pseudo selector

play11:38

and that is, if i want to have a div

play11:41

whose class is selector

play11:44

and i will write I am selector

play11:47

I'll put 1 here, so that you don't get confused

play11:51

ONE

play11:52

Now I'll tell you about first child pseudo selector

play11:55

Which might be a little tricky for you

play11:57

And this category is pseudo selector

play12:00

So this pseudo selector

play12:02

If I write P first child background color aqua

play12:06

It means that wherever P is the first child of the parent, the background color will be aqua

play12:13

Like in this case, you can see that I have put a main tag here

play12:16

And it is written here P I am first and P I am second

play12:20

So here you can see that wherever the first child of the main is P, which is here

play12:24

This P is the first child of the main is P, which is here, this P is the first child of the parent.

play12:27

So this class will be applied in this.

play12:29

So see here,

play12:31

its background has been acquired, and this has not.

play12:33

But I will tell you again that such things are very rarely used when you do web development.

play12:38

So you guys do more research about such pseudo classes.

play12:42

What I will do in the description,

play12:44

I will put links in the description about this type of things.

play12:46

So if you want to see additional reading material

play12:48

and pseudo classes,

play12:50

then you will get the link

play12:52

in the description.

play12:54

Maybe I will put it in the pinned comment too.

play12:56

And if I forget, then please remind me

play12:58

that you have not given the link.

play13:00

So I will give all the links to you.

play13:02

From there you can read about these types of selectors.

play13:04

But now we will move ahead in this course.

play13:06

And we will read about the main things.

play13:09

Because we have to come on projects very soon.

play13:12

And we have to make real world apps.

play13:15

And we have to do backend and database.

play13:18

So we have to build pace.

play13:21

I hope you are enjoying this course.

play13:24

If you haven't bookmarked it yet, do it now. Click here and save it.

play13:25

Access the playlist, gethub code, like the video, share it.

play13:28

And name your village and uncle's land.

play13:31

Thank you so much for watching this video.

play13:34

And I will see you next time. Thanks for watching!

Rate This

5.0 / 5 (0 votes)

関連タグ
CSS SelectorsWeb DevelopmentStyling RulesElement SelectorClass SelectorID SelectorChild SelectorDescendant SelectorUniversal SelectorPseudo SelectorWeb Design
英語で要約が必要ですか?