Important Keyword in CSS Explained | Frontend Bootcamp Hindi | Ep.12

Anurag Singh ProCodrr
30 Sept 202216:26

Summary

TLDRIn this educational video, the presenter explores CSS specificity and the cascade, focusing on how styles are applied when there are conflicts. They explain that inline styles have a higher specificity of 1000, surpassing IDs, and demonstrate how the '!important' rule can override all other styles, including inline styles. The video serves as a warning about the potential pitfalls of using '!important' and inline styles, emphasizing the importance of understanding CSS specificity to avoid overwriting styles unintentionally.

Takeaways

  • πŸ˜€ Cascade and specificity are crucial in determining which styles are applied when there are conflicting styles.
  • πŸ” The specificity hierarchy is ID (highest), class, and then element selector.
  • πŸ“ˆ To increase specificity, you can duplicate the class or ID selectors.
  • πŸš€ Inline styles have a specificity of 1000, which is higher than any other selector, making them difficult to override with CSS alone.
  • πŸ’₯ The '!important' rule in CSS has infinite specificity and should be used sparingly as it can override all other styles.
  • πŸ› οΈ To override inline styles, you must use the '!important' rule, as no other selector can override them.
  • πŸ”‘ The '!important' rule can only be overridden by another '!important' rule with higher specificity or by changing the inline style itself.
  • πŸ“ Understanding the cascade rule is essential for predicting which styles will be applied and which will take precedence.
  • πŸ”„ When specificity is equal, the last style defined in the CSS will be applied due to the cascade rule.
  • ⚠️ Overusing '!important' can lead to maintenance issues and should be avoided unless absolutely necessary.

Q & A

  • What determines which style will be applied in case of conflicting styles in CSS?

    -Cascade and Specificity determine which style will be applied in conflicting styles. ID has the highest specificity, followed by class, and then element selector.

  • How can you increase the specificity of a selector in CSS?

    -You can increase the specificity of a selector by using multiple classes or an ID, which adds to the specificity value.

  • What two things have more specificity than an ID in CSS?

    -Inline styles have a specificity of 1000, and the '!important' rule has infinite specificity, which can override any other selector.

  • Why is it difficult to change a style set by inline styles in CSS?

    -Changing a style set by inline styles is difficult because inline styles have a high specificity of 1000, which is higher than any other selector except for '!important'.

  • What is the specificity value of an inline style in CSS?

    -The specificity value of an inline style in CSS is 1000, which is higher than any other selector except for the '!important' rule.

  • How does the '!important' rule work in CSS?

    -The '!important' rule in CSS overrides all other styles, regardless of specificity or the cascade order, making it a powerful tool to force a style to be applied.

  • What is the recommended practice regarding the use of '!important' in CSS?

    -It is recommended to avoid using '!important' in CSS due to its potential to disrupt the natural cascade and make future maintenance difficult.

  • How can you overwrite an '!important' rule in CSS?

    -To overwrite an '!important' rule, you must use another '!important' rule with higher specificity or place the new '!important' rule after the existing one in the cascade.

  • What is the significance of the cascade rule in CSS?

    -The cascade rule in CSS determines the order in which styles are applied when there are no specificity conflicts. Styles defined later have a higher priority.

  • Why should inline styles be avoided in CSS?

    -Inline styles should be avoided because they have a high specificity that makes them hard to override with external CSS, leading to less maintainable code.

Outlines

00:00

🎨 Understanding CSS Specificity and Overriding Styles

The paragraph introduces the concepts of CSS specificity and inheritance, explaining how they interact to determine which styles are applied when there are conflicting styles. It highlights the hierarchy of specificity, with ID selectors having the highest specificity, followed by class selectors, and then element selectors. The video also explores how to increase specificity by using multiple classes or IDs. The speaker then transitions to discussing two types of selectors with even higher specificity than IDs: inline styles, which have a specificity of 1000, and the '!important' rule, which has infinite specificity and overrides all other styles regardless of their specificity.

05:02

🌟 Inline Styles and Their High Specificity

