Cascading Style Sheets

Cascading style sheets (CSS) are used to:

Students should be aware that the area of CSS covers far more than the contents of these notes. The notes only cover some of the more immediately useful aspects of CSS.

How Do Style Sheets Work?

A style represents a set of instructions that recommend to a web browser how to draw a webpage. Styles do not force a browser to display a webpage in a particular way. They merely recommend to the browser how the webpage should be displayed.


Defining Styles

There are three ways of defining a style. It can be placed:

  1. directly into the <head> element of a HTML document;
  2. directly into any HTML element (e.g. the <body> or <p> element);
  3. into an external .css file.

Styles are dealth with slightly differently when declared as external, in the <head> element or as part of a element.

Style scope cascading refers to the way in which the scope of a style cascades through a HTML document. The scope of a style depends on where the style is declared. The scope cascading rules are:

Style Syntax

A style is made up by combining one or more properties. Each property has a property name followed by a colon and then the value for that property.

Some properties take a number of values. In such cases, the values are separated by a comma or a space. We shall always use a space, as this is normal practice in industry.

As with HTML, white space can be used to make a style sheet easier to read and write.

As with HTML, CSS is NOT case sensitive. We shall always use lower case letters, as this is normal practice in industry.

CSS Properties

All properties have the form of a property name followed by one or more values.

Examples of property categories include:

Text Style Properties

The text style properties affect how text appears on a webpage. They include:

(a)    color

This property sets the text colour of an element. Color is set using either a keyword or a hexidecimal number. Keywords include the obvious colours, such as:

Hexidecimal numbers must be preceeded by an # and be six digits in length. The six digits represent the amount of red (first two digits), green (middle two digits) and blue (last two digits). For example:

(b)    font-family

The font-family property helps to ensure that text looks the way it was intended to on various platforms. This property specifies a hierarchical list of preferred fonts that a browser should use to draw an element. The browser will use the first font in the list that is installed on the system it is running on.

Because none of the desired fonts may be on the system a browser is running on, CSS introduces the idea of a font family. A font family is a generic name for a type of font. The specified font families are:

The font-family property is a list of font names and font family names. Each item in the list is separated by a comma.

Font names should be capitalized (for example, Ariel, not ariel). When the name of a font is more than one word, it should be quoted, as in "Times New Roman".

(c)   font-size

The font-size property specifies how large text appears on a webpage. The font-size property can specify the size of a text element in several units:

In addition, font-size can be specified using both relative and absolute keywords. The following are the relative keywords:

The seven absolute keywords (that match the seven HTML sizes) are:

(d)   font-weight

The weight of a font is how bold it is. The font-weight property affects how heavily text is drawn on a webpage. Font-weight can take two kinds of values:

keywords

  • normal
  • bold
  • bolder   
  • lighter  

numerical values

  • 100
  • 200
  • 300
  • 400
  • 500
  • 600
  • 700
  • 800
  • 900

(e)     font-style

The font-style property specifies the style of the font that is to be applied to an element. Font-style can take the value:

(f)    text-decoration

The text-decoration property specifies a number of effects. Text-decoration can take one or more of the values:

(g)    text-transform

text-transform specifies if text should be capitalised. Text-transform can take the value:

(h)    font-variant

The font-variant property specifies if text should appear in small capitals. Font-variant can take the value:

Text Layout Properties

Text layout properties affect how text is laid out on a webpage. These properties are specific to text, providing control over things like line height and letter spacing. The text layout properties are:

(a)    letter-spacing

The letter-spacing property gives you control over the space (kern) between characters. Letter-spacing can be specified in any length unit. Negative letter-spacing can be used to make characters appear more closely together.

(b)   word-spacing

word-spacing gives control over the space between words (track) in text. Word-spacing can be specified in any length unit. Negative word-spacing can be used to group words closer together.

(c)    line-height

The line-height property sets the distance between adjacent lines.

line-height can be specified in a number of ways:

(d)   vertical-align

The vertical-align property gives control over how elements align vertically within an element. Vertical-align can be specified by either:

Background Properties

Background properties affect how the background of an element appears. Any element can have a background image or color. Background images can repeat vertically, horizontally, both (the traditional tiling effect of background images in the <body> of HTML webpages) or not at all (enabling easy "watermark" effects). A background image can either scroll or remain fixed while the webpage scrolls.

