| This 45 message thread spans 2 pages: 45 (  2 ) > > || |
|CSS Crash Course - Introduction to StyleSheets|
What are Cascading StyleSheets?
CSS Crash Course - Introduction
What are Cascading StyleSheets?
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:
- Font Control
- Colors, Borders, Margins and Padding
- The CSS Box Model
- CSS-P - Using CSS for Layout
- Form presentation
- List styles
- Anything else you want (just let me know..)
You're invited to discuss the course and please point out any missing or
complimentary information. er... and mistakes of course ;)
Browser support So What are Cascading Style Sheets?
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
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! The Concept of StyleSheets
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.
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:
- Users style sheet - Styles given by the visitors browser (most important)
- Inline styles - Styles defined directly in the <html> tags
- Internal Stylesheet - Styles defined in the <head> of your HTML
- External Stylesheet - Styles defined in a separate file
- Browser defaults
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
<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>
Kind of ugly right? -- Inline styles have there uses though and are
often used for one off styles or to overide a style that was defined
elsewhere only for this element. Maybe you define all your
<p>aragraphs as arial but for this one paragraph, on this one
page, you want it to be verdana. That would be a good cause to use an
inline style. The only thing that can override this is a user defined
Inside the <head> of your document
<title>Inside the head of your html</title>
font-family: verdana, arial, sans-serif;
Preferable to inline styles but if you did this on every page, what
happens when you want to change your <p>aragraphs to a nice shade of
blue in arial?
From both a clean code and SE friendly perspective the external
stylesheet is a true knight in shiny armour! Here's how it works:
<title>The External StyleSheet</title>
<link rel="stylesheet" type="text/css"
Or you can import the styles. This does the same as above but has the sometimes useful effect of not being understood by NN4 (more on browser support later...)
Your styles.css can be named anything you like and put anywhere
you like within your public html directory. I like to keep mine in /css.
It's just a simple text file with the .css extension:
/* 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> */
font-family: verdana, arial, sans-serif;
Some of the advantages to using an external stylesheet include:
- Lighter HTML documents
- Make global site changes by editing just one file
- Web browsers cache external stylesheets (think about it....)
- Can be easily overridden if need be with styles in the <head> or
Important point on multiple styles
It's perfectly legal and very common to have multiple styles defined on the page from multiple sources. For example, your HTML document links to a stylesheet that defines your <p>aragraphs, then you include another style in the <head> of your page to define the <p> tag. Who wins? - The closest one. If you were to further define an inline style on one of your <p> tags, that style would take precedence. With me?
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...
[edited by: Nick_W at 10:31 pm (utc) on Dec. 17, 2002]
Great Post Nick!
Nice Layout, Interesting Content
This looks good, Nick_W :)
Took me a year reading courses to find out what CSS actually was, never mind how it works. You seem to sum it up nicely above....... LOL
This is going to be very useful :) I remember my confusion when trying to convert to external style sheets from a mixture of an internal style sheet and inline styles. If I'd had a summary like this from the begining I would have done it properly from the start.
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.
Well done Nick...
Good Luck wish it was there when I needed it!
sp? as always![/edit]
Shellycat, that sounds like a perfectly good way to do things to me, what worries you about it?
excellant idea nick, i think i will be reading the series as it goes along.
Fantastic Nick.... whens the next installment? :)
Excellent post :)
Great thoughts Nick. This is very much appreciated.
hey nick, someone told me it was good to put your styles in your html as comments. is that a good idea? if so why?
BTW, this crash course will be great! :) go on nick!
|someone told me it was good to put your styles in your html as comments. is that a good idea? |
> 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...
Fab job 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.
As an addition to what shelleycat mentioned in the thread, it might be worthwhile dealing with the cascade and non-duplication of styles - as this is something which would have helped me tremendously at the beginning if I had got a good grasp of this fundamental when I was learning.
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...
great post Nick, I for one will be following closely.
Are you intending to cover a bit about how css layouts can help with seo?
Hehe, of course! -- This is a WebmasterWorld thread afterall ;)
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?
Good post Nick.
This article about Wired redesign using only XHTML and CSS [stopdesign.com] may interest you (maybe for "CSS Advanced Crash Course" ;))
|troels nybo nielsen|
Nick, is this the one you're missing?
|someone told me it was good to put your styles in your html as comments. is that a good idea? if so why? |
Unless you need page specific styles (and even those can be made externally, if you so desire) then I can't see any benefit of using head defined or inline styles. At least, I've not come across a good reason yet, and I've long been a fan of css, its just so tidy!
Of course, I'll happy gobble down any information that prooves that head style tags are better.
Hooray! - Thnnks very much...
CSS and SEO [webmasterworld.com]
Some good points in there...
>Are you intending to cover a bit about how css layouts can help with seo
For heaven's sake don't use these:
for hidding and selectively placing keywords in body
position: absolute; overflow: scroll; width:1px; height:1px;
for automatic redirects
So, which is better CSS1 or CSS2?
CSS2 is the natural progression from CSS1, but few browsers support it properly or fully. Even browsers that boast standards support muck bits up from time to time, though its increasingly hard to do so.
|> Someone told me it was good to put your styles in your html as comments. is that a good idea? |
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!
re: commenting css - i'm seeing it more and more often and never quite 'got' it - still don't. I never comment out my css and have never had a problem with it. Tho tedsters point makes sense if you consider how many fubar and put it after the body tag :D
I found this lesson of great help and I am looking forward to your next post.
Great post, Nick.
Question: what is the difference between
link rel="stylesheet" and
Is there a difference other than browser support? when should I use one or the other?
There is no difference transistor except the fact that you can import additional styles into an existing stylesheet from and existing stylesheet...
| This 45 message thread spans 2 pages: 45 (  2 ) > > |