Some of the benefits to using CSS are more consistency, easier HTML
coding, better layout and
visual design. Also you can do things with style
sheets that could never be done before. One of the best features of style sheets is its ability to degrade
nicely with older browsers. Most CSS styled documents still appear
perfectly readable and formatted when viewed in a browser that does not
support style sheets. Furthermore, CSS is now supported (to some degree)
by most browsers (IE4+, NS4+), making it a very practical technology.
The browser needs to know what style sheet or style elements being
specified. There are several ways style can be specified:
If you are familiar with JavaScript you will notice the same use of
comments to fool older browser to ignore the extra code. The HTML
specification states that browsers are to ignore tags they do not
understand. Thus, an older browser reading this could would ignore the
STYLE tag, and then bypass the style code because it is between HTML
comments (<!-- -->)
The previous page explained how style sheets are referenced by the
browser. This page expands on that and shows several working examples of
style sheets.
Starting off with applying a color to a simple header tag, H1. Here is
the complete code for the HTML file with a simple blue header.
If you want your header aligned in the center of the document, you can
add the 'text-align' property as follows:
Classes allow you to define a style whereby this style can then be
applied to multiple elements on your page. To create a class, use a
custom name with a period in front of it. For example:
IDs are similar to classes except they work on a per element basis. The idea behind the ID is to
define a certain style for a single specific element. ID is also used
extensively by DHTML to
reference and manipulate this element. They can not be used
by multiple tags on the same page as the above example did.
IDs are defined with a pound sign (#) in front of a custom name.
Being the tricky guy that I am, I have used a new tag, SPAN, to segue
into the next topic.
Two new tags have also been introduced to implement CSS. The tags alone
do not have any predefined attributes, and are there for style to be
applied to them.
Ok, I think we have the basics out of the way. Let's get down and dirty
with CSS now.
The following will cover several specific attributes you can change using
style sheets. These are margins, fonts and text, and backgrounds.
Margins
Style sheets allow you to specify margins. What a nice addition to web
pages. No longer will text be shoved right up against the sides of the
browser, and no more extraneous tables to work around this. Well maybe not
all table workarounds will disappear, but they can be much simpler.
With margins you can format the text within tables or any HTML element.
Also you can indent single lines, whole paragraphs, image spacing, and
more.
There are 5 margin properties, well really 4 and 1 shorthand. These
properties are:
margin-top
margin-bottom
margin-right
margin-left
margin (shorthand)
The values they accept are lengths, or percentages. The
lengths are measured in pixels(px), inches(in), centimeters(cm),
millimeters(mm), font point (pt), font height (em), and picas (pc). The percentages are relative to the object, which could be the
whole page, or 'inside' of another object, like a table. Negative values
are accepted, but be careful when using them.
Examples
P { margin-top: 10px; margin-bottom:
75px;
margin-left: 50px; margin-right: 25px; }
This will define the above margins for everything in paragraph tags. Play with resizing your browser window, notice the
margins stay the same width. Click
here for an example
The margin shorthand notation could have been used in the above and
would have looked like this:
P { margin: 10px 25px 75px 50px }
The shorthand order being top - right - bottom - left.
Some even nicer short cuts, if using similar values, can be used. To
set a 0.5in margin on each side you could use:
P { margin: 0.5in }
If you have the same top and bottom margins, and the same right and
left margins you can use the following shorthand notation:
P { margin: 0.5in 1.0in }
This would create a half inch margin top and bottom, and a full inch
margin on the left and right.
Here's a definition to indent text
.indent1 { margin-left: 5%; margin-right:
30%; }
.indent2 { margin-left: 30%; margin-right: 5%; }
Click
here see example. It shows shifting paragraphs around using margins,
this probably could be a quote or image in the now empty blank spots.
Fonts and text
Finally some real control over fonts, yipee!!
There are 5 font properties, and 1 shorthand.
font-family
font-style
font-variant
font-weight
font-size
font (shorthand)
font-family
The font-family property specifies what font will be used, ie. Courier,
Helvetica, Arial, etc... or if not available what general family to be
used: serif, sans-serif, monospace.
The fonts are listed in preferred order. One nice thing about listing
numerous fonts is that if the user system does not support one font,
another font in the list will be used instead.
Examples:
BODY { font-family: Times, TimesRoman,
serif }
P { font-family: Helvetica, Verdana, Arial, sans-serif }
H1 { font-family: Monaco, Courier, monospace }
Other generic font families that can be used are cursive and fantasy.
The generic font families above are serif, sans-serif and monospace. If
your font has more than one word use quotes.
.comix { font-family: "Comic Sans
MS", sans-serif }
font-style
Valid values for font-style are normal, italic, and oblique. Very
straight forward, oblique is similar to italic.
font-variant
Valid values for font-variant are normal and small-caps. Small caps
displays the lowercase letters as scaled down uppercase letters.
font-weight
This property specifies what you want the font to weigh, ex. 50 lbs of
pure Courier. Well not exactly, the weight of the font is the boldness, or
lightness of the font. The valid values that you can assign font-weight
are:
normal, bold, bolder, lighter
and 100, 200, 300, 400, 500, 600, 700, 800, 900
Note: normal=400, bold=700
font-size
You guessed it, this specifies the size of the font. There are 5
different ways you can specify the font size. In no particular order, here
they are:
absolute size:
point size: ie. 7pt, 22pt, 14pt, 36pt, 72pt, ...
pixel size: ie. 100px, 45px, 90px, 10px, ...
relative size:
key words: xx-small, x-small, small,
medium, large, x-large, xx-large
percentage: 24%, 58%, 150%, 10%, 100%, ...
ems: .24em, .58em, 1.5em, .1em, 1.0em, ...
For font-size, ems are equivalent to percentages. Also you only put in
the values for the size you want, do not specify the descriptive words
absolute size, point size, etc...
Pixel sizing is not recommended by Netscape. The relative sizes are
relative to the parent element. For example, if a base font size is set in
the BODY, then the the relative sizing is relative to that base font size.
font
Font is a shorthand notation to keep our sheets a little cleaner and
neater. The order of the shorthand is 'font-style' 'font-variant'
'font-weight' 'font-size' 'font-family' Also keeping with normal
typographical syntax, you can specify the leading, or line-height along
with the font-size by using font-size/line-height.
Examples:
H1 { font: bolder 72pt Impact, "New
SchoolBook", sans-serif }
H2 { font: 700 36pt/48pt Monaco, Courier, monospace }
H5 { font: lighter 12pt Times, TimesRoman, serif }
Text
There are 5 text properties that you can specify.
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
text-align
text-indent
line-height
letter-spacing and word-spacing
Exactly how it sounds, this specify the spacing between letters and
words. The valid values are length values, such as em, px, pt, %,...
Examples:
P { word-spacing: 0.75em; letter-spacing:
10px; }
text-decoration
The text-decoration values you can assign to it are: normal, underline,
overline, line-through, and blink (blech!!) Each one is fairly self
explanatory. Here is the popular definition that rids all links of the
underline beneath them:
A {text-decoration:none}
vertical-align
Valid values are: baseline, sub, super, top, text-top, middle, bottom,
text-bottom.
I am not exactly sure the minute differences between all of these, play
around with different alignments and see what does what.
text-transform
This property can transform text to either uppercase, lowercase, or capitalized. The valid values are: none, capitalize,
uppercase, lowercase.
text-align
This probably should have been called horizontal-align, since they have
a vertical align, but my guess is that that is to hard to spell and type
in numerous times. Valid values for text-align are: left, right, center,
and justify.
text-indent
Indented a paragraph, what a concept. Valid values are length and
percentages. This property will indent the first line in a paragraph, the specified
length or percentage.
Examples:
.indent1 { text-indent: 1.0em; }
.indent2 { text-indent: 30px; }
line-height
This allows you specify the height of a line, which will allow you to
specify the distance between two lines. I found this a little tricky to
use, it doesn't work well as an inline property, I found as a paragraph
property works pretty good. Valid values are: length and percentages, negative values are not allowed.
Example:
P.listi { line-height: 80%; }
Backgrounds
There are 5 background properties, and 1 shorthand.
background-color
background-image
background-repeat
background-attachment
background-position
background (shorthand)
The important thing to note about backgrounds is that it doesn't have to apply to
the whole page, as the normal background HTML tag does. You can specify backgrounds in paragraphs, table cells, headers and
almost anything else you want.
background-color and background-image
These two are very straight forward. For background-color the valid
values are colors, be it rgb, hex, or color name, and transparent, which
makes the background transparent.
There are only two values for background-image, none and a url to point
to an image object.
Examples:
P.bg1 { background-color: #000000;}
P.bg2 { background-image: url(images/bg1.gif) }
background-repeat
Valid values are: repeat, repeat-x,
repeat-y, and no-repeat. A value of
repeat, repeats the background horizontally and vertically. repeat-x only
repeats the background in the horizontal direction, repeat-y vertical
direction. And as you might have thought no-repeat does not repeat the
background at all.
Example:
<BODY
style="background-repeat:no-repeat"
background="test.gif">
background-attachment
There are two values for background-attachment, scroll and
fixed. A value of "scroll" is what is common with browsers today. The background
scrolls along as you do. A value of "fixed" will keep the background fixed,
with the text and other goodies scrolling on top of it.
Example:
<BODY
style="background-attachment:fixed"
background="test.gif">
background-position
You can specify where the background is to be placed with this
property. Valid values are: length, percentage,
top, center,
bottom, left,
right.
For example, the below declaration positions the background image at
the lower footnote of the page:
<BODY style="background-position: bottom
right" background="test.gif">
background (shorthand)
Finally, we've stumble upon the shorthand notation for background,
essentially all you need to manipulate the background image using CSS. The
notation combines
background-color, background-image,
background-repeat, background-attachment, and
background-position, in that order.
Here's the classic example that renders a non repeating background
image, centered and fixated on the page:
BODY { background: white url(bg1.gif)
no-repeat fixed center center }
Grouping and contextualizing
selectors
CSS supports 2 nifty declarations to help streamline and shorten your
style sheets. Let's look at grouping first:
-Grouping
Identical styles that apply to different elements can be combined into
one. Simply separate each element with a comma(,), followed by the style
itself:
BODY, P, #mytable {background-color:gray}
With the above, your <BODY>, <P> and ID "mytable"
will all have a gray background.
-Contextualizing selectors
Getting a little technical here, in a CSS definition, the selector is the tag, id, or class that
proceeds the style. For example:
P {font-weight:bold}
.myclass {color:red}
"P" and "myclass" are called selectors.
CSS allows you to nest one selector within another, in which the result
is that the style will only be applied when the HTML source contains such
a setup. Take a look at this:
TABLE A {color:brown}
DIV B {background-color:yellow}
This will cause only links that appear inside a table to be brown in
foreground color, and bold text that appear inside a div to be yellow in
background.
Cascading and
inheritance
There's a reason why CSS is called CSS (Cascading Style Sheets), and it
has to do with the order of precedence in which styles are applied. When
you have an linked, embedded, and inline style sheet all defined on the
same page, the order of precedence is ascending. For example, if both an
embedded and inline style sheet attempts to manipulate the same aspect of
the same element,
inline dominates.
Inheritance refers to how the style of one element in "passed
on" to another provided no explicit style is assigned to the later.
Some style declarations (ie: font-family)
are inherited, meaning they affect not just the element it was assigned
to, but elements under it as well:
<P
style="font-family:Verdana">This font is Verdana. <b>So
is this font</b></P>
Both P and B carry the font Verdana, even though the style was only
defined on P. Inheritance exists to save the user the hassle of redefining
styles that are obviously intended for everything the element encapsulates.
A good CSS reference will list which style in CSS are inherited, and which
are not.
On that note we come to a conclusion. You now should have a solid
understanding of CSS, and how to take advantage of it on your site.