JPMK12.net

Live Chat | Forum
Home | News | Contact Us | Live Chat | Support Forum | Tutorials
Blog | Live Chat | News | Support Forum | Web Links | Site Ring
Downloads | FAQ | Reviews | Tips and Tricks | Tutorials | Web Links
Web Development | Shell Tutorials | Windows Tutorials | Misc.
subglobal5 link | subglobal5 link | subglobal5 link | subglobal5 link | subglobal5 link | subglobal5 link | subglobal5 link
subglobal6 link | subglobal6 link | subglobal6 link | subglobal6 link | subglobal6 link | subglobal6 link | subglobal6 link
subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link
subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link

Tutorials - Basic CSS

CSS Tutorial

Back to the tutorials Home Page

This tutorial will help you learn to create styles for your web pages. You will be able to use style sheets to create uniform looking webpages. Style sheets allow you to customize almost every part of you webpage with a constant look. We will examine how to change page styles, set background and a lot more.

Getting started

The first thing we should do it set the background and text color of our web page. You can do this by using the STYLE element to set style properties for the document's tags:

<style type="text/css"> body { color: black; background: white; } </style>

The style element is placed within the document head. Here is a template HTML file showing where the above style element fits. You can copy and paste this into your editor as a convenient way to experiment with CSS style sheets:

<html> <head> <title> replace with your document's title </title> <style type="text/css"> body { color: black; background: white; } </style> </head> <body>Body of Site </body> </html>

The code between the <style> and </style> is written in a special way. Each rule starts with a tag name followed by a list of style properties bracketed by { and }. In this example, the rule matches the body tag. As you will see, the body tag provides the basis for setting the overall look and feel of your Web page.

Each style property starts with the property's name, then a colon and lastly the value for this property. When there is more than one style property in the list, you need to use a semicolon between each of them to seperate one property from the next. In this example, there are two properties - "color" which sets the color of the text, and "background" which sets the color of the page background. I always add a semicolon after every property to keep it uniform.

When choosing colors, make sure you pick colors that are easy on the eyes and go together well. You can learn more about how to create colors at my Color tutorial. You can also use our color generator to get hex codes for colors.

Note that the style element must be placed in the document's head along with the title element. It shouldn't be placed within the body.

You can also use an external style sheet to style an entire website with one sheet. The code below allows you to reference an external style sheet.

<link type="text/css" rel="stylesheet" href="style.css">

The link tag should be placed within the <head> ... </head> element. Here is an HTML file with a link to an external style sheet:

<html> <head> <title>Page Title</title> <link type="text/css" rel="stylesheet" href="style.css"> </head> <body> Site Body </body> </html>

The link element's rel attribute must be set to the value "stylesheet" to allow the browser to recognize that the href attribute gives the Web address (URL) for your style sheet. A simple stylesheet file might look like the following:

		/* style.css - a simple style sheet */  
		     body {    
		          margin-left: 10%; margin-right: 10%;   
		          color: black; background: white;  
				  }  

You can use internal and external styles in a single html page. If you place the link element before the style element, you can use the latter to override the style settings in the linked style sheet.

Page Margins

Web pages look a lot nicer with bigger margins. You can set the left and right margins with the "margin-left" and "margin-right" properties, e.g.

		<style type="text/css">  
		  body   { 
		   margin-left: 10%; 
          margin-right: 10%; } 
		   </style>  

This sets both margins to 10% of the window width, and the margins will scale when you resize the browser window.

Indentions

To make headings a little more distinctive, you can make them start within the margin set for the body, e.g.

  <style type="text/css">  
 body { 
  margin-left: 10%; 
  margin-right: 10%; 
  }   
 
 h1 { 
  margin-left: -8%;
  }   
  
h2,h3,h4,h5,h6 { 
  margin-left: -4%; 
  }
  </style>  

This example has three style rules. One for the body, one for h1 (used for the most important headings) and one for the rest of the headings (h2, h3, h4, h5 and h6). The margins for the headings are additive to the margins for the body. Negative values are used to move the start of the headings to the left of the margin set for the body.

Controlling the white space above and below

Browsers do a pretty good job for the white space above and below headings and paragraphs etc. Two reasons for taking control of this yourself are: when you want a lot of white space before a particular heading or paragraph, or when you need precise control for the general spacings.

The "margin-top" property specifies the space above and the "margin-bottom" specifies the space below. To set these for all h2 headings you can write:

h2 { margin-top: 8em; margin-bottom: 3em; }

The em is a very useful unit as it scales with the size of the font. One em is the height of the font. By using em's you can preserve the general look of the Web page independently of the font size. This is much safer than alternatives such as pixels or points, which can cause problems for users who need large fonts to read the text.

