Sunday, August 22, 2010

Webdesign, chapter two: CSS

This is just going to be a short introduction to CSS, it will only show a fraction of the things that you can do with CSS. This post is just meant to introduce how to use CSS and how to replace things that were formerly done with pure html with CSS.
I have decided to just focus on html5 and CSS3 since both of those are already usable even though they're still in development. That obviously changes the doctype of the previous post to the much easier <!doctype html>. we're also going to throw in a <meta charset="UTF-8"> to define which character set we're using. Now we're going to have to save all of our files in utf8 which is never a bad idea (unless, of course, if you're speaking a language that uses an alphabet not covered in utf8, in which case i'll leave it up to you to find the right charset to use (all "western" languages use only characters that are included in utf8 (English, German, French, Italian, etc.), Chinese, Japanese, Korean, Farsi, Arabic, etc. are not included)).
Now with all of that in mind our previous html code changes to this (viewable here):
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
<title>My First Website</title>
</head>
<body>
<p>This is my first website !</p>
</body>
</html>

Back to the CSS at last....
There are three different locations inside an html document where you can put your CSS code:
in a seperate file,
inside the <head> tag,
inside the html tag of the content you're trying to beautify.
If you want to put your CSS into a seperate file (which is strongly recommended, at least for things that you're using a lot), you have to link to that file inside the <head> tag. Just insert a tag into the <head> like this: <link href="./style.css" type="text/css" rel="stylesheet"/>. That assumes that you have a file named style.css in the same folder as your html file.
You can put all of the CSS code for the current document inside the <head> tag itself as well. For that you just need to add a new tag like this: <style type="text/css">, add your CSS code in there just like you would put it into a different file, for example: body {background-color: #000000; color: #0000FF} and close the tag with </style>.
The third option is to put all of the CSS inside each of the tags that you're changing, like so <body style="color: #ff0000">.
If you now add both of the latter CSS statements to your website the color of the text will be red (ff0000), unless you change it to a different color inside a deeper tag. That is because the code inside the html tag is closer to the content than the code in the head tag, which in turn would be closer to the content than an external stylesheet, and thus would overwrite the external stylesheet should there be anything that is defined contradictingly (for some reason that's not a word) in both.
so with that we have our new website (viewable here):
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
<style type="text/css">
p {color: #00F;}
</style>
<title>My First Website</title>
</head>
<body style="background-color: #000; color: #F00">
<p>This is my first website !</p>
</body>
</html>

No comments:

Post a Comment

Facebook