1.

Solve : CSS in Web Design?

Answer» Changes since ORIGINAL version:Version 1.01 - Small proofreading done; clarifications added.
Version 1.02 - Added section on multiple external style sheets.
Version 1.03 - Added section on IMPORTING style sheets.
Version 1.04 - Added section on CSS comments.
Version 1.05 - Modified section on importing style sheets.
Version 1.06 - Added info on pseudo-classes; corrected terminology.

Note: This tutorial assumes you are familiar with at least basic knowledge of HTML.
Question
What is CSS?

Answer
CSS is short for "Cascading Style sheets". It is a method of formatting text when building HTML Web Pages. Using multiple style sheets will cause them to "cascade", hence the name.

Question
Why should I use CSS?

Answer
There are two very important reasons. The first is that it replaces deprecated tags. Deprecated tags are obsolete tags made obsolete either by newer HTML CODE or by CSS. Browser support for deprecated tags decreases with each new generation, so eventually to comply with newer versions you will need to learn CSS.

Another good reason is that if you use a "master" style sheet, it is possible to change one setting and have it apply to all documents that use that style sheet. Change the layout of 20 tables on 5 pages with one small edit! Impossible, you say? Not with CSS!

Question
How do I use CSS?

Answer
The main syntax of a CSS setting is:

Selector.Class { attribute: value; attribute: value; }

There are also pseudo-classes, such as:

:link - properties of unvisited links.
:hover - properties of links when the mouse is over them.
:ACTIVE - properties of links when they are being visited.
:visited - properties of links users have visited.

Kudos to robpomeroy for name correction and pseudo-classes.

The key for this is as follows:

Selector: Where this class applies. (Each set of values is considered a "class") For example, p.fire would only work with the

tag, and td.fire would only work with the table cell. If you specify an asterisk (*.fire) then this class will work with whatever you assign it to.

TipSo why would you ever use anything besides *.etc, anyway? Well, if you want the same name to mean different things to different tags, you can specify that and only need to use one name.

For example, let's say you wanted to create a fire theme for your web page. Let's also say that you want paragraphs to be red on a black background, and table cells to be black on a yellow background. You could use these CSS classes, even in the same style sheet:
Code: [Select]p.fire { background-color: black; color: red; }
td.fire { background-color: yellow; color: black; }
Both will use the same class name, so when you call classes in HTML you can just specify "fire" across the board and it will effectively apply your theme.

Class: Whatever you want to call it. Ideally, you want it to be short while being descriptive at the same time. In other words, if "one" isn't significant to you, don't name your attribute that.

Attribute: The part of the element you want to modify. Valid attributes include background-color, text-indent, border-style, and more.

Value: What you set the attribute to. For example, a valid text-indent value would be "2%". A valid background-color value could be "black". Acceptable values vary from attribute to attribute.

NOTE: It is not necessary to include the spaces between the { and the code and the }; however, this makes your classes much more readable.

Tip
In order to keep track of what class does what in your style sheets, you will want to implement comments. Comments in CSS are very simple to put in, and they will help identify what your code is about. Comments are placed between /* and */. For example:

Code: [Select]/* Background for the main site */
BODY { background-color: blue; }
Codes can also be multi-line, as in this example:

Code: [Select]/* Background for the site.
Note to IT: Blue not a good color; blends with images */
body { background-color: blue; }
Use them when appropriate, but be careful of overdoing them. You don't need a paragraph on the table cell element; a sentence or two will do.


Now, you need a place to put all these classes, right? Well, there are several methods of doing this. They are:

  • Create an external style sheet. Open Notepad and type in your classes. When done, go to "file" and "save". Under "File types", select "all files(*.*)". Save it in the same folder as your home page. Name it whatever you'd like, but make sure the name ends in .css.


From within your HTML document, make a new line between your and tags. In this new line, type the following:

Code: [Select]<link rel="stylesheet" type="text/css" href="mystylesheet.css" />
replacing "mystylesheet.css" with the name of your .css file.

But those classes are not automatically applied. To apply them, when you make your tag, add 'class="myclass"' to the end. For example, if you had the following class:

Code: [Select]p.fire { background-color: black; color: red; }
Then in your HTML, if you wanted a paragraph to have this style, you'd type this:

Code: [Select]<p class="fire">This text uses a FIRE class!</p>
and you'd get nicely formatted text (assuming you declared the .css file in the tags). This method is ideal for creating a "master" style sheet that applies to the entire site. For example, if you created a style sheet and linked to it in all of your pages, you essentially have applied a style to the entire site. This way, if you want to change the background color of all your pages, you don't have to modify the bgcolor attribute in every page - that attribute is deprecated anyway. Instead, just change the value in your style sheet, and bingo! the whole site changes.

  • Create a style sheet inside your web page, called an internal style sheet. To do this, underneath your link to the .css file (if you use it) type the following:

    Code: [Select]<***style*** type="text/css">
    <***/style***>
    ***PLEASE NOTE: Wherever I use a style tag, I am forced to put the *** in the tags. Do not use those ***'s in your actual tag!

    Between the two <style> tags, place all the classes you would normally put in a .css file.

    TipYou may be thinking: "Why would I use this method when I've got this nifty .css file?" And the answer is this: You use this method if you want one page to be the exception to the overall style. More detail on this later.

  • Use inline styles within the tag. To do this requires no additional tags. Let's return to the p.fire example. You can do this within a <p> tag by typing this:
Code: [Select]<p style="background-color: black; color: red">This paragraph uses a specially-defined FIRE style!</p>
And there you have it: Perfectly formatted text without even defining a style.

TipSo why on Earth would you define this here instead of at the top of the page? You may have already guessed it, but I'll make it perfectly clear: If you want a particular paragraph or cell table to be different from all the rest on the page and site, this is the most efficient way to do it. Should your whole site be designed this way? Nope. Use the first method unless you need exceptions to the rules you specify.
[/list]

Question
Is it possible to use two classes in one element?

Answer
Yes! The fact that you can, I've found, is very freeing. It eliminates the need for an additional class that just combines two other classes. To do this is extremely easy.

Code: [Select]<p class="fire brimstone"></p>
The above code will call the FIRE class and the BRIMSTONE class.

However, you should note that it is possible for two classes to conflict. For an example of this, let's say you have a .css file that includes

Code: [Select]p.fire { background-color: black; color: red; }
and let's also say you have a one-page



Discussion

No Comment Found