Points are commonly used in word processing packages, e.g. 10pt text. Unfortunately the same point size is rendered differently on different browsers. What works fine for one browser will be illegible on another! Sticking with em's avoids these problems.

To specify the space above a particular heading, you should create a named style for the heading. You do this with the class attribute in the markup, e.g.

<h2 class="subsection">Getting started</h2>

The style rule is then written as:

h2.subsection { margin-top: 8em; margin-bottom: 3em; }

The rule starts with the tag name, a dot and then the value of the class attribute. Be careful to avoid placing a space before or after the dot. If you do the rule won't work. There are other ways to set the styles for a particular element but the class attribute is the most flexible.

When a heading is followed by a paragraph, the value for margin-bottom for the heading isn't added to the value for margin-top for the paragraph. Instead, the maximum of the two values is used for the spacing between the heading and paragraph. This subtlety applies to margin-top and margin-bottom regardless of which tags are involved.

First-line indent

Sometimes you may want to indent the first line of each paragraph. The following style rule emulates the traditional way paragraphs are rendered in novels:

p { text-indent: 2em; margin-top: 0; margin-bottom: 0; }

It indents the first line of each paragraph by 2 em's and suppresses the inter-paragraph spacing.

Font styles

The most common styles are to place text in italic or bold. Most browsers render the em tag in italic and the strong tag in bold. Let's assume you instead want em to appear in bold italic and strong in bold uppercase:

em { font-style: italic; font-weight: bold; } strong { text-transform: uppercase; font-weight: bold; }

If you feel so inclined, you can fold headings to lower case as follows:

h2 { text-transform: lowercase; }

Setting the font size

Most browsers use a larger font size for more important headings. If you override the default size, you run the risk of making the text too small to be legible, particularly if you use points. You are therefore recommended to specify font sizes in relative terms.

This example sets heading sizes in percentages relative to the size used for normal text:

h1 { font-size: 200%; } h2 { font-size: 150%; } h3 { font-size: 100%; }

Setting the font family

It is likely that your favorite font won't be available on all browsers. To get around this, you are allowed to list several fonts in preference order. There is a short list of generic font names which are guaranteed to be available, so you are recommended to end your list with one of these: serif, sans-serif, cursive, fantasy, or monospace, for instance:

body { font-family: Verdana, sans-serif; } h1,h2 { font-family: Garamond, "Times New Roman", serif; }

In this example, important headings would preferably be shown in Garamond, failing that in Times New Roman, and if that is unavailable in the browsers default serif font. Paragraph text would appear in Verdana or if that is unavailable in the browser's default sans-serif font.

The legibility of different fonts generally depends more on the height of lower case letters than on the font size itself. Fonts like Verdana are much more legible than ones like "Times New Roman" and are therefore recommended for paragraph text.

Avoid problems with fonts and margins

My first rule is to avoid text at the body level that isn't wrapped in a block level element such as p. For instance:

<h2>Spring in Wiltshire</h2> Blossom on the trees, bird song and the sound of lambs bleating in the fields.

The text following the heading runs the risk on some browsers of being rendered with the wrong font and margins. You are therefore advised to enclose all such text in a paragraph, e.g.

<h2>Heading 2</h2> <p>Paragraph 2 </p>

My second rule is to set the font family for pre elements, as some browsers forget to use a fixed pitch font when you set the font size or other properties for pre.

pre { font-family: monospace; }

My third rule is to set the font family on headings, p and ul elements if you intend to set borders or backgrounds on elements such as div. This is a work-around for a bug where the browser forgets to use the inherited font family, instead switching to the default font as set by the browser preferences.

h1,h2,h3,h4,h5,p,ul { font-family: sans-serif; }

Adding borders and backgrounds

You can easily add a border around a heading, list, paragraph or a group of these enclosed with a div element. For instance:

div.box { border: solid; border-width: thin; width: 100% }

Note that without the "width" property some browsers will place the right margin too far to the right. This can then be used with markup such as:

<div class="box"> The content within this DIV element will be enclosed in a box with a thin line around it. </div>

There are a limited choice of border types: dotted, dashed, solid, double, groove, ridge, inset and outset. The border-width property sets the width. Its values include thin, medium and thick as well as a specified width e.g. 0.1em. The border-color property allows you to set the color.

A nice effect is to paint the background of the box with a solid color or with a tiled image. To do this you use the background property. You can fill the box enclosing a div as follows:

div.color { background: rgb(204,204,255); padding: 0.5em; border: none; }

Without an explicit definition for border property some browsers will only paint the background color under each character. The padding property introduces some space between the edges of the colored region and the text it contains.

