CSS Introduction: The How and Whys of Applying Style: Recap
CSS Introduction: The How and Whys of Applying Style: Recap
Recap
So far we've learned HTML is a bunch of tags describing the structure of your page. We talked about a lot of tags: <b> <i> <ul> <ol> <li> <table> <a> <img> <html> <head> <title> <body> And learned there's many more which we can learn about at HTML Dog's tag reference directory. Then we created some boring examples; this is because HTML alone is insufficient since all it does is describe the content of a page - this is a table, this is a paragraph, this is a list, this is the body, etc. The missing piece of the puzzle is CSS to add style.
To see what style tags you have at your disposal, HTML Dog has a reference page for that.
Now, inside the <head></head> tags add this bit of code: <style type='text/css'> div { color:blue; font-weight:bold;} </style>
Lets note some things about the above: The CSS code is nested inside a <style> tag. All the styles associated with the <div> tag are nested inside of an open and closing curly bracket { } Like inline styles, each declaration:value pair is separated with a semicolon; For neatness sake I put each declaration:value pair on its own line.
That's great, but what if I didn't want all my div's to have that same style?
In addition to applying styles to every instance of an HTML tag, you can target just specific HTML tags by giving them a class name. <style type='text/css'> .profit { color:green; font-weight:bold;
} .loss { color:red; font-weight:bold;} </style> ... <div class='profit'> Your profit this month was +$12.00 dollars! :) </div> <div class='loss'> Your loss this month was -$10.00 :( </div> The period in front of the class name in the style block lets it know we're targeting an element by its class name. We can use our class names repeatedly: <div class='profit'> Your profit in August was +$12.00 dollars! :) </div> <div class='profit'> Your profit in July was +$17.55 dollars! :) </div> <div class='profit'> Your profit in June was +$147.55 dollars! :) </div>
Last method for applying styles: External (the most awesome way)
When we apply styles externally we take the styles completely out of the page and move them into their own file. That file then gets linked back to the pages we're working on. With external style sheets, we can apply the same styles to multiple pages! Imagine this scenario:
One day you decide your site made up of 20 different pages should have a green theme instead of a blue theme. Instead of having to edit every individual page making changes, you only have to change it in your one master style sheet.
To use an external style sheet you're going to take all of the CSS code out of your HTML and put in a separate file you name with a .css extension. Because it's in a .css document, no <style> tag is necessary, just jump right into the CSS (see right). Then, in the head of your HTML page, you connect that stylesheet you just created: <link rel="stylesheet" type="text/css" href="styles.css" /> File: index.html
<!DOCTYPE html> <head> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class='profit'> Your profit this month was +$12.00 dollars! :) </div> <div class='loss'> Your loss this month was -$10.00 :( </div> </body>
File: styles.css
.profit { color:green; font-weight:bold; } .loss { color:red; }
For a really interesting example of the power of external styles, check out the CSS Zen Garden. Here's a metaphor describing the three different ways to use CSS in your
page. Note that you can use inline, internal and external styles together on a page.
Links
Links are unique from other text in that they have different states: link, visited, hover, active
By default links are underlined and blue (or purple if they've been clicked). Using this bit of CSS code we can change any of that. a:link { color:#000000; text-decoration:none; } a:visited { color:#00FF00; } a:hover { color:#FF00FF; } a:active { color:#0000FF; }
Colors
CSS has a selection of "named" colors you can use: color:red; color:blue; color:yellow; So on... But if you want full control of your color, using variations of shades, you'll need to find it's hex value which is just an 8 character code specifying that color.
ColorPicker.com is a really simple tool that lets you figure out the hex value for the color you want. color:#800E2A;
Exercise
Recreate the following page. Tip: You'll end up using these styles. margin padding width height border font-weight font-size You don't have to match stuff like width, height or font-sizes exactly - just get close.