Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Wednesday, April 8, 2009

Cascading Style Sheats (CSS) for Beginners - Part 3

Using CSS for Text Formatting

For the purposes of this part of the tutorial I am just going to assume you have absorbed the basic rules and terminology from the previous part - if you haven't, go back and review it now!

While CSS can control just about every aspect of the style and layout of your pages we are going to concentrate for now on the text only as this is the most useful and easy to apply aspect.

We are all accustomed to using different font, in various sizes, colors and combinations when designing our pages. The difficulty in pages where CSS are not used is that making a small change to, say the size of Heading 3 or the colour of visited links can mean having to go through each page individually. Where linked style sheets are used one page - the style sheet - is all that needs to be changed.

Exercise

1. Create a new web in FP. When it is open click File>New Page. Choose the 'Style Sheets' tab and then choose 'Normal Style Sheet'.

2. Paste the style sheet below into that page and save the file as 'site.css'.

P {font-family: Arial, Helvetica, sans-serif; font-weight: normal; color: blue;}

H1 {font-family: Verdana, Helvetica, sans-serif; font-weight: bold; color: green;}

H2 {font-family: Verdana, Helvetica, sans-serif; font-weight: bold; color: red;}

H3 {font-family: Verdana, Helvetica, sans-serif; font-weight: bold; color: #CC0099;}

A:link {text-decoration: underline; color: #ff3399;}

A:hover {text-decoration: none; color: #990000;}

A:visited {text-decoration: underline; color: #3399FF;}

3. Now insert the following into the section of a normal page in an FP site.

<link rel="STYLESHEET" type="text/css" href="site.css">

This is the method used to link a HTML file to a style sheet.

4. In normal view in this document type three headings with short paragraphs below them - any old rubbish will do! Highlight the headings in turn and select 'Heading 1' from the drop down menu for the first, 'Heading 2' for the second etc. Now create a link or two in your paragraphs. Save this page.

5. Pretty colourful eh? Now lets have a look at what is happening. This style sheet defines seven rules, one each for the HTML tags

,

,

,

and three for three separate classes of : , and

You can have a look at the stylesheet and the sample page and copy them to practice with if you like.

Each of the rules defines - using selectors and definitions - the font family, font weight and font style in which any text surrounded by those tags should be rendered on the page.

Play with this a bit, changing things in the style sheet to see the effect it has on your page.

Inheritance

You may have noticed that no font family or font weight is specified for any of the classes of a. This is because the links are appearing within a paragraph and anything that is specified for the

or paragraph tags will apply to them automatically. This is referred to as 'inheritance' and the

tag in this case is referred to as the 'parent tag'.

The only selectors that need to be defined in this case are those which either do not appear in the parent tag or which differ from the parent tag.

Linked or Embedded?

In the exercise above the style sheet is a linked one. You could just as easily paste exactly the same sheet between tags in the head of a page. However then it would only apply to that page and would need to be individually included in each page to which it applied. It would also need to be changed on each page if you wanted to vary the style of any tag. This means you would miss out on the real power of CSS.

It is best practice to use a linked style sheet for your web saving embedded ones for variations that you only want included in one page or in a group of pages. Remember the cascade: an embedded sheet will override a linked one.

Selectors for Text Style

We are going to look in detail at each of the selectors commonly used for text, how they can be applied and at the browser compatibility issues that may affect them. During this the rest of this tutorial you might like experiment with some of these things by making alterations to the CSS file you created earlier and trying them out on the page.

A Note About Browser Support...

As a general rule you can assume that versions of NN prior to version 4 and of IE prior to version 4 have no support for CSS. Opera, from version 3.6, fully supports all these properties. Things are further complicated by the fact that there are different versions of each browser for Windows and Mac and these can sometimes vary in their support of CSS. That said you will see that, provided you take care, all these properties can be applied in a browser friendly way.

Ok lets go with the various selectors!

1. font-size

This is probably the property that causes most angst so we will take quite bit of time with it. You will note that I have not actually included it in any of the rules in the style sheet above. This is because I want you to experiment - add in bits to that style sheet as we go through this section and see how they behave.

Declarations: Font size can be defined in a number of different ways, which broadly divide into three methods.

Fixed Measurements - These could be:
- point size (pt)
- pixels (px)
- millimeters, centimeters or inches (mm, cm, in)

eg

h1 {font-size: 15px;}
h2 {font-size: 12pt;}

Using a fixed size had the advantage that you are reasonably sure how your text will appear to the end user. Probably the best choice from among these is pixels. Inches and centimeters etc are not at all reliable and should be avoided.

All suffer the same problems - there is varying cross browsers support and, more importantly, they take away the users choice in how text is rendered. We will see later how other methods can give the user increased choice.

Exercise: Try adding 'font-size:12 pt' to the p rule in the sample style sheet to see its effect. You can also add various sizes to each of the header rules.


Words - xx-small, x-small, small, medium, large, x-large, xx-large.

These are very dodgy and best avoided altogether! In most version of NN anything below small is unreadable. In any case they are a fairly blunt tool.

Relative Sizes - You can use relative sizes as either percentages or ems.

Percentages of what I hear you ask? Well there are two option here:

1. Define a base size:
So for example you could define text size using the following rule for

body {text-size:12 pt}

And these rules for heading 1 and paragraphs respectively

h1 {text-size: 150%}
p {text size: 90%}

In this case heading 1 text would be rendered larger than 12 points, paragraph text smaller.

2. Allow the user's browser setting to be the base size:
In this case you simply use percentages in your rules without defining a measured size anywhere. This is a very elegant solution as it means that if a short sighted user has their browser set to render large text, you will acknowledge this just as you will the preference of someone who has, by choice set their browser default to small.

About 'ems'
Ems are a measurement of the letter capital 'M' in the font size chosen by the user, so again using ems accommodates the users choice.

You can express the proportional size you wish the font to appear in as a fraction of ems - eg 0.5 ems or 1.2 ems. Ems take some getting used to but can allow very fine gradation of sizes.

Exercise: Try adding percentage values to some of the rules in the style sheet.

Note the following important points:

  • Size is inherited. So for example if you set 'p' text to 80% and also specify 80% in the 'a' rule also your link text will be smaller than text in the surrounding paragraph. It also means it is important that tags are closed, which unfortunately FrontPage sometimes neglects to do. So if you had a series of paragraphs, using p text at 80%, and the

    tags are missing, each paragraph will be 80% of the previous one until the text pretty quickly becomes unreadable.
  • The default browser settings, which many people never change, are different in NN and IE: IE default is medium, NN default is small. Thus you many be surprised to see that your pages look different when you view them in different browsers. Do not panic about this, you can never have absolute control over the way your pages are seen. The point is that by using percentages of what the viewer is accustomed to seeing all users should be comfortable with the way your page is rendered.
  • If undefined: Any size applied to a parent tag will apply (ie the parent style will be inherited).
    If you do not define size at all in your style sheet then the user's browser default sizes will apply. This may seem desirable and many purists would say it is as the web is intended to be. But the fact is that people do not often choose to configure their browser settings and they are accustomed to looking at pages where text size has been defined in some way. Using percentages as least acknowledges the efforts of those who, for one reason or another have chosen to use custom settings.
  • Browser Support: Patchy if measurements are used, best for pixels; poor where words are used. In general pretty good in the case of percentages.

2. font-family

Declarations: Any font family name can be used and any number of alternatives suggested. It is good practice to have as a final alternative a generic family, eg serif or sans-serif.

Example: p {font-family: Americana, Verdana, Helvetica, sans-serif;}

If undefined: Any style applied to a parent tag will apply (ie the style will be inherited). If none is available then the users own browser default setting will apply.

Browser Support: Fully supported by NN 4.0+ and IE 4.0+

3. font-style

Declarations: normal, italic. Example:

h3 { font-style:italic}

If undefined: Any style applied to a parent tag will apply (ie the parent style will be inherited). Otherwise 'normal' is assumed.

Browser Support: Fully supported by NN 4.0+ and IE 4.0+

4. font-weight

Declarations: There are two ways in which font weight can be defined:

a) normal, bold, bolder, lighter
bolder and lighter must be applied with reference to a parent with the property 'bold' applied.
b) Using numeric values: 100, 200, 300, …..800, 900. (Normal text has a value of 400).
These values can only be used where the font has a range of weight values built in. Relatively few fonts have - arial and veranda do.

Example:

p { font-weight:bold }
a.link {font-weight:bolder }

Where supported this would result in link text in a paragraph being bolder than the main paragraph text.

If undefined: Any style applied to a parent tag will apply (ie the parent style will be inherited). Otherwise 'normal' is assumed.

Browser Support: Both 4.0+ browsers fully support 'normal' and 'bold'. After that it gets a little complicated!

  • Netscape Navigator prior to version 4.7 does not support 'bolder' and 'lighter'. 100-500 is all drawn as normal. 600-900 is all drawn as bold.
  • Internet Explorer for Mac prior to 5.0 will render 100-400 as normal, 500-900 as all bold.
  • Internet Explorer for Windows prior to version 5.5 will render 100-500 as normal, 600-900 as increasingly heavy.

5. text-decoration

Declarations: none, underline, overline, line-through, blink

Example:

a:link { text-decoration: underline;}
a:hover { text-decoration: none;}

which would cause the underline on the link to disappear when the link is moused over.

If undefined: In most cases none is assumed; in the case of the selector a (which applies to everything within tags, ie links) underline is assumed.

Note that this value is not inherited.

Browser Support: These features are pretty well supported with the exceptions that IE does not support 'blink' and NN prior to 4.7 does not support 'overline'. Also note that 'a:hover' is not supported by NN, but using it will not cause a problem in that browser.

6. text-transform

Declarations: none, capitalize, upper-case, lower-case

What they do:
1. Capitalize capitalizes first character of each word
2. Uppercase capitalizes all characters of each word
3. Lowercase uses small letters for all characters of each word
4. None is self explanatory!

Example: h1 { text-transform:capitalize }
This Will Cause Each Word In Heading 1 To Be Capitalized

If undefined: Any style applied to a parent tag will apply (ie the parent style will be inherited). Otherwise 'none' is assumed.

Browser Support: Fully supported by NN 4.0+ and IE 4.0+

Cascading Style Sheats (CSS) for Beginners - Part 2

Constructing a Style Sheet

Style Sheets consist of code which browsers use to format the HTML in your document and present it to the viewer. While CSS is not complicated, as with all code there are a few basic terms to learn and rules to follow.

Terminology
New terminology is always confusing - but getting it right is vital. This next section introduces the terminology associated with CSS. If you get a bit confused at first please persist - get this lot into your head and the rest of CSS is a snip! You will be introduced to the following terms, as they apply to CSS:

  • Rule
  • Selector and Declaration
  • Property and Value
  • Class and ID

A simple Style Sheet

Let's look at a very simple style sheet, for this example an embedded Style Sheet - one that would be placed in the head of a document - which controls the appearance of Heading 1, or the tag

.

We will use this example for much of this section so have a careful look at it.



The opening and closing tags - simply tell the browser that this is an embedded style sheet.

Exercise
Create a new page in FrontPage now. Paste the style sheet above just over the tag of your page in HTML view (you may need to paste it into Notepad first to retain the formatting).

Now type a few words into the page in normal view and highlight them. From the drop down formatting menu choose Heading 1.

You should have a large, bold, red, statement.

A Few Words

Now, on to a closer look at how that was acheived.

Rules

A style sheet consists of a series of rules that will be interpreted by the browser to display the content of your page.

This particular Style Sheet has just a single rule which tells the browser how any text surrounded by the

tags should appear.

Selectors and Declarations

Each rule in a style sheet must have two components - a selector and a declaration.

H1, or the tag whose style is defined, is referred to as a selector. Any HTML tag can be a selector.

Every thing within the curly braces {} is referred to as the declaration.

Look very carefully at how it is written: the selector, followed by the declaration in curly braces:

H1 {font-family: Verdana, Helvetica, sans-serif; color: red; font-weight: bold;}

Properties and Values

The declaration in turn consists of a series of properties and their associated values.

In our simple style sheet three properties for H1 and their associated values are defined. The properties and their values are as below:

Property Value

font-family

Verdana, Helvetica, sans-serif

color red
font-weight bold


Again look very carefully at how the property and value are written: the property is always followed by a colon and the value or values by a semicolon.

H1 {font-family: Verdana, Helvetica, sans-serif; color: red; font-weight: bold;}

In most cases a property will have only one value - eg color: red, - but in the case of font-family it is common practice to have a series of values. In our example the style sheet tells the browser to look first for Verdana, if that is not there look for Helvetica, if that fails use any san serif font. Where several alternate fonts are specified like this a comma separates them.

font-family: Verdana, Helvetica, sans-serif;


Class and ID

As mentioned above any HTML tag can be used as a selector. But suppose you are looking for even more than that - for example suppose instead of defining one rule for H1 you would like to have 2 different H1 styles from which you could pick and choose at will. This is where Class comes in and it is one of the most powerful and important aspects of CSS.

In our example let's say we want to specify two different types of H1 style. We can alter our style sheet to read:



In this case 'two' is a class of H1, or an alternative style rule for H1.

Now we can choose from the two Heading 1 styles in our document by making a small change to the

tag.

If we want to use the first style we would simply select Heading 1 from the formatting menu to get this code:

This is a red, bold, headline.

and this effect:

This is a red, bold, headline

If we want to use the second style we can start by applying Heading 1 from the formatting menu, the switch to html view and make a small alteration to the code for that heading:

This is what we would find:

This is a green, normal, headline.

Now we simply need to apply the second style to this particular instance of the H1 tag and we do this by adding 'class=two' to the tag.

This is a green, normal, headline.

This is a green, normal, headline

If you take a look at the code on this page you will see that style sheet in action.

Naming Classes

You can use any title you like for your class: one, two, first, second, red, green, monkey, giraffe - literally anything. As long as you use the same title in your HTML tag it will be recognized.

Once again note carefully the way it is written.

In the Style Sheet the class is specified immediately after its selector and separated from it by a full stop (period). In the body tag you simply insert 'class=classname' after the name of the tag.

You can use the different classes as often and as much as you like in your documents. If you had a mind to there is no reason why you could not have 20 classes of H1, or of any other tag. This is where class differs from ID.

Using ID

Unlike classes an ID can only be used once. So suppose you are happy with your big, bold, red headline but on just one occasion in a page you want it to be different. In this case you might choose to use an ID. Your style sheet would now read:



Now on the one occasion where you wished to use the green headline you would alter the

tag to read:

This is a green, normal, headline.



Again note the format: ID is specified by putting '#' between the selector and the name of the chosen ID. Again you can give the ID any title you like.

Which to choose? Class or ID?
The primary difference between class and ID is that the former can be used as often as you like - you can only use ID once in a page.

So if you are creating a variation in style that you are going to use again and again then choose class, if you are just creating a one off variation then choose ID. It might seem that there is little advantage in bothering with ID but it does save a lot of fiddling with tags if you are only going to use the variation once. However classes are much more important, more useful and more widely used.

Important Note
You can only use Class and ID in embedded and linked sheets - not in local styles.

Cascading Style Sheats (CSS) for Beginners - Part 1

Most of us are familiar with the use of HTML tags as a means of formatting our pages. For example we can use this tag:

Arial 12 point

to create Arial 12 point text, whether we apply it by hand or using FrontPage.

However individually applying styles to each element of your page in this way is laborious, prone to error and creates bulky code. There is another way: welcome to CSS, Cascading Style Sheets!!

What is a style sheet?

Print publications such as newspapers and magazines have long used style sheets - sets of rules governing page formatting, typefaces, nature of headlines etc - to give them a consistent and coherent appearance. Similarly in HTML documents style sheets can control text formatting, colors, image placement and a myriad of other features that determine how our pages look.

Why Bother with them?

1) Consistency: Style sheets are easy to construct and can be readily applied to all the pages in your site, ensuring continuity of style throughout.

2) Convenience: If you have used style sheets to format your pages making changes to the format of your entire site, or to individual pages, can be as simple as making a small alteration to your style sheets or the way in which they apply.

