Now we've got CSS working let's backtrack and understand how CSS works.
Whenever we want to style something in CSS we need to describe what
we want to style. To do this we use something called selectors.
In the example code above we styled h1
tags. We did this by using a h1
selector. Then we set the property called color
to be red
.
The basic structure of css is this:
Task
To check your understanding add some CSS that makes your ordered list (ol
)
that you created earlier have blue text.
As an extension set the background color of body
to be red. To do this you need
to use the property called background-color
. The image above should help
you do this.
Once you've got this to work, feel free to change the colours to suit you.
Tip: you can use the pre-defined named colours like blue
and yellow
or you could use a hex colour. For example if I write: color: #a74343;
the text is a pale red. You can find more by searching for hex colours online.
Classes
Currently we only know how to target specific tags, but what if we
want more control than that? Let's say we want to target a certain group of p
tags but not all of them.
Well we need to use a class. Let's imagine we want some paragraphs to have a
large font size. We could use create a class called large
and then apply that class to the paragraph elements we want to be large.
To do this we use a class
attribute on the HTML tags we want to have the class.
<p class="large">This should be large</p>
<p>This won't be large</p>
<p class="large">This should also be large</p>
Okay, so we've labeled two paragaphs with the class large
but we haven't
defined what large
means. We need to do this in CSS. To specify a class
selector we use a dot (.
) and then the class name.
.large {
font-size: 40px;
}
As long as that CSS is imported any paragraph with the class large with have
a large font size.
Task
Your task is to create a class called important
which makes the text stand out.
You can decide what you want to do, but I'd recommend changing the text colour,
increasing the font size, making it bold and maybe changing the background colour.
I haven't told you how to make text bold so you'll have to look that up. (hint:
search css bold text
).
Make sure you add a paragraph to your HTML with that class.