|
Cascading Style Sheets (the SPAN and DIV tags) The colors below are for instructional emphasis.
Now Preview this code by clicking on the Preview button in CodeRunner below. Notice anything? Right. It didn't do anything. Now add the red text below:
Preview this. You should have gotten a some white text with a blue background around it, like this. What we have here is a new HTML attribute, the style attribute. After the equals sign and in between a pair of quotes are a couple of properties the background-color and the color properties. Values for properties are set after a semi-colon :. Take some time right now to try changing SPAN to DIV to see what happens Also, we can put any HTML within the SPAN tag. Try putting HTML you are familiar within the SPAN tag. This is it. All we are going to learn in the rest of this and the next two DHTML learning labs is some more properties and what values to set. There are a lot of properties we can set in CSS. Let's try some more properties:
We just added the font-size and font-family properties. Notice that the font-size property value is 16pt. That stands for 16 point. You should try many different sizes right now. You can specify the font-size by point size (as we did), or percentage, or smaller or larger, or absolute expression (absolute expressions are xx-small,x-small,small,medium,large,x-large,xx-large).
Percentage, and smaller or larger are relative sizes to the parent element. For example if we put the whole sentence in a <font> tag and use 40% font-size like this:
Preview this and try some other percentages. The font-family should be a font that you expect computers to have. The most computers have the following fonts built in: Times,Arial or Helvetica, and Courier. Notice that we listed the fonts seperated by a comma. The browser looks for the fonts on the computer in the order that you have listed, if it doesn't have that font it looks for the next and so on. It is generally a good idea to list the Generic font family at the end after the specific examples. There are generally four types of generic fonts:
Using the STYLE tag
Preview the code if only to see that it works Pretty redundant, eh? Too much typing! Isn't there a lazy way to do this? Would I mention it if there wasn't? ;o) The STYLE tag, IDs and CLASSes with save the day! The following code will do exactly the same thing as the previous code we just did:
Preview this to see that it is the same as the last one. Much easier to type, eh? We define the properties for the id billy and then user the ID ATTRIBUTE to select the properties defined in #billy (the # sign is important, try taking it out and see what happens). All property definitions go between the <style type="text/css"> and </style> tags, and are blocked off with a pair of { and } after the id name. Of course we can add individual properties using the style attribute even after we have we have used a ID selector. Observe this by trying the following code:
Preview it of course. Notice that STYLE ATTRIBUTES defined within a tag override defintions in the STYLE tag.
Notice the period before the word "friends", that is important. To define a class you put a period before the CLASS NAME followed by the definition enclosed by brackets. You now pretty much know most of the syntax of CSS. In the next lab we find out that we can add and change properties of HTML tags that we are already familiar with, and later that the so called CSS-P (Cascading Style Sheets Positioning) is nothing more than some more properties. At the end of learning lab 4 we will point you to complete lists of CSS properties. See you at the next learning lab! |