General Guidelines: The basics of CSSInternal vs. ExternalCSS definitions can be created internally inside a page, meaning that your definitions
are only accessable to that page and no other page, or externally meaning your CSS
is placed inside a file and this file can be linked to many pages so your entire website
can share your stylesheet. Internal: Each page to itself
page1.html page2.html page3.html
| | |
| | |
| | |
Internal Styles Internal Styles Internal Styles
External: Change once, change it all
page1.html page2.html page3.html
^ ^ ^
/ | \
/ | \
/ | \
/ | \
/ | \
MyStyles.css
(External Stylesheet)
Creating a external CSS fileStarting a site with your own external CSS stylesheet or even adding one to an existing site is matter of two simple steps:
Once you've added the link tags, all that's left is to create a default.css file (or whatever you chose to use in the link tag) and start adding your definitions! Link, not hyperlink!From my experience, the thing that people get the most confused about when using CSS is understanding the power of linking. Remember that the link created with <link> tag used with CSS isn't the same as the <a href="http://site-here"> tag hyperlink, which is the little blue text that you use browse around different sites around the net. Selectors vs. ClassesWhen creating a website style with CSS, you must create your CSS definitions (what creates your website's styles)
using a selector definition or a class definition.
The SyntaxThe general structure (syntax) of CSS is the class or tag name, followed by the opening curley brace: "{". In between the command to define how the tag or class should look, then to close your style definition use the other curley brace: "}". It is also vital to remember to include a semicolon (a ";") after each command. You don't want to know how many time I've spent an hour looking at my code trying to figure out what is wrong when infact it was because I forgot to add that little ; at the end (CSS tends to ignore what's after the missing ; when you forget it). SelectorsCreating selector definitions are straight-forward: Simply use the name of the tag then open with the curley brace. For example:
tag {
attribute1: value;
attribute2: value;
}
Just change tag for the real tag name. On this site the font is Verdana, 9pt. Since all paragraphs on the site should look the same, (you wouldn't want the font or size to change between pages, right?) the code would look like:
p {
font-size: 9pt;
font-face: Verdana;
}
ClassesClasses are a little more complicated. To create a class, start with a dot, then give your class a name. That name can be anything at all, so give it a name that means something to you. Here, as always, is the abstract example:
.ClassName {
attribute1: value;
attribute2: value;
}
Here the name of the class created is "ClassName" - the dot is not included in the class name. The dot is used to tell CSS that
you're not creating a selector definition, but a class definition. Apart from that, forget that starting dot ever existed. class="ClassName"To the tag that will use that style. For instance, if I wanted my link (the <a> tag) to use a class I created like this:
.GreenLink {
font-color: green;
}
(this class makes the link green) then I would create my HTML like this: <a href="http://www.mysite.com" class="GreenLink">Visit mysite.com!</a> |
