Web Design Tips
Cascading Stylesheets: Exercises
Why | How | Exercises | Links
Requirements | 1. Viewing and Applying Styles | 2. Creating Styles | 3. Text Formatting | 4. Positioning
Show instructions for:
FrontPage |
Dreamweaver |
Both
Software Requirements and Prerequisite Knowledge
- Internet Explorer and either Firefox or Netscape (to browse the Web and test pages)
- FrontPage 2003 or Dreamweaver MX 2004 (to author Web pages)
- Basic knowledge of HTML code (commonly used tags and attributes)
- About 30 to 60 minutes per session. Each session builds on the previous session.
- See the CSS links page for more information and online references.
Session 1: Viewing and Applying Existing Styles
- View the results of disabling a stylesheet.
- Go to the CSU Libraries home page.
- Click the Disable Stylesheet link.
- Click the Enable Stylesheet link.
- Open Firefox.
- Click , , . The current page will lose its formatting.
- Click , , to return to regular browsing.
- View some other pages without their stylesheets, such as:
- Open Internet Explorer.
- Click , , . Check the three Ingore check boxes. Click OK. The current page will lose its colors, font sizes and font styles, but some styles will remain.
- View some of the above pages.
- Click , , . Uncheck the three Ingore check boxes. Click OK to return to regular browsing.
- View the results of alternate stylesheets.
- Go to W3Schools: CSS in action.
- Click the 6 links in the "Click on the styles" section (With Style1-5 and Without styles).
- Go to ATRC: CSS Style Sampler.
- Click each of the 1 2 3 buttons to see different stylesheets applied to the same HTML page.
- View the results of a print stylesheet.
- Go to Garst Wildlife Photographic Collection.
- Click , . Notice that the navigation is hidden.
- View the source code of an HTML page.
- Go to W3Schools: CSS in action or ATRC: CSS Style Sampler.
- Right-click in the page body. Click . (In Firefox, click , .)
- View the source code of an external stylesheet.
- Go to W3Schools: CSS in action.
- Click the 5 links in the "View the stylesheets" section.
- Can you explain what each line in each stylesheet does?
- Copy an example to your computer.
It includes an HTML page, an external stylesheet, and 3 images.
- View the example HTML page in a browser. Click , . Create a folder to save the file, or click My Documents. Type
example.html. Click Save.
- View the example stylesheet in a browser. Click , . Type
example.css. Click Save.
- Right-click these links to the CSU libraries logo, the CSU logo and the background. Click or . Save all documents into the same folder.
- Open your copy of
example.html using FrontPage or Dreamweaver.
- Switch to Code or Split view to see the source code. It is just plain XHTML, with a few
div tags and id attributes.
- Link to an external stylesheet.
- In FrontPage, click , . Click Add. Click
example.css. Click OK twice.
- In Dreamweaver, click , , . Click Attach.
Or: Click the panel on the right, click the tab, and click the Attach Style Sheet icon
.
- Click
example.css. Click OK. Click Done.
- Switch to Design view. You should see that your document has been reformatted using the stylesheet.
- Switch to Code or Split view. You should see that a line of code has been added that looks something like this:
<link href="example.css" rel="stylesheet" type="text/css" />
- Make sure that the path to
example.css is relative. There should be no folders before example.css.
- Press Ctrl-S to save your changes to
example.html.
- Press F12 to test in a browser if desired.
- Apply an existing class to an HTML tag.
- Select the Section 1 heading. Select from the Style menu. You should see that the heading has become red.
- In FrontPage, for internal styles, click , , select from the List: menu, select from the Styles: menu, and click OK.
- In Dreamweaver, right-click the
<h1> tag in the bar between the document and the Properties window, move to , and select .
- Repeat for the Section 2 heading. Note that you can apply a class to more than one element.
- Repeat for the Example HTML Page heading. Note that you can apply a class to different types of elements.
- Apply an existing ID to an HTML tag.
- Select the Introduction heading. Switch to Code or Split view. Change
<h2> to <h2 id="redborder">.
- In FrontPage, for internal styles, click , , select from the List: menu, select from the Styles: menu, and click OK.
- In Dreamweaver, right-click the h2 tag, move to , and select .
- Switch to Design or Split view. You should see that the heading has a red border.
- Remove a class or ID from an HTML tag.
- Select the Section 1 heading.
- In FrontPage, select from the Style menu.
- In Dreamweaver, select from the Style menu.
- Repeat for the Section 2 heading and Example HTML Page heading.
- Select the Introduction heading. Switch to Code or Split view. Change
<h2 id="redborder"> to <h2>.
- Switch to Design or Split view. You should see that the headings have returned to normal.
- Work with styles in Microsoft Word.
- Open csspractice.doc.
- Format the first line as Heading 1.
- Format the second line as Heading 2.
- Format the remaining headings as Heading 3.
- Add some text after "Session 1: Viewing and Applying Existing Styles".
- Select "Session 1: Viewing and Applying Existing Styles".
- Click , .
- Right-click Heading 3 in the Styles and Formatting window on the right. Click .
- Change the font color to red by clicking the A with a red underline.
- Click OK. Notice that all styles formatted with Heading 3 become red.
- Press Ctrl+A to select all. Press Ctrl+C to copy .
- Create a new HTML document in FrontPage or Dreamweaver (press Ctrl+N).
- Paste into the new document (press Ctrl+V).
- Switch to Code or Split view. Notice that the Word headings have become h1, h2 and h3 in HTML.
- Also notice that FrontPage adds a MsoNormal class and other styles which you would probably want to remove.
Session 2: Creating Styles
- If you are not continuing from Session 1, download the example files and link the stylesheet to the Web page.
- Create an inline style to format a single instance of a tag.
- Open your copy of
example.html using FrontPage or Dreamweaver.
- Click in the upper left cell of the table in Section 2.
- Switch to Code or Split view.
- Change
<table> to <table style="border: 5px solid blue;">
- Switch to Design or Split view. You should see that the table now has a thick blue border.
- Tip: You can use the Quick Tag Editor to make a change to a tag without switching views.
- Select part of the page such as a table.
- FrontPage
- Click , or press Ctrl+/, and click on a tag.
- Click , or press Ctrl+Q.
Or:
- Click , and make sure is checked.
- Right-click the
<table> tag in the Quick Tag Selector bar and click .
- Dreamweaver
- Right-click the
<table> tag in the bar between the document and the properties window.
- Click , or click , , or press Ctrl+T.
- Make changes to the tag and press Enter.
- Create an internal stylesheet and format
one or more existing tags within the document.
- FrontPage
- Click , Click the
h2 tag. Click Modify...
- Click , Select a font, size, style, color, effects, and/or character spacing. Click OK 3 times.
- Dreamweaver
- Click , ,
Or click the panel on the right, click the tab, and click the New CSS Style icon
.
- For the Selector Type, click the Tag radio button. Select the
h2 tag 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
h2 tags in the document are now formatted differently.
- Switch to Code or Split view. Find the CSS code that was inserted for you, in the
<head> section, within a <style> tag.
- Create an external stylesheet from a template.
- FrontPage
- Click , ,
Or click the arrow next to the white New icon in the upper left corner and click
- Click the tab. Select a stylesheet. Click OK.
- Dreamweaver
- Click , or press Ctrl-N. In the General tab, click .
- Select a stylesheet in the right side of the dialog box. Click Create.
- Click , . Type
styles1.css. Click Save.
- Create an external stylesheet
from scratch.
- FrontPage
- Click , , .
- Click the tab. Click . Click OK.
- Dreamweaver
- Click , or press Ctrl+N. Choose from the Category menu.
- Choose from the Basic page menu. Click Create.
- Add styles to the external stylesheet.
- Type a line of CSS code on the second line.
body { background-color: lightyellow; }
- FrontPage
- Click , . Click the body tag. Click Modify....
- Click , . Select a text color (or other formatting if desired). Click OK 3 times.
- Dreamweaver
- Click , , . Or click , ,
.
- For the Selector Type, click the Tag radio button. Select the h2 tag from the Tag menu. Select the radio button Define in: (New Style Sheet File). Click OK.
- Select a text color (or other formatting if desired). Click OK.
- Click , . Type
styles2.css. Click Save.
- Move a style from inline to internal
(so it can be used by more than one instance of a tag).
- Click in the upper left cell of the table.
- Switch to Code or Split view.
- Select
border: 5px solid blue;. Press Ctrl-X to cut. Select style="". Press Delete.
- Move the cursor to the right of the
<style> tag. Press Enter to add a new line. Press Ctrl-V to paste. Add a table style rule so the line looks like this:
table { border: 5px solid blue; }
- Save your changes. Test that the table still has a thick blue border.
- Move a style from internal to external
(so it can be used by more than one Web page).
- Switch to Code or Split view.
- Select
table { border: 5px solid blue; }. Press Ctrl-X to cut.
- Open
example.css. Position the cursor outside of any style rule. Press Ctrl-V to paste.
- Save your changes to both
example.html and example.css. Test that the table still has a thick blue border.
- In Dreamweaver, you can copy a page's internal styles to an external stylesheet. Click , , . Type a file name and click Save.
- Create a class selector to format groups of elements.
- FrontPage
- Click , Select from the List menu. Click New.
- Type a name for the selector (e.g.
large).
- Click , . Type in a different font size (e.g.
3em). Click OK 3 times.
- Dreamweaver
- Click , , .
Or click , ,
.
- 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.
- Type in a different font size (e.g.
3em). Click OK.
- Check that the
large style now appears in the Style menus.
- Apply this style class to any element to test.
- Create an ID selector to format a single element.
- FrontPage
- Click , Select from the List menu. Click New.
- Type a name for the selector, starting with a # symbol (e.g.
#huge).
- Click , . Type in a different font size (e.g.
250%). Click OK 3 times.
- Dreamweaver
- Click , , . Or click , ,
.
- 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.
- Type in a different font size (e.g.
250%). Click OK.
- Check that
huge now appears in the menu.
- Apply this style ID to any element to test.
- Format links and create rollover effects using pseudo-classes (a:link and a:hover).
- FrontPage
- Click , . Select from the Styles: list. Click Modify....
- Click , . Change the color (or other formatting if desired). Click OK three times.
- Dreamweaver
- Click , , . Or click , ,
.
- For the Selector Type, click the Advanced radio button. Select from the Selector: menu.
- Select the radio button Define in: This document only. Click OK.
- Change the color (or other formatting if desired). Click OK.
- Test in a browser by viewing unvisited links.
- Repeat for to format visited links.
- Repeat for to create a rollover effect. Test by moving the mouse over a link.
- Look at the W3Schools tutorial for more information and examples.
Session 3: Text Formatting
- If you are not continuing from Session 1, download the example files and link the stylesheet to the Web page.
- Use CSS lists.
- Add this code to the internal or external stylesheet and observe the effect on the bulleted list:
ul { list-style-type: circle; }
- Change the
list-style-type to disc, square, and none, and observe the effect for each.
- Add this code to use an image for the bullets:
ul { list-style-image: url(http://lib.colostate.edu/rds/webtips/css/tinysprout.gif); }
- Add this code and observe the effect on the numbered list:
ol { list-style-type: decimal; }
- Change the
list-style-type to upper-roman, lower-roman, upper-alpha and lower-alpha.
- Use CSS colors.
- In the table in Section 2, highlight each word and add an inline style to use a different background-color:
<td style="font-size: 20pt; background-color: red;">one</td>
- Try the following values:
| red |
darkred |
transparent |
| #00F |
#0F0 |
#FF0 |
| #F00 |
#FF0000 |
rgb(255,0,0) |
- Use contextual selectors to apply styles only to elements that occur within other elements.
- To make links only in the footer red, add this line of code in the internal or external stylesheet:
#footer a { color: red; }
- Group multiple selectors to reduce repetitive code.
- To underline both
h1 and h2 headings, add this line of code in the internal or external stylesheet:
h1, h2 { text-decoration: underline; }
- Add an alternate stylesheet to a Web document, e.g. a large font or text-only version of a page for accessibility.
- If you are not continuing from Session 2, download styles1.css and styles2.css or create from steps 4 and 5.
- Add these codes to the head section of
example.html:
<link href="styles1.css" rel="alternate stylesheet" type="text/css" title="fancy" />
<link href="styles2.css" rel="alternate stylesheet" type="text/css" title="simple" />
- Save, open in Firefox or Netscape, and test by clicking , , or .
- Add a print stylesheet to a Web document.
- Create a stylesheet named
print.css. Add this code to the head section of example.html:
<link rel=stylesheet href="print.css" type="text/css" media="print">
- To just override a few styles, use either of these methods:
- In the print stylesheet, include the regular styles using the statement
@import url(styles.css);
- Instead of using a separate print stylesheet, add the following statement at the end of the regular stylesheet:
@media print { }
Add the print styles between the curly braces
- Add comments to CSS code.
- In
example.css or within the <style> tag of example.html, add text between /* and */ marks.
/* This is a comment to explain CSS code to other people. It is ignored by all browsers. */
- If you are not continuing from Session 1, download the example files and link the stylesheet to the Web page.
- Use CSS dimension units.
- In the table in Section 2, highlight each word and add an inline style to use a different font-size:
<td style="font-size: 20pt;">one</td>
- Try 15pt, 20pt, 15px, 20px, 150%, 200%, 1.5em, 2em, xx-large.
- Hold down Ctrl and scroll the mouse wheel to change the default font size in both Firefox and Internet Explorer.
- Observe which font sizes change, and by how much. In Internet Explorer, pt and px sizes are fixed.
- Use margin, padding and border
around an image, table or other block element.
| |
|
|
↑↓ margin-top |
|
|
|
| |
|
|
↑↓ border-top |
|
|
|
| |
|
|
↑↓ padding-top |
|
|
|
margin-
left
↔ |
border-
left
↔ |
padding-
left
↔ |
object |
padding-
right
↔ |
border-
right
↔ |
margin-
right
↔ |
| |
|
|
↑↓ padding-bottom |
|
|
|
| |
|
|
↑↓ border-bottom |
|
|
|
| |
|
|
↑↓ margin-bottom |
|
|
|
- Add lines of code to the
img#csu rule in the stylesheet to change the padding, margin and border of the CSU logo.
padding: 2px;
border-top: 3px
dotted red;
border-right: 5px dashed blue;
border-bottom: 3px double #F0F;
border-left: 5px groove #0FF;
margin: 2px 5px 2px 0px; /* top right bottom left */
- Add lines of code to the
img#csulibraries rule in the stylesheet to change the padding, margin and border of the CSU Libraries logo.
padding: 2px; padding-left: 5px;
border: 2px solid #6c9; border-right-color: #042; border-bottom-color: #042;
margin: 0;
- Make an image float to the left or right of some content.
- Position the cursor at the beginning of the Section 1 paragraph, to the left of "Curabitur".
- Insert the CSU logo image. Only the first line of text will be moved to the right.
- Edit the image tag, add an inline
float style, and see the image move to the left or right of the paragraph.
<img style="float: left" border="0" src="csu.gif" width="109" height="60">
<img style="float: right" border="0" src="csu.gif" width="109" height="60">
- Create a skip link for screen readers, but hide it from regular browsers.
- Before the navigation bar, include a link to an anchor named content. It should use the skip class.
<a href="#content" class="skip">Skip Navigation</a>
- After the navigation bar, include the anchor named content.
<a name="content" id="content"></a>
- In the internal or external stylesheet, define a skip class that positions the link above the visible page.
.skip { position: absolute; top: -500px; }
- Make navigation buttons using a list of links.
- Set the
list-style-type of the list to none.
ul#nav { list-style-type: none; }
- If the buttons will be in a row, set the
display of list items to inline.
ul#nav li { display: inline; }
- Remove the underline from the list item links.
ul#nav a { text-decoration: none; }
- Add a
background-color to the links.
ul#nav a { background-color: #ccc; }
- Add
padding to the links.
ul#nav a { padding: 1px 5px; }
- Add a
border to the links.
ul#nav a { border: 3px groove #999; }
- Change the
color, background-color and/or background-image of the list item links when the user moves the mouse over them.
ul#nav a:hover { color: #f99; background-color: #ffc; }
- Add a
background-image to the links instead of background-color.
ul#nav a { background-image: url(http://lib.colostate.edu/lts/tips/css/gradient.jpg); }
ul#nav a:hover { background-image: url(http://lib.colostate.edu/lts/tips/css/gradient2.jpg); }
- Create a centered page.
- For a variable-width page, the body style should have equal but nonzero left and right margins (pixels or percent).
body { margin: 0 10%; }
- For a fixed-width page, enclose the entire page content within a centered div, which contains a document div with a fixed width (e.g.
780px or 48em).
- In the stylesheet, add this line:
div#doc { width: 48em; min-width: 780px; max-width: 100%; }
- In the Web page, enclose the page content in two div tags:
<body>
<div align="center">
<div id="doc">
[page content goes here...]
</div>
</div>
</body>
- Create a two-column page with navigation and content.
- Create a list
<ul id="nav"> that contains a list of navigation links. (This could be enclosed in a div tag for consistency.)
- Add a style in the stylesheet for
ul#nav that positions the list on the left side of the page, below the header.
ul#nav { position: absolute;
top: 70px;
left: 10%; }
- Create a section
<div id="body"> that contains the body column of the document.
- Add a style in the stylesheet for
div#body that positions the body column to the right of the navigation column.
div#body { margin-left: 160px;
}