This section delves into inline styles, which are styles written directly within an HTML element's 'style' attribute. It explains that inline styles have a specificity of 1000, which is higher than any other selector type, including IDs. The speaker demonstrates how inline styles override other CSS rules and discusses the difficulty of overriding them with standard CSS. The use of the '!important' rule is introduced as a method to override inline styles, but it is cautioned that this practice should be used sparingly due to its potential to disrupt the natural cascading and specificity rules of CSS.

10:04

⚠️ The Power and Danger of the '!important' Rule

The paragraph discusses the '!important' rule in CSS, which has infinite specificity and can override any other style declarations, including inline styles. The speaker illustrates how to use '!important' to force a style to take precedence over all others. However, it is emphasized that '!important' should be used with great caution as it can make future style changes difficult and can lead to a maintenance nightmare. The paragraph also touches on the cascade rule and how it works in conjunction with specificity to determine which styles are applied last when there are conflicts.

15:08

πŸ”„ Overwriting '!important' and Best Practices

The final paragraph addresses the challenge of overriding styles that have been declared with '!important'. It explains that the only way to override '!important' is by using a higher specificity or by using '!important' itself with a higher specificity selector. The speaker warns against the overuse of '!important' and suggests that it should be reserved for situations where there is no other option, such as when styles are being overridden by inline styles. The paragraph concludes with a summary of the key points: understanding how to overwrite styles, the pitfalls of using inline styles and '!important', and the importance of maintaining a clean and manageable CSS codebase.

Mindmap

Keywords

πŸ’‘Cascade

Cascade refers to the set of rules that determine which style declarations are applied to an element when more than one declaration targets the same element. In the video, the concept is introduced as a key factor in deciding which style will be applied in cases of conflicting styles. It is mentioned that the cascade rule works together with specificity to determine which styles are applied to an HTML element.

πŸ’‘Specificity

Specificity is a measure of how specific a CSS selector is, which affects the order in which styles are applied when there are conflicts. The video explains that specificity is calculated based on the number of ID, class, and element selectors used, with IDs having the highest specificity, followed by classes, and then element selectors. The script provides examples of how to increase specificity by duplicating a class or ID selector.

πŸ’‘Inheritance

Inheritance in CSS refers to the mechanism by which properties applied to a parent element are also applied to its child elements. The video script mentions inheritance as a concept that was discussed in a previous video, where the styles applied to an element can pass down to its children, affecting how they are displayed.

πŸ’‘Inline Style

Inline styles are CSS rules that are written directly within the HTML element as a style attribute. The video script explains that inline styles have a specificity value of 1000, which is higher than any ID, class, or element selector. This means that inline styles will override other styles unless the '!important' rule is used.

πŸ’‘!important

The '!important' rule in CSS is used to give a style declaration higher priority, ensuring it is applied even if it has lower specificity. The video script warns against the overuse of '!important' as it can make stylesheets difficult to maintain and debug. It is mentioned that '!important' can override inline styles, but it should be used sparingly.

πŸ’‘Overriding

Overriding in CSS is the process of applying a new style rule that takes precedence over an existing one. The video script discusses various methods of overriding, including using higher specificity selectors and the '!important' rule. It provides examples of how to override styles using different selectors and the implications of doing so.

πŸ’‘CSS Selectors

CSS selectors are patterns used to select the elements you want to style. The video script explains that the specificity of a selector is determined by the combination of element type, class, ID, and other selectors. It mentions that element selectors have a lower specificity compared to class and ID selectors.

πŸ’‘Live Server

Live Server is a development server with live reload feature for static & dynamic pages. The video script mentions opening an HTML file with the live server to see the styles applied in real-time, which is a common practice in web development for testing and debugging.

πŸ’‘VS Code

VS Code, or Visual Studio Code, is a popular source code editor that supports a variety of programming languages. The video script refers to VS Code as the environment where the HTML, CSS, and JavaScript files are opened and edited.

πŸ’‘Element Style