You can set different values for padding on the left, top, right and bottom sides with the padding-left, padding-top, padding-right and padding-bottom properties, e.g. padding-left: 1em.

Suppose you only want borders on some of the sides. You can control the border properties for each of the sides independently using the border-left, border-top, border-right and border-bottom family of properties together with the appropriate suffix: style, width or color, e.g.

p.changed { padding-left: 0.2em; border-left: solid; border-right: none; border-top: none; border-bottom: none; border-left-width: thin; border-color: red; }

which sets a red border down the left hand side only of any paragraph with the class "changed".

Setting Colors

Some examples for setting colors appeared in earlier sections. Here is a reminder:

body { color: black; background: white; } strong { color: red }

This sets the default to black text on a white background, but renders strong elements in red. There are 16 standard color name, which are explained just below. You can also use decimal values for red, green and blue, where each value appears in the range 0 to 255, e.g. rgb(255, 0, 0) is the same as red. You can also used hex color values which start with the '#' characted followed by six hexadecimal digits. A two-way converter is included below which allows you to convert from RGB to hex color values.

Setting Link Colors

You can use CSS to set the color for hypertext links, with a different color for links that you have yet to follow, ones you have followed, and the active color for when the link is being clicked. You can even set the color for when the mouse pointer is hovering over the link.

:link { color: rgb(0, 0, 153) } /* for unvisited links */ :visited { color: rgb(153, 0, 153) } /* for visited links */ a:active { color: rgb(255, 0, 102) } /* when link is clicked */ a:hover { color: rgb(0, 96, 255) } /* when mouse is over link */

Sometimes you may want to show hypertext links without them being underlined. You can do this by setting the text-decoration property to none, for example:

a.plain { text-decoration: none }

Which would suppress underlining for a link such as:

This is <a class="plain" href="what.html">not underlined</a>

Most people when they see underlined text on a Web page, will expect it to be part of a hypertext link. As a result, you are advised to leave underlining on for hypertext links. A similar argument applies to the link colors, most people will interpret underlined blue text as hypertext links. You are advised to leave link colors alone, except when the color of the background would otherwise make the text hard to read.

Setting the color and background

You can set the color using the BODY tag. The following example sets the background color to white and the text color to black:

<body bgcolor="white" text="black">

The BODY element should be placed before the visible content of the Web page, e.g. before the first heading. You can also control the color of hypertext links. There are three attributes for this:

  • link for unvisited links
  • vlink for visited links
  • alink for the color used when you click the link

Here is an example that sets all three:

<body bgcolor="white" text="black" link="navy" vlink="maroon" alink="red">

You can also get the browser to tile the page background with an image using the background attribute to specify the Web address for the image, e.g.

<body bgcolor="white" background="texture.jpeg" text="black" link="navy" vlink="maroon" alink="red">

It is a good idea to specify a background color using the bgcolor attribute in case the browser is unable to render the image. You should check that the colors you have chosen don't cause legibility problems. As an extreme case consider the following:

<body bgcolor="black">

Most browsers will render text in black by default. The end result is that the page will be shown with black text on a black background! Lots of people suffer from one form of color blindness or another, for example olive green may appear brown to some people.

A separate problem appears when you try to print the Web page. Many browsers will ignore the background color, but will obey the text color. Setting the text to white will often result in a blank page when printed, so the following is not recommended:

<body bgcolor="black" text="white">

You can also use the bgcolor attribute on table cells, e.g.

<table border="0" cellpadding="5"> <tr> <td bgcolor="yellow">colored table cell</td> </tr> </table>

Tables can be used for a variety of layout effects and have been widely exploited for this. In the future this role is likely to be supplanted by style sheets, which make it practical to achieve precise layout with less effort.

Setting the font, its size and color

The FONT tag can be used to select the font, to set its size and the color. This example just sets the color:

This sentence has a <font color="yellow">word</font> in yellow.

The face attribute is used to set the font. It takes a list of fonts in preference order, e.g.

<font face="Garamond, Times New Roman">some text ...</font>

The size attribute can be used to select the font size as a number from 1 to 6. If you place a - or + sign before the number it is interpreted as a relative value. Use size="+1" when you want to use the next larger font size and size="-1" when you want to use the next smaller font size, e.g.

<font size="+1" color="maroon" face="Garamond, Times New Roman">some text ...</font>

 


You should now know enough to make an html site with a style sheet. I always use external style sheets because they are easy to work with. I suggest you review the Colors Tutorial and the Tips and Tricks Section as well as teh rest of the html tutorials. A web designers work is never done. We are always learning more.

 

 

 

 

 

Site Map | Contact Us | ©2006 JPMK12.net