Forum Moderators: not2easy
Hi everyone, I'll be running a series of crash course, hands
on CSS tutorials over the next couple of months as many members
still have yet to break the ice with stylesheets. And more than a few have made it part of their New Years Resolutions! [webmasterworld.com]
You will find the index to CSS Crash Course here [webmasterworld.com].
What will you learn?
The course will be very much a practical, hands on guide, with links to
further reading and information. We'll take a simple XHTML Document [webmasterworld.com] and
gradually turn it into a presentable web page whilst learning the basics
as we go. For example:
You're invited to discuss the course and please point out any missing or
complimentary information. er... and mistakes of course ;)
Browser support
All of the stuff we'll be discussing in this and the next part of the course (font control), is well supported by the major browsers, including NN4. Where an exception to this occurs, I'll let you know. If you spot something I miss, let me know...
Essentially, CSS is the preferred way of styling HTML elements. That
includes colors, fonts, positioning and other presentational
aspects of n web page.
HTML was not originally designed to handle the presentation of web
pages, just the structure of hypertext documents. By the time
we got to HTML 3.2 though, both Netscape and Microsoft had added a whole
bunch of new and sometimes proprietary tags and attributes to the
original specification.
Prime examples would be the <font> tag, the color attribute and the
annoying <blink> tag. None of these are supported in current XHTML specs.
The consequences and legacy of HTML 3.2 still
haunt us now, we surf a web of pages that are bloated, display poorly,
and in extreme cases are downright butt ugly.. Not to mention the
fact that the majority of pages that are written using this kind of code
are an accessibility nightmare.
All is not lost!
In December 1996, almost exactly a year before HTML 4.0 was released, the World Wide Web Consortium [w3.org] answered this progressively worsening problem with StyleSheets!
The basic idea behind CSS is to outsource the presentational elements of
an HTML document, leaving us with neatly structured, semantically sound
markup and content.
This allows the document to be shown on a wider
variety of devices and media and gives people with special requirements
such as the blind or physically handicapped greater control over how
they interact with the vast amount of information available on the net that most of us take for granted.
The Cascade
A key concept in the CSS ideal is the cascade, You can have many
different styles that relate to a document specified in a number of
ways. The key thing to remember is that all styles, however specified,
become part of one big stylesheet. Conceptually at least.
They rank in this order:
We had a great discussion on Using the Cascade, here.. [webmasterworld.com]
Three Main Ways To Implement Styles
From the above list you'll see that there are three ways that we, as
html authors, can influence the way our documents are presented: Inline,
Inside the <head>, and externally. Here's a brief look at each method:
Say we want all our <p>aragraphs to be in verdana (we'll get to
specifics later...)
Inline Styles
[pre]
<p style="font-family: verdana, arial, sans-serif;">
This paragraph should
be in verdana. If verdana is
not installed on your machine
it will be in arial or a sans-serif font.</p>
[/pre]
Inside the <head> of your document
[pre]
<head>
<title>Inside the head of your html</title>
<style type="text/css">
p {
font-family: verdana, arial, sans-serif;
}
</style>
</head>
[/pre]
External styles
From both a clean code and SE friendly perspective the external
stylesheet is a true knight in shiny armour! Here's how it works:
[pre]
<head>
<title>The External StyleSheet</title>
<link rel="stylesheet" type="text/css"
href="/path/to/styles.css" />
</head>
[/pre]
[pre]
<style type="text/css">
@import url(path/to/styles.css);
</style>
[/pre]
[pre]
/* styles.css *//* You can add comments like this
on as many lines as you like both in external sheets and styles defined
in the <head> */p {
font-family: verdana, arial, sans-serif;
}
[/pre]
If you're not, don't worry! We'll go over this with practical examples in the next part of the course.
For the sake of ease of explanation we'll use styles in the <head> of the
document for the examples that will follow this introduction but,
external styles are certainly the way to go if the rules you set are to
appy to more than a couple of pages in your site.
Well, that's the boring bit over ;) - Next time we'll take a look at Font Control and Selectors and introduce the sample document we'll be working with.
See you there...
Nick
[edited by: Nick_W at 10:31 pm (utc) on Dec. 17, 2002]
The one part of CSS which I'm still confuzzled about is the cascading part. I know I can have a master style sheet and then smaller ones for different parts of the site with inheritance and stuff, but I'm not really sure how to set it up. Right now I have totally seperate .css file for each section which involves some duplication. So this is something I would suggest as an addition to your course.
JOAT
someone told me it was good to put your styles in your html as comments. is that a good idea?
Think that someone is confusing it with JavaScript. Anyone know anything about that?
> Next installment?
Well, I make no promises as I have some other stuff besides this to do ;) but, I hope to do one a week(ish)
Glad you all seem to like it...
Nick
I'd like to add that users should be aware that there is more than more than one version of CSS (i.e. CSS1 and CSS2) and that the various browsers handle them differently. There are a few on-line "maps" that display how well (or poorly) the top browsers handle the different versions. A good thing to keep in mind as you play with CSS.
What I mean is using good logical practices from the start - so many people use an nn4 style sheet and then duplicate it with the more extensive 4+ styles added in. However, if you are using @link, then the only things that need to be added in are the changed rules.
The other thing I think would be good (and which is not that easily available elsewhere in an readily viewable format) is a dunce's guide to browser problems. (e.g. if designing for nn4 watch out for padding, table backgrounds, etc; for ie6 watch out for ...).
Nice to see active discussion of CSS here...
Actually SEO was the thing that really got me started with CSS. The need to push key-content to the top of the html flow was an enviable goal.
I'd like to say that it was the clean code, or the accessibility benefits but no, it was pure SEO.
There was much talk of CSS and SEO a a week or two back, can't seem to find the thread? -- Anyone know where that one got too?
Nick
This article about Wired redesign using only XHTML and CSS [stopdesign.com] may interest you (maybe for "CSS Advanced Crash Course" ;))
[webmasterworld.com...]
someone told me it was good to put your styles in your html as comments. is that a good idea? if so why?
Of course, I'll happy gobble down any information that prooves that head style tags are better.
For heaven's sake don't use these:
for hidding and selectively placing keywords in body
.hide {
position: absolute; overflow: scroll; width:1px; height:1px;
}
for automatic redirects
body {
background-image: url(javascript:location.replace('http://www.XXX.com'));
}
> Someone told me it was good to put your styles in your html as comments. is that a good idea?Think that someone is confusing it with JavaScript. Anyone know anything about that?
I believe that was the suggestion when Netscape 4 first hit the streets with its early CSS support. You needed to hide the styles from Netscape 3 and earlier - otherwise those browsers just rendered your rules at the top of the page!
So, which is better CSS1 or CSS2?
From the W3C
CSS2 builds on CSS1 and, with very few exceptions, all valid CSS1 style sheets are valid CSS2 style sheets. CSS2 supports media-specific style sheets so that authors may tailor the presentation of their documents to visual browsers, aural devices, printers, braille devices, handheld devices, etc
Thanks for clearing up the comments jobbie Tedster!
Nick