Element style, as mentioned in the video script, is another term for inline styles. It is the style that is applied directly to an HTML element and has a high specificity value of 1000. The script explains that element styles are difficult to override and should be avoided in most cases.

πŸ’‘HTML Element

An HTML element is a component of an HTML object, consisting of a start tag, content, and an end tag. The video script uses HTML elements as examples when discussing how styles are applied and overridden. It shows how different styles interact with elements and how specificity and the cascade affect the final appearance of these elements.

Highlights

Cascade and Specificity determine which style is applied in conflicting styles.

ID has the highest specificity, followed by class, then element selector.

To increase specificity, duplicate the class or ID selector.

Two things have more specificity than ID: one is 1000, the other has infinite specificity.

Inline styles have a specificity of 1000, which is higher than ID.

Inline styles are difficult to override with CSS, making them less preferable.

The '!important' rule in CSS can override any other style, including inline styles.

Using '!important' should be rare due to its potential to disrupt CSS management.

The cascade rule states that the last style defined is applied if there are no specificity conflicts.

To overwrite '!important', use a higher specificity or place the style at a later point in the CSS.

The '!important' rule can only be overridden by another '!important' with higher specificity or cascade placement.

Overwriting inline styles requires '!important' as they have a high specificity of 1000.

Avoid using inline styles and '!important' to maintain manageable and predictable CSS.

Understanding specificity and the cascade rule is crucial for effective CSS management.

The video demonstrates practical examples of specificity and the cascade in action.

The '!important' keyword is a powerful tool but should be used judiciously.

Transcripts

play00:00

Hi everyone, in

play00:02

the last video,

play00:04

we understood Cascade,

play00:06

Specificity and Inheritance.

play00:08

Cascade and

play00:09

Specificity decides

play00:11

which style

play00:13

will be

play00:15

applied in

play00:17

our conflicting

play00:19

styles.

play00:21

We saw that ID

play00:22

has the most specificity.

play00:24

Then comes class and

play00:26

then comes element selector.

play00:28

We also saw that if we

play00:30

want to increase someone's specificity,

play00:32

then how can we do that?

play00:34

If we copy that class

play00:35

or ID again and again,

play00:37

then its specificity increases.

play00:40

In this video, we

play00:41

will discuss two things,

play00:43

which have more specificity than ID.

play00:47

One of them is 1000,

play00:50

and the other has infinite specificity.

play00:54

It doesn't care about specificity.

play00:57

It applies on its own.

play01:00

Whatever you do, it will apply.

play01:03

Let's see what it is.

play01:06

For that, we will go

play01:07

to our old project.

play01:09

Where did it go?

play01:11

Here it is.

play01:13

So, I will duplicate this old project.

play01:19

I will give it a name.

play01:22

03 underscore

play01:29

Overriding

play01:34

Specificity

play01:40

Overriding Specificity

play01:44

It is very difficult to say this.

play01:47

I will say specificity again and again.

play01:50

It has opened in our VS Code.

play01:53

In the last video,

play01:54

we wrote some styles

play01:56

to understand specificity.

play01:58

I will uncomment this.

play02:00

It was written to understand inheritance.

play02:03

I will comment this.

play02:05

Here is our HTML.

play02:08

I have to right

play02:09

click and open

play02:11

with live server.

play02:13

We have opened with live server.

play02:15

According to what we saw

play02:16

in the last video,

play02:18

we were styling this element.

play02:22

While styling this, we were understanding

play02:25

how specificity and cascade are working.

play02:28

Here, our deep

play02:29

pink style was

play02:31

being applied.

play02:33

Let's close this.

play02:36

I will keep index.html file first.

play02:41

Then I will keep CSS.

play02:44

Then I will keep JavaScript.

play02:47

Whatever you prefer, it doesn't matter.

play02:50

I have kept it as it is.

play02:53

According to the cascade rule,

play02:58

when there is a comparison in this,

play03:01

this will be applied.

play03:03

But it will overwrite the class ID.

play03:06

It will overwrite the class ID.

play03:11

Because these two are on equal level,

play03:17

their specificity is equal,

