class
Objectives:
Set up
HTML
andCSS
files with css file link in head.Test set up with a color change: body { background-color: blue; }
Reveal info about classes with the classes div.
Discuss rules of classes.
Discuss descendant selectors in
CSS
.
File Location
You should be located in the following files:
Description
Classes are one of the ways we have to target elements for styling in CSS. Using class
allows you to apply thematic concepts to elements that can be easily modified. For example, if you want every <button>
and every <a>
tag to have the same text color, assigning a class
allows you to specify a value
for the property color:
to that class
. Let's explore this a little bit more by writing some HTML and assigning a class
to several elements so we can see how classes can affect several elements using the same rules.
Finished Code
HTML Code:
CSS Code:
Discussion
Breaking down the code a little bit, let's first recognize that we've assigned a class
of "css-classes"
to all of the <div>
tags above. Remember, class
is an attribute
that we can use to change the way HTML elements behave. Now we can delve into the .css
file!
The first thing to identify in the .css
file is that it's possible to reference generic HTML elements by their tag type.
We've done this by using the selector
of body
. This is followed by a {
which indicates that we're starting our ruleset. We then move into the ruleset and choose a property
. In the case of our body
tag, we're choosing to change the background-color:
and then we assign it the value
of blue
. If it isn't obvious, this states to the browser that all elements with a <body>
tag will have a background that's blue. We state that the assignment is completed with a ;
at the end of the line. Finally, the ruleset is closed by a mated }
. Just like in HTML, every opening {
needs a closing }
to let the browser know that we're done giving it instructions for the specific element/class.
Native HTML tags are one thing, but what about custom .css
classes? Well, we identified "css-classes"
a moment ago, and in the .css
file we can see an entry with the same name. The only difference is that custom classes must be targeted using the .
operator before the class name, as is reflected in the .css
file above.
The last thing we need to discuss before moving on to the next lesson is descendant selectors. We know how to target a class
using .class
, but what if we want to target specific HTML elements in our class
, but nowhere else? CSS has a neat trick where every element type that is a child can be easily pointed at by appending the element type to the end of our class
. This is reflected with the syntax .css-classes h1
in our code above.
Challenge
Try adding a property
to one of your selectors above. Explore some of the properties that we haven't discussed yet...there are quite a few of them!
More Reading
A great CSS example, if needed, can be found here.
Last updated