Now that we have the basics, let’s talk more about selectors. Here is the syntax we showed you earlier. CSS is made up of a selector and property-value pairs.
CSS has different types of selectors. Let’s open our text editor, and browser, and look at type selectors first. Type selectors refer to HTML elements within the page. Here is the HTML generating the page, you can see the H1 and H2 headers, paragraphs, and a list. Now let’s look at the CSS. If we want to turn the first H1 tag blue we change the color to blue and refresh the page. So, type selectors select all the elements of a certain type of HTML tag, like divs, spans, headings, paragraphs. Any visible HTML element.
Sometimes you want to apply the same CSS styling to different elements, so instead of writing them all out, like this: H1, then H2, then H3, you can use what is called a multiple selector. Add a list of all the pseudo elements you want to style, separated by commas, so H1, H2, H3 and then give them all the same color.
If you want to write a comment in your CSS you can do it like this, forward slash, asterisk, your comment, asterisk, forward slash. Comments are great to remind you of what you were thinking, or to tell other people what you were thinking, kind of like Facebook... “And no, before you ask, I won’t accept your friend request.”
Type selectors are the most basic and they change all the HTML tags of a certain type. What if you want to change just one tag? That’s where classes come in. Classes are a group of CSS properties that can be applied to any tag. To use a class you have to add the class attribute to an HTML element, and then the corresponding class and styles can be added to your stylesheet. In the style sheet a class will have a period before the name to identify it as a class.
Let’s open the text editor and do an example by adding a class, highlight, to change the background color on a paragraph to yellow. Just add the class attribute to the paragraph element you want to highlight and then add the highlight class to the CSS style sheet. Now the paragraph specified will have a yellow background.
Classes can be defined differently for different tags, like paragraph dot highlight, or H1 dot highlight. Let’s add both to the style sheet and create an H1 highlight with a color property, then we can go into the HTML and add the highlight class to the H1 header. When we refresh, we see the H1 element has a different background color. You can add the class to as many HTML elements as you wish. Classes are polygamist, like Mormons, one to many.
IDs are another type of selector. They work like classes, except that, “THERE CAN ONLY BE ONE” id per page, like the Highlander. One use of IDs is for page layout. For example, if we want a div element to be the footer of the page we can add an ID of footer to this bottom div. Now lets add a width and height to the footer and a gray background in the CSS. Since we have a footer, we can wrap the rest of the HTML in a div tag with an ID of content.
Next are contextual selectors. They allow you to apply styles to tags that meet a certain criteria and are in a parent child relationship. There are various forms of child and sibling selectors. The most common, is the descendant selector. It selects all the children, grandchildren, great-grandchildren, you get the point. So, we want to style paragraphs in the footer differently from paragraphs in the content. We would use the descendant selector, or more simply, a space. It reads, pound, footer, space, paragraph. And another selector that reads pound, content, space, paragraph. Now, we can add a background color for the paragraphs in our content, and a border for the footer paragraph. background: #af295c; footer: #189fc4
Last are pseudo classes. These select specific states of an element, with the addition of a colon. Most commonly these are used for links , with four possibilities: Link, Visited, Hover and Active. The four pseudo classes should be used in this order so they don’t interfere with each other. Link is for things that have not been clicked, and can usually be omitted. Visited is for links that have been clicked before, Hover is what happens when your mouse hovers over the link, and Active is the link’s state while it is being clicked.
Let’s add some links to our HTML, remove the underline, and change the color.
Open up your text editor. First, let’s add two links to our HTML. One to Google, and one to Reddit. Let’s look at our HTML with these links added. You can see the links are underlined, and if they are inside a paragraph they inherit that paragraph’s color, this parent-child inheritance is a big part of CSS.
Let’s move over to the CSS. We can add a type selector for the anchor tag and give it a bigger text-size... We all know, bigger is better. Then, let’s set a style for the default link state and the Visited link state. Ok, now we can add a style for when you hover over the link and when the link is in the process of being clicked. Save the CSS changes and take a look.
As you can see, the links reflect these style changes. They are no longer underlined and the color changes on hover and when you click. And, we have a Fisherprice looking website. Sweet. Ok. Go pass the quiz.
Questions or Comments?