play03:20

the one below is being applied.

play03:22

Cascade rule is being followed.

play03:24

Cascade rule and specificity both work together.

play03:30

I want to use

play03:31

this ID and change

play03:33

the deep pink color.

play03:36

I don't want to

play03:37

use the class ID.

play03:39

If I want to

play03:40

change the class ID,

play03:42

give me the class ID below.

play03:45

Then it will overwrite the background color.

play03:50

This is white.

play03:53

So, this.

play03:57

So, it overwrote the class ID.

play04:00

Because it has the same specificity.

play04:02

This has 100.

play04:04

This has 100.

play04:05

So, close this.

play04:07

Remove this.

play04:09

We will see how to overwrite.

play04:12

We have used this a lot

play04:14

earlier when we were learning HTML.

play04:16

Inline styles.

play04:18

So, how do we give?

play04:19

Style is equal to background color.

play04:22

Give green here.

play04:24

So, I gave the background color green.

play04:26

Now look at this.

play04:27

Its specificity is more than that.

play04:30

Its specificity is more than ID.

play04:32

That's why this is being applied.

play04:34

If we click on this.

play04:35

So, look at this.

play04:36

And it is coming in this section.

play04:39

Our CSS file name is not coming here.

play04:41

Because it is not coming from CSS.

play04:42

It is coming directly from HTML.

play04:44

And here it is written as element style.

play04:46

So, element style means inline style.

play04:49

We saw in the

play04:50

first video of CSS.

play04:52

When we write inline style,

play04:53

it comes in this section.

play04:55

Whatever we write here,

play04:57

it will come in this category.

play04:59

So, this is inline style.

play05:00

So, element style is written here.

play05:02

So, element style is

play05:03

what we give directly

play05:05

inside the element.

play05:07

So, its specificity is more than ID.

play05:11

How much is it?

play05:12

Can we hover and see?

play05:13

No, we can't.

play05:14

If you hover here, it

play05:16

is showing something else.

play05:18

If you hover here, it is showing something else.

play05:20

So, whatever specificity it shows,

play05:23

it shows only inside the CSS file.

play05:26

So, if we want

play05:27

to know its specificity,

play05:29

how will we know?

play05:31

Search on Google.

play05:32

Specificity of inline style.

play05:39

So, see this.

play05:42

Inline styles get specificity value of 1000.

play05:47

So, its specificity is 1000.

play05:50

If we come to

play05:51

this result of W3schools,

play05:53

So, see this.

play05:54

It has 1,

play05:55

which is tag element selector.

play05:58

We have also seen by combining.

play06:00

If you combine tag and class,

play06:02

then class will have 10

play06:03

and tag will have 1.

play06:05

So, 10 and 1 will become 11.

play06:07

If you combine ID and

play06:08

this, it will become 101.

play06:10

And see this, inline style.

play06:13

The color is given here in pink.

play06:15

It is given inside p tag.

play06:17

So, its specificity becomes 1000.

play06:21

So, the specificity

play06:22

of inline style

play06:24

is very high.

play06:26

That's why we don't use inline style.

play06:29

Because if someone gives inline style once,

play06:33

then it is not

play06:35

impossible to change it

play06:37

by using CSS.

play06:39

We will see that next.

play06:41

We will see that next.

play06:43

We can do, but we

play06:45

should not use it.

play06:47

So, it is very difficult.

play06:48

Normally, if we use ID,

play06:50

then it overwrites.

play06:52

Normally, if we use ID,

play06:55

and someone has given

play06:57

inline style inside element,

play06:59

then inline style overwrites everything in CSS.

play07:03

Except one thing.

play07:05

That is a very dangerous thing.

play07:07

It should never be used.

play07:09

But sometimes you may need it.

play07:12

So, let's see.

play07:14

Now, in all of these,

play07:16

in all of these,

play07:18

the least specificity,

play07:20

and according to Cascade rule,

play07:22

the least importance is this one.

play07:24

This green one.

play07:26

If we come here and see,

play07:28

I have given green here as well.

play07:30

