Introducing CSS Classes
Until now each CSS rule we created was directly connected to an element or group of elements on the stage. CSS classes enable you to define groups of elements of your own. CSS classes are not restricted to an element type and as such enable you to define one class for many different types of elements. By the end of this lesson, you will know how to create CSS classes and you will know how to use them.
Video Transcript
00:00Up until now, everything we wanted to style had to be of a type.
00:06So for example, if I change a paragraph,
00:08then all paragraphs that were in the same area would then be
00:11redefined to whatever rulings that I define to them.
00:14But many times you'll have where you want only some of
00:17the paragraphs or even multiple different types of
00:20elements to have a certain behavior.
00:22To enable that type of control,
00:24CSS introduced something that is called classes.
00:27Now, we control them through our HTML,
00:30we say, hey, by the way,
00:31you're an instance of this class,
00:34and a class is really just a name.
00:35So you could call it whatever you want,
00:37and we're going to see that right away.
00:39Then in the CSS, you could define the rules and
00:41whatever will have that class associated with it,
00:44would then be able to have those features.
00:47So let's see it in action.
00:48Classes, that's the topic of JU right now.
00:54So you'll see that in our source files,
00:56you see that I already created a class,
00:58and literally this is what it is.
01:00You define a name for a class in any element in HTML,
01:04and then set a name, any name you want.
01:07In our case, we set it and created it as an article.
01:10Now that we created an article,
01:12I can go into my styling,
01:14into the CSS and define a rule for that specific article.
01:18Now, contrary to HTML elements where I just write
01:22that name directly for a CSS class,
01:25I start it with a dot.
01:28Once I created that dot,
01:30I'm ready to start configuring and doing
01:32whatever I want to that element.
01:34So for example, let's go about and add a background color
01:37to this element, to this article element,
01:40and I'm going to go ahead and set that background color
01:43to let's say be red, completely red.
01:47If I go ahead and click on refresh,
01:49we'll see that now our article area
01:51is going to be defined in red.
01:54Now, the wonderful thing about the article element
01:58that we just created is that I could go ahead
02:00and define this rule to any item that I want.
02:03For example, what if I wanted O2 my first item
02:06to be inside of that ruling of class equals article?
02:13That's all I had to do,
02:14and what if I wanted O2 for my footer to be that?
02:17So I could, notice how I have your footer as a class,
02:20I could go ahead and put here a space
02:22and add here article as well.
02:25And if I go ahead and save this and click on refresh,
02:28we'll see that now both our first headline and our footer
02:32are now part of that article class.
02:36Now, obviously in a real world scenario,
02:38I wouldn't want to define everything as an article,
02:41but I really only want to define my article as an article,
02:44so I'll go ahead and delete that.
02:46But that's basically the really, really powerful capability
02:49that I have using CSS classes to basically define classes
02:53of my own, and then define the rule to the elements
02:57so it doesn't have to be defined
02:58based on HTML native elements.
03:00I could have different elements no matter what they are,
03:03as long as I'm using a shared class with them,
03:06they could share common features that I configure for them.
03:10Now, beyond that O2, a powerful feature that I have
03:13is I could have multiple classes
03:14as we saw with our footer.
03:16When I added a space, I just added another class
03:18and I could add as many as I want,
03:21and the priority is really one after each other,
03:23so if we override something with a new element,
03:26then the newest element is what we'll take into effect.
03:29So that's it about classes.
03:31It's a little short video.
03:32In the next lesson, we're gonna continue speaking
03:35about this topic, but we're gonna move on to a new topic,
03:37which is IDs, and we're gonna see
03:39what is the difference between an ID and a class.
03:42So see you in the next lesson when we explore that.