The background properties are:

(a)    background-color

background-color specifies the colour of the background of an element. Any element can be given a background colour. background-color is specified as either a color value, or as transparent.

(b)   background-image

Any element can have a background image. The background image can be used in more ways than the simple tiled backgrounds of traditional webpages.

(c)    background-attachment

With traditional background images in the body of HTML documents, when the webpage is scrolled, the image scrolls too. Using styles, we can specify that the background image should not scroll, but rather remain fixed as the webpage scrolls.

background-attachment can take one of two values:

(d)   background-repeat

With the traditional background image associated with the body of a webpage, where the background image is smaller than the width and/or height of the webpage, browsers "tile" the image both horizontally and vertically.

Using styles we can specify how a browser treats background images when the width and height of the image is less than that of the element.

background-repeat can take the following values:

(e)    background-position

With a traditional HTML based background image, the image is placed in the top left hand corner of the webpage, and tiles from there. Using styles, we can specify where a background image is placed using the background-position property. If the image repeats (horizontally, vertically or both) then it repeats from this location. If there is a single image, it is placed using the background-position property. Background positions may be specified using:

  • Pairs of percenelemente values

When a position is specified using a pair of percenelemente values (for example, background-position:0% 0%) the first value relates to the position of the top of the image, while the second relates to the position of the left of the image.

  • Pairs of length values

When a position is specified using a pair of length values, (for example background-position 20px 20px) the first value specifies the position of the top of the image relative to the element, while the second value specifies the position of the left of the image relative to the element.

  • Individual keywords

Rather than relying on length values, or percenelemente values, keyword values can be used. Keywords have a similar effect to percent.

The keywords which can be used to specify position are:

  • top
  • left
  • center
  • right
  • bottom
  • Pairs of keywords

For example, 'top center' and 'center top' mean the same as '50% 0%'.

(f)     background

background is a shorthand property that lets you set any or all background properties at once.

The background property takes exactly the same types of values as the properties described in this section. To specify more than one background property using the background shorthand, the various background values are separated simply by spaces.

For example, the background property

background:url(image.jpeg) fixed no-repeat top center

is the equivalent of

background-image:url(image.jpeg);
background-attachment:fixed;
background-repeat:no-repeat;
background-position:top center;

Border Properties

Any element may have a border, of varying widths, colours and styles. Elements can have different borders set to a different style, width and colour.

The border properties are:

Each border property always has three values associated with it.

To define the same border properties for each side of the element, use the border property.

{
   border:thin red solid;
}

More than one border property can be set in a style. For example, to give the top edge of an element a thin, red solid border, and the bottom edge a thick black dotted border we'd use the following:

{
   border-top:thin  red   solid;
   border-bottom:thick black dotted;
}

Margin Properties

Any element can have a margin. A margin is the space between an element and the elements to its top, left, bottom and right. The margin properties are:

The margin properties set the size of the top, left, bottom, right and all margins respectively.

Margins can be specified as either:

Padding Properties

Any element can have padding. Padding is the space between the edge of an element (its border) and its contents. There can be padding to the left, top, bottom and right of any element. The padding properties are:

Padding sizes can be specified in the same way as margins.

Box-Sizing

When measuring content element sizes, we should use set the css box-sizing property to border-box for all elements using the css style below. This will cause the margin and padding to be included in the elements size. We should normally include this as the first line of our css code.

*   
{
      box-sizing: border-box;  
}

User Interface Properties

User interface properties allow the:

Printing

Printing styles allow us to control how a webpage should appear when it is being printed. The two most useful printing styles are:

The only useful property of both the above printing styles is the keyword:

As shown earlier in these notes, the usual way of using webpage breaking is to set up a class style and to attach it as needed. For example:

.pageBreak
{ 
   page-break-before:always
}

To attach this style to a particular instance of (for example) a <h1> element, use the code below:

<h1 class = 'pageBreak'>

To attach the same style to a particular instance of a <p> element, use the code below:

<p class = 'pageBreak'>

Media

There are many more ways to access a webpage than simply via computer and monitor. Each different media has its own characteristics. It might be monochrome, it might be limited in size or it might read webpage content aloud. Styles can be made to behave differently to different media.

 
<div align="center"><a href="../../versionC/index.html" title = "DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>