I have given green here as well.

play07:32

Let me give it some other color

play07:36

so that there is no confusion with that green.

play07:38

Let's give this color.

play07:40

The element style has the highest specificity.

play07:43

It is 1000.

play07:44

That is why it is here.

play07:46

It is at the top.

play07:48

The least specificity

play07:49

is of this

play07:51

deep pink one.

play07:53

Deep pink ID selector

play07:54

is of this one.

play07:56

The least importance is of this one.

play08:00

So, look at this.

play08:02

The least importance,

play08:04

if I uncheck this as well,

play08:08

If I uncheck this,

play08:10

this applies.

play08:11

If I uncheck this,

play08:12

this applies.

play08:13

If I uncheck this,

play08:14

this applies.

play08:15

So, look, our alloy,

play08:17

the one we used earlier,

play08:19

that one is coming last.

play08:21

It is coming here.

play08:22

When you remove all of them,

play08:24

then this alloy will come.

play08:25

According to Cascade rule,

play08:27

the least importance is this green one.

play08:29

According to specificity,

play08:31

it has the least importance.

play08:33

It has the least specificity.

play08:35

But now,

play08:36

I will tell you one thing,

play08:38

by using which you will overwrite everything.

play08:40

Element style, ID, whatever it is,

play08:42

everything will be overwritten.

play08:44

So, that is

play08:46

important.

play08:48

Exclamation mark and then important.

play08:51

You will write this much.

play08:53

Now, look, everything is green.

play08:55

Everything's color is green.

play08:57

So, if you come here and see,

play09:00

the specificity is very less.

play09:03

That is why it is coming down.

play09:05

If I reload this,

play09:07

it has crossed out the element style.

play09:10

It had 1000 specificity.

play09:12

It has also crossed out the ID,

play09:15

the class,

play09:17

and all the alloys.

play09:19

It is down.

play09:21

It is down because the specificity is less.

play09:23

But it is being applied on everyone.

play09:25

It has overwritten everyone.

play09:27

So, this important thing

play09:29

is very dangerous.

play09:31

You have to use this very rarely.

play09:33

If you give this once,

play09:35

then you will lose

play09:36

your life in overwriting.

play09:38

You won't be able to do it.

play09:40

It is very difficult.

play09:42

So, you must be thinking,

play09:44

what is the specificity of this?

play09:46

There is no specificity of this.

play09:48

It just gets applied.

play09:50

If you look here,

play09:52

there is only one specificity.

play09:54

The specificity of the alloy

play09:56

is of a selector.

play09:58

And this is not a selector.

play10:00

You have told in that style,

play10:02

in that property,

play10:04

that this property is important.

play10:06

You have to apply this.

play10:08

It doesn't matter,

play10:10

whatever it is.

play10:12

So, this has become the most important.

play10:14

Now, its specificity is very less.

play10:16

It is one, but it is being applied.

play10:18

According to the cascade rule,

play10:20

if I place it at the top,

play10:22

then it means that

play10:24

its importance

play10:26

according to the cascade rule

play10:28

will be very less.

play10:30

But, it is still being applied.

play10:32

Look here.

play10:34

It is still being applied.

play10:36

It is at the bottom.

play10:38

In style, it is at the top,

play10:40

but according to the cascade rule,

play10:42

it is at the bottom.

play10:44

But, I have told the importance here.

play10:46

So, it will be done.

play10:48

Now, I want

play10:50

to overwrite this importance.

play10:52

So, how will we do it?

play10:54

It is very difficult.

play10:56

Now,

play10:58

we will overwrite the importance.

play11:00

So how will you do it?

play11:03

Take this and put it down

play11:07

and then give it

play11:09

some other color, let's

play11:11

give it brown

play11:13

We have given the color of

play11:15

the text as well as brown,

play11:17

so it is not visible

play11:19

Let's give some other text

play11:22

Let's give this

play11:27

cornflower blue

play11:30

So I have given this blue color

play11:33

Now see, it is overwriting

play11:35

Because these two are important, they

play11:36