3) Consideration: Style sheet rules can be applied in a manner that allows viewers to see your site in a way that suits them. We will look at this in more detail later.

4) Compulsion!: the use of inline tags to define style – such as the tag example given above – will in the future be deprecated and we will be required to use style sheets to format our pages, so we may as well start now.

What about Browser Compatibility?

Many people are put off using style sheets because they have heard or read about poor cross browser support for them. While it is true that there are differences in the degree and nature of support given to CSS, it is also true that very many features enjoy full cross browser support and in many other cases it is possible to find work rounds.

As with all other of web design the trick is in knowing what browsers will do with your code and then applying it accordingly. Throughout this series of tutorials we will look at the issue of browser compatibility wherever it is a consideration.

CSS Basics

For the moment let's forget about how style sheets are structured and just have a look at how they are applied.

There are three different ways to apply style sheets:

1. Inline Styles
You can apply a style to any individual html tag in a page. This is something you already do to some degree all the time. For example, you can easily change the text colour of an individual paragraph in your page. In so doing you are applying a style to that portion of the page – or an ‘Inline Style’.

2. Embedded Style Sheets
An embedded style sheet controls the formatting of an individual page. The Style Sheet is placed between the and tags.

If you have used FrontPages facility to apply a ‘rollover effect’ to text links you will have placed an embedded style sheet in the head of your document which will look something like this:

This causes your hyperlinks, when in the hover state, to appear red and bold.

In the case of CSS the effect it the same but formatting is a little different and the same style sheet would look like this:

3. Linked Style Sheets
This is where the real power of style sheets is found. You can create a stand alone set of instructions for how things should appear on your page – a Style Sheet - and then link it to as many pages of your site as you wish.

This single style sheet will control the appearance of all the pages and making a change to it will affect all pages at once.

What about the ‘Cascading’ bit?

This is a term that seems to cause an immense amount of confusion but really need not do so. The thing to remember is that you can use each of the three different types of style sheet, as defined above, in the same web site. Which one will be applied in any situation is governed by a set of priorities:

- In line styles take precedence
- Embedded styles come next
- Where neither an inline not an embedded style is used
the linked style will apply

This is referred to as the ‘cascade’.

Let's look at it another way. Suppose you have a web site that includes a style sheet to which all pages are linked, giving it a nice professional and consistent appearance. But you have a single page that you want to appear radically different from the others. You can simply place an embedded style sheet in the head of this page and that style sheet will override the linked one on that page.

Now you look at this page and it is fine – except for one small section that, again, you would like to appear in a different format. Simply apply an inline style and this will, in turn, over ride the embedded one. Hurrah! Your style sheets are cascading!!