Here are a few tips to get you started using CSS effectively. Many more CSS resources are available on the Web. The best way to learn is to look at the code of pages and sites you want to emulate.
Types of Styles | FrontPage 2003 | Dreamweaver MX 2004 | Page Layout | Organizing CSS Code
Types of Styles
- Inline styles format a specific tag on one page. The
style
attribute is much like other HTML attributes, though it can contain more than one style declaration, separated by semicolons.- For example, to make a heading tag blue, change
<h1>
to<h1 style="color: blue;">
- For example, to make a heading tag blue, change
- Internal (embedded) stylesheets apply to one or more tags on one
page. For example:
- To make all heading tags
<h1>
and<h2>
in the page blue, add these lines to the<head>
section of the code:
<style type="text/css">
h1, h2
{
color: blue;
}
</style> - To make one or more heading tags larger using a class, change
<h1>
to<h1 class="large">
and add a style to the header:
<style type="text/css">
.large
{
font-size: 3em;
}
</style> - To make one heading tag larger using an id, change
<h1>
to<h1 id="huge">
and add a style to the header:
<style type="text/css">
#huge
{
font-size: 250%;
}
</style>
- To make all heading tags
- External (linked) stylesheets can be included in multiple pages
using a link tag.
- For example, if you have a stylesheet file named
styles.css
, you can include it into any HTML document by adding this line in the<head>
section of the code:
<link href="styles.css" rel="stylesheet" type="text/css" />
- For example, if you have a stylesheet file named
- Which style will be used? If there is a conflict, the order of priority/importance is inline > internal > external > browser default.
CSS Tips for FrontPage 2003
- To create an internal style to modify the style of an existing
tag:
- Click Format, Style... Click a tag such as
h1
and click Modify... Click Format, Font... Select a font, size, style, color, effects, and/or character spacing. Click OK 3 times. - You should see that all
h1
tags in the document are now formatted differently. - Click the Code tab in the lower left corner to see the CSS code that was inserted into the HTML document for you. Click the Design tab to return to the normal view.
- Click Format, Style... Click a tag such as
- To create a selector (class) to modify the style of selected tags:
-
- Click Format, Style... Select User defined styles from the List menu. Click the
New button. Type a name for the selector (e.g.
large
). Click Format, Font... Type in a different font size (e.g.3em
). Click OK 3 times.
- Click Format, Style... Select User defined styles from the List menu. Click the
New button. Type a name for the selector (e.g.
- To apply a class to a tag:
-
- Select some text (such as a header) that has a tag associated with it.
- Click Format, Style..., and the style name (e.g.
.large
).
Or click the arrow in the Style menu (in the upper left corner, left of the font menu) and click the style name. - Click OK. You should see the text has become larger.
- To create an id to modify the style of a single tag:
-
- Click Format, Style... Select User defined styles from the List menu. Click the
New button. Type a name for the selector, starting with a # symbol (e.g.
#huge
). Click Format, Font... Type in a different font size (e.g.250%
). Click OK 3 times.
- Click Format, Style... Select User defined styles from the List menu. Click the
New button. Type a name for the selector, starting with a # symbol (e.g.
- To apply an id to a tag:
-
- Switch to Code or Split view.
- Add an id attribute to the tag, e.g. replace
<h1>
with<h1 id="huge">
- Switch to Design view, or press F5 if you are in Split view. You should see the text has become larger.
- To create an external stylesheet:
-
- Click File, New, more page templates...
Or click the arrow next to the white icon in the upper left corner and click Page... - Click the Style Sheets tab. Select a stylesheet and click OK. Make any desired changes
to the styles. Click File, Save As and give the stylesheet a name (e.g.
styles.css
).
- Click File, New, more page templates...
- To link a document to an external stylesheet:
- Open any HTML document. Click Format, Style Sheet Links... Click Add. Browse to
locate the external stylesheet file which has a .css extension (e.g.
styles.css
). Click OK twice. - You should see that your HTML document has changed to use the styles in the external stylesheet.
- Open any HTML document. Click Format, Style Sheet Links... Click Add. Browse to
locate the external stylesheet file which has a .css extension (e.g.
CSS Tips for Dreamweaver MX 2004
- To create an internal style to modify the style of an existing
tag:
- Click Text, CSS Styles, New...
Or click the New CSS Style button at the bottom of the CSS Styles tab in the Design panel. - For the Selector Type, click the Tag radio button. Select the tag (e.g. h1) from the tag menu. Select the radio button Define in: This document only. Click OK. Select properties such as font and/or size. Click OK.
- You should see that all
h1
tags in the document are now formatted differently. - Click the Code tab in the upper left corner to see the CSS code that was inserted into the HTML document for you. Click the Design tab to return to the normal view.
- Click Text, CSS Styles, New...
- To create a selector (class) to modify the style of selected tags:
- Click Text, CSS Styles, New...
- For the Selector Type, the Class radio button should be selected. Type the tag name
(e.g.
.large
). Select the radio button Define in: This document only. Click OK. Select some properties and click OK.
- To apply a class to a tag:
- Select some text (such as a header) that has a tag associated with it.
- Click the Style menu (in the Properties window, near the bottom of the screen) and
click the style name (e.g.
large
). - You should see the text of the selected tag has changed.
- To create an id to modify the style of a single tag:
-
- Click Text, CSS Styles, New...
- For the Selector Type, click the Advanced radio button. Type the tag name, starting
with a # (e.g.
#huge
). Select the radio button Define in: This document only. Click OK. Select some properties and click OK.
- To apply an id to a tag:
-
- Select some text (such as a header) that has a tag associated with it.
- Right-click the tag (e.g. h1) in the bar just below the document and above the Properties window.
- Move to the Set ID menu and click the style name (e.g.
#huge
). - You should see the text of the selected tag has changed.
- To create an external stylesheet from a template:
- Click File, New... In the General tab, click CSS Style Sheets. Select a stylesheet in the right side of the dialog box, and click Create.
- Make any desired changes to the styles. Click File, Save As and give the stylesheet
a name (e.g.
styles.css
).
- To link any HTML document to an external stylesheet:
- Open the HTML document.
- Click Text, CSS Styles, Manage Styles... and click Attach.
Or click the Attach Style Sheet button at the bottom of the CSS Styles tab in the Design panel. - Browse to locate the external stylesheet file which has a .css extension (e.g.
styles.css
). Click OK. Click Done. - You should see that your HTML document has changed to use the styles in the external stylesheet.
- To modify the style of a selector in an internal or external stylesheet:
- Click the CSS Styles tab in the Design panel. Double-click any style.
- In the CSS Properties tab, click the + next to any category (e.g. Font) and fill in a value for any of the CSS variables (e.g. font-size).
Page Layout using CSS
- To create page layout using HTML only, designers create a large table and put parts of the page into table cells. Sometimes designers put tables within tables to further organize content. These methods usually work for most users, but the resulting pages are often not very accessible.
- To create page layout using CSS, use
div
tags instead of tables and table cells. Think of a div as a rectangular box that holds content. It behaves much like a table, image or other rectangular element. Divs are used to contain one or more elements including other divs, just like tables can contain cells and other tables. - To position a div, image or table to the left or right part of the screen, use
float: left
orfloat: right
.- To keep following content from wrapping next to a floated element, use
clear: both
.
- To keep following content from wrapping next to a floated element, use
- To center a heading or other block of text, use
text-align: center
.- There are various ways to center the entire body of a document but left-align the
text, e.g.
- Fixed width body:
<div align="center">
<div style="width: 780px; text-align: left;">
contents
</div>
</div> - Variable width body (preferred):
<div style="margin: 0% 10%;">
contents
</div>
- Fixed width body:
- There are various ways to center the entire body of a document but left-align the
text, e.g.
- To position something at an exact location on the screen, use
position: absolute
and specify the distance fromtop
,bottom
,left
and/orright
of the page.- Note that this may cover up something else in the same position. To position this
layer on top, use a higher positive value for
z-index
.
- Note that this may cover up something else in the same position. To position this
layer on top, use a higher positive value for
- You can set the
width
andheight
of a div to more precisely control its size.- By default, a div is as wide as the screen. Use
display: inline
to make it as narrow as its contents.
- By default, a div is as wide as the screen. Use
Organizing CSS Code
- Use HTML tags as they were designed to be used. (Show the semantic
structure of the content.)
Useh1
for the main page header. Useh2
for subheadings. Usep
for paragraphs. Useol
andul
for ordered and unordered lists. Usetable
for tabular data. - Format using CSS instead of HTML as much as possible.
Avoid HTML formatting tags likefont
,b
,i
,u
,br
, andhr
.
Avoid HTML formatting attributes likecolor
,bgcolor
,border
,width
andheight
. - Keep as many styles as possible in external stylesheets.
This assures that all the pages in the site have the same formatting and layout.
Use internal and inline styles sparingly, e.g. for features unique to a single page or single element of it. - Consider grouping related styles together for easier editing. For
example,
- Use separate sections for styles of page sections such as header, footer, navigation bar, and content.
- Separate global styles that override HTML tags (e.g.
body
,h1
,p
,li
) from local styles for classes and IDs (e.g.div#header
orp.small
). - Use separate styles for color scheme, font and page layout.
- Explain your styles using comments (enclosed in
/* */
).
This is especially at the top of groups of styles, and near code that may not be obvious to someone else. - Use a consistent code formatting scheme for readability.
For example, put{
and}
in the left margin on separate lines, and indent lines between them with one tab.