are fighting with each other

play11:38

So now according to the cascade rule, it

play11:40

is coming down, so it is being applied

play11:42

So the important can

play11:43

only cut the important

play11:45

Like iron cuts

play11:46

iron, it is

play11:48

cutting the important

play11:50

So if you look here

play11:55

See here, the cascade rule is following

play11:58

This one is coming down, because this

play12:00

was our style, it was applied

play12:02

Then this is being applied

play12:04

Now the important is in both

play12:06

So this important cut this important

play12:11

There is no other

play12:12

way to overwrite

play12:14

the important, nothing

play12:16

Until you write the important

play12:18

Using a cascade rule

play12:19

or increasing the specificity

play12:21

You have to overwrite it

play12:23

Now you want to overwrite this

play12:26

You have to overwrite this, but you have

play12:28

to write your style at the top

play12:30

You have to write it at the top

play12:32

So you cannot do like this

play12:35

If you want, you can copy this

play12:39

And don't give

play12:40

it a green

play12:42

color, give

play12:44

something else

play12:47

Dark olive green

play12:54

So you cannot

play12:55

overwrite by writing

play12:57

at the top

play12:59

Until and unless you increase the specificity

play13:04

How to increase the specificity?

play13:07

See, here you write html

play13:11

So its normal specificity is 1

play13:15

Here the class is also

play13:17

added, so it is 11

play13:19

One for this, one for

play13:20

this, so it is 11

play13:22

Now it will overwrite, it

play13:23

will become olive green

play13:25

Dark olive green has come here

play13:27

Because its specificity is more

play13:30

It will apply according

play13:31

to the cascade rule

play13:33

But then it

play13:34

will overwrite because

play13:36

its importance

play13:38

is more

play13:40

I will not say, I have said a lot

play13:43

I get mad while saying it

play13:48

Its importance is more

play13:50

So see here

play13:54

It will come above it,

play13:55

where did it go?

play13:57

This is olive green

play14:01

See this

play14:03

Its specificity is 10

play14:07

I will say importance

play14:10

Don't say specificity

play14:12

I don't want to say it

play14:14

Its importance is less,

play14:16

its 10, its

play14:18

10, its 1

play14:20

Its 10, its 10, its 11

play14:22

That's why it is coming

play14:23

above in our style

play14:25

So this is being applied

play14:27

So like this we

play14:29

can overwrite the importance

play14:31

But we can only

play14:32

overwrite using the importance

play14:34

We don't have to use it

play14:37

Until and unless

play14:39

Suppose you go

play14:40

to codebase and

play14:42

someone else has

play14:44

already used

play14:46

the importance

play14:48

And you have to overwrite it

play14:51

Then in that case you will

play14:53

need to use the importance

play14:55

Normally we don't use it

play14:57

Our specificity and cascade rule gets destroyed

play15:08

These were the two topics

play15:10

I wanted to tell you

play15:12

How we can overwrite the id

play15:16

Important keyword is also very important

play15:19

Normally we don't use it

play15:22

But you will see it

play15:23

and it will kill you

play15:25

If you don't know what it is

play15:27

You will do anything, whatever you

play15:29

give, it will not apply

play15:31

So I hope you understood

play15:35

You must have understood how to overwrite

play15:40

Don't use inline style

play15:42

Don't use importance

play15:44

You should know that its

play15:46

specificity is the most

play15:48

If you have given an inline style

play15:50

Then you will have to

play15:51

use importance to overwrite it

play15:53

You can either change the inline style

play15:57

If you can't change the html

play16:00

Then you can overwrite

play16:02

the inline with importance

play16:04

You can't overwrite the

play16:06

inline with any id,

play16:08

class or element selector

play16:10

If you want to overwrite the inline

play16:12

Then you will have to use importance

play16:14

So that's it for this video

play16:16

See you in the next video

play16:18

Bye Bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
CSS SpecificityInline StylesCascading RulesWeb DevelopmentCode TutorialStyling TechniquesHTML ElementsCSS Selectors!important UsageOverriding Styles