Yo reader, how are you today?
Two lessons ago we have discussed how we can include our CSS file in the HTML tree. Last lesson we have discussed how the browser knows which colours and distances to apply to which element. Also we found out how to ease his job by knowing when to use a class and when to use and id.
Today we will re-use the example from HTML Tree and tags . We will create a few leaves for our HTML tree and colour them a bit.
<link rel=“stylesheet” type=“text/css” href=“mystyle.css”>
<title>The title of our tableau</title>
<div class=”green leaf” id=”first_leaf”>first leaf</div>
<div class=”red leaf” id=”second_leaf”>second leaf</div>
<div class=”green flower”>very very small flower</div>
Before actually writing some CSS I will take some time and explain all those “div” branches of our tree.
Let’s do this exercise:
- Take a piece of paper.
- Cut it in 3. Do it!.
- Take a second piece of paper.
- Put the 3 parts on top of it.
The “div”-s for HTML are exactly as the papers are for you. They are transparent, one with the background but they exist there to hold information for you.
Let’s do the following exercise:
- Take the first small piece of paper and write class=”green leaf” id=”first_leaf” on it
- Take the second small piece of paper and write class=”red leaf” id=”second_leaf” on it
- Take the third small piece of paper and write class=”green flower” on it
- Flip the first piece of paper and write on it first leaf
- Flip the second piece of paper and write on it second leaf
- Flip the third piece of paper and write on it very very small flower
- Put them back on the big piece of paper with the text upwards. Not the “class” and “id” non-sense, but with the nice human readable text.
Great job! This is what your browser sees.
Now we are ready to write our CSS file. Let’s play again:
- Open your program with which you write text. If you are unsure what I mean please go to http://www.sublimetext.com , download, install and start this program. From now on this is our HTML editor.
- Write into the HTML editor all HTML code from above.
- Save the file into a directory/folder that you want and name the file “html_file.html”
- Get back to sublime and go to File, New file
- Save this new empty file into the same directory/folder as the “html_file.html” file and name it “mystyle.css”
That was all! Now you have your very own website. Yes, it takes some more work to make it visible to other people but you can see it locally by:
- Go to the directory/folder you have used previously to save the html and css file
- Double click on “html_file” or drag it on top of your browser
That’s all, if all went fine you should be able to see the text of your leaves in there. They should have the same text as your small pieces of paper. You see the resemblance now. The browser does no magic tricks. He just reads everything and splits the information is 2 piles. Information for him and information for you. Same as you pieces of paper. Information for the viewer, the nice text, and information for the programmer, the classes and ids from the back of the pieces of paper.
Next time we will write some CSS code to make things pretty.