CSS

Lesson 1: What is CSS?

Maybe you already heard about CSS without really knowing what it is. In this lesson you will learn more about what CSS is and what it can do for you.

CSS is an acronym for Cascading Style Sheets.

What can I do with CSS?

CSS is a style language that defines layout of HTML documents. For example, CSS covers fonts, colours, margins, lines, height, width, background images, advanced positions and many other things. Just wait and see!

HTML can be (mis-)used to add layout to websites. But CSS offers more options and is more accurate and sophisticated. CSS is supported by all browsers today.

After only a few lessons of this tutorial you will be able to make your own style sheets using CSS to give your website a new great look.

What is the difference between CSS and HTML?

HTML is used to structure content. CSS is used for formatting structured content.

Okay, it sounds a bit technical and confusing. But please continue reading. It will all make sense to you soon.

Back in the good old days when Madonna was a virgin and a guy called Tim Berners Lee invented the World Wide Web, the language HTML was only used to add structure to text. An author could mark his text by stating "this is a headline" or "this is a paragraph" using HTML tags such as <h1> and <p>.

As the Web gained popularity, designers started looking for possibilities to add layout to online documents. To meet this demand, the browser producers (at that time Netscape and Microsoft) invented new HTML tags such as for example <font> which differed from the original HTML tags by defining layout - and not structure.

This also led to a situation where original structure tags such as <table> were increasingly being misused to layout pages instead of adding structure to text. Many new layout tags such as <blink> were only supported by one type of browser. "You need browser X to view this page" became a common disclaimer on web sites.

CSS was invented to remedy this situation by providing web designers with sophisticated layout opportunities supported by all browsers. At the same time, separation of the presentation style of documents from the content of documents, makes site maintenance a lot easier.

Which benefits will CSS give me?

CSS was a revolution in the world of web design. The concrete benefits of CSS include:

  • control layout of many documents from one single style sheet;
  • more precise control of layout;
  • apply different layout to different media-types (screen, print, etc.);
  • numerous advanced and sophisticated 
  • Lesson 1: What is CSS?

    Maybe you already heard about CSS without really knowing what it is. In this lesson you will learn more about what CSS is and what it can do for you.

    CSS is an acronym for Cascading Style Sheets.

    What can I do with CSS?

    CSS is a style language that defines layout of HTML documents. For example, CSS covers fonts, colours, margins, lines, height, width, background images, advanced positions and many other things. Just wait and see!

    HTML can be (mis-)used to add layout to websites. But CSS offers more options and is more accurate and sophisticated. CSS is supported by all browsers today.

    After only a few lessons of this tutorial you will be able to make your own style sheets using CSS to give your website a new great look.

    What is the difference between CSS and HTML?

    HTML is used to structure content. CSS is used for formatting structured content.

    Okay, it sounds a bit technical and confusing. But please continue reading. It will all make sense to you soon.

    Back in the good old days when Madonna was a virgin and a guy called Tim Berners Lee invented the World Wide Web, the language HTML was only used to add structure to text. An author could mark his text by stating "this is a headline" or "this is a paragraph" using HTML tags such as <h1> and <p>.

    As the Web gained popularity, designers started looking for possibilities to add layout to online documents. To meet this demand, the browser producers (at that time Netscape and Microsoft) invented new HTML tags such as for example <font> which differed from the original HTML tags by defining layout - and not structure.

    This also led to a situation where original structure tags such as <table> were increasingly being misused to layout pages instead of adding structure to text. Many new layout tags such as <blink> were only supported by one type of browser. "You need browser X to view this page" became a common disclaimer on web sites.

    CSS was invented to remedy this situation by providing web designers with sophisticated layout opportunities supported by all browsers. At the same time, separation of the presentation style of documents from the content of documents, makes site maintenance a lot easier.

    Which benefits will CSS give me?

    CSS was a revolution in the world of web design. The concrete benefits of CSS include:

    • control layout of many documents from one single style sheet;
    • more precise control of layout;
    • apply different layout to different media-types (screen, print, etc.);
    • numerous advanced and sophisticated techniques.                                                                                                                                                                   
       

    Lesson 3: Colors and backgrounds

    In this lesson you will learn how to apply colors and background colors to your websites. We will also look at advanced methods to position and control background images. The following CSS properties will be explained:

    Foreground color: the 'color' property

    The color property describes the foreground color of an element.

    For example, imagine that we want all headlines in a document to be dark red. The headlines are all marked with the HTML element <h1>. The code below sets the color of <h1> elements to red.

    
    h1 {
    	color: #ff0000;
    }
    

    Colors can be entered as hexadecimal values as in the example above (#ff0000), or you can use the names of the colors ("red") or rgb-values (rgb(255,0,0)).

    The 'background-color' property

    The background-color property describes the background color of elements.

    The element <body> contains all the content of an HTML document. Thus, to change the background color of an entire page, the background-color property should be applied to the <body> element.

    You can also apply background colors to other elements including headlines and text. In the example below, different background colors are applied to <body> and <h1> elements.

    
    body {
    	background-color: #FFCC66;
    }
    
    h1 {
    	color: #990000;
    	background-color: #FC9804;
    }
    

    Notice that we applied two properties to <h1> by dividing them by a semicolon.

    Background images [background-image]

    The CSS property background-image is used to insert a background image.

    As an example of a background image, we use the butterfly below. You can download the image so you can use it on your own computer (right click the image and choose "save image as"), or you can use another image as you see fit.

    Butterfly

    To insert the image of the butterfly as a background image for a web page, simply apply the background-image property to <body> and specify the location of the image.

    
    body {
    	background-color: #FFCC66;
    	background-image: url("butterfly.gif");
    }
    
    h1 {
    	color: #990000;
    	background-color: #FC9804;
    }
    

    NB: Notice how we specified the location of the image as url("butterfly.gif"). This means that the image is located in the same folder as the style sheet. You can also refer to images in other folders using url("../images/butterfly.gif") or even on the Internet indicating the full address of the file: url("http://www.html.net/butterfly.gif").

    Repeat background image [background-repeat]

    In the example above, did you notice that by default the butterfly was repeated both horizontally and vertically to cover the entire screen? The property background-repeat controls this behaviour.

    The table below outlines the four different values for background-repeat.

    Value Description Example
    background-repeat: repeat-x The image is repeated horizontally Show example
    background-repeat: repeat-y The image is repeated vertically Show example
    background-repeat: repeat The image is repeated both horizontally and vertically Show example
    background-repeat: no-repeat The image is not repeated Show example

    For example, to avoid repetition of a background image the code should look like this:

    
    body {
    	background-color: #FFCC66;
    	background-image: url("butterfly.gif");
    	background-repeat: no-repeat;
    }
    
    h1 {
    	color: #990000;
    	background-color: #FC9804;
    }
    

    Lock background image [background-attachment]

    The property background-attachment specifies whether a background picture is fixed or scrolls along with the containing element.

    A fixed background image will not move with the text when a reader is scrolling the page, whereas an unlocked background image will scroll along with the text of the web page.

    The table below outlines the two different values for background-attachment. Click on the examples to see the difference between scroll and fixed.

    Value Description Example
    Background-attachment: scroll The image scrolls with the page - unlocked Show example
    Background-attachment: fixed The image is locked Show example

    For example, the code below will fix the background image.

    
    body {
    	background-color: #FFCC66;
    	background-image: url("butterfly.gif");
    	background-repeat: no-repeat;
    	background-attachment: fixed;
    }
    
    h1 {
    	color: #990000;
    	background-color: #FC9804;
    }
    

    Place background image [background-position]

    By default, a background image will be positioned in the top left corner of the screen. The property background-position allows you to change this default and position the background image anywhere you like on the screen.

    There are numerous ways to set the values of background-position. However, all of them are formatted as a set of coordinates. For example, the value '100px 200px' positions the background image 100px from the left side and 200px from the top of the browser window.

    The coordinates can be indicated as percentages of the browser window, fixed units (pixels, centimetres, etc.) or you can use the words top, bottom, center, left and right. The model below illustrates the system:


    The table below gives some examples.

    Value Description Example
    background-position: 2cm 2cm The image is positioned 2 cm from the left and 2 cm down the page Show example
    background-position: 50% 25% The image is centrally positioned and one fourth down the page Show example
    background-position: top right The image is positioned in the top-right corner of the page Show example

    The code example below positions the background image in the bottom right corner:

    
    body {
    	background-color: #FFCC66;
    	background-image: url("butterfly.gif");
    	background-repeat: no-repeat;
    	background-attachment: fixed;
    	background-position: right bottom;
    }
    
    h1 {
    	color: #990000;
    	background-color: #FC9804;
    }
    

    Compiling [background]

    The property background is a short hand for all the background properties listed in this lesson.

    With background you can compress several properties and thereby write your style sheet in a shorter way which makes it easier to read.

    For example, look at these five lines:

    
    background-color: #FFCC66;
    background-image: url("butterfly.gif");
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: right bottom;
    

    Using background the same result can be achieved in just one line of code:

    
    background: #FFCC66 url("butterfly.gif") no-repeat fixed right bottom;
    

    The list of order is as follows:

    [background-color] | [background-image] | [background-repeat] | [background-attachment] | [background-position]

    If a property is left out, it will automatically be set to its default value. For example, if background-attachment and background-position are taken out of the example:

    
    background: #FFCC66 url("butterfly.gif") no-repeat;
    

    These two properties that are not specified would merely be set to their default values which as you know are scroll and top left.

    Summary

    In this lesson, you have already learned new techniques that would not be possible using HTML. The fun continues in the next lesson which examines the broad range of possibilities when using CSS to describe fonts. 

    Lesson 4: Fonts

    In this lesson you will learn about fonts and how they are applied using CSS. We will also look at how to work around the issue that specific fonts chosen for a website can only be seen if the font is installed on the PC used to access the website. The following CSS properties will be described:

    Font family [font-family]

    The property font-family is used to set a prioritized list of fonts to be used to display a given element or web page. If the first font on the list is not installed on the computer used to access the site, the next font on the list will be tried until a suitable font is found.

    There are two types of names used to categorize fonts: family-names and generic families. The two terms are explained below.

    Family-name
    Examples of a family-name (often known as "font") can e.g. be "Arial", "Times New Roman" or "Tahoma".
    Generic family
    Generic families can best be described as groups of family-names with uniformed appearances. An example is sans-serif, which is a collection of fonts without "feet".

    The difference can also be illustrated like this:

    Three examples of generic families and some of their family members

    When you list fonts for your web site, you naturally start with the most preferred font followed by some alternative fonts. It is recommended to complete the list with a generic font family. That way at least the page will be shown using a font of the same family if none of the specified fonts are available.

    An example of a prioritized list of fonts could look like this:

    
    h1 {font-family: arial, verdana, sans-serif;}
    h2 {font-family: "Times New Roman", serif;}
    

    Headlines marked with <h1> will be displayed using the font "Arial". If this font is not installed on the user's computer, "Verdana" will be used instead. If both these fonts are unavailable, a font from the sans-serif family will be used to show the headlines.

    Notice how the font name "Times New Roman" contains spaces and therefore is listed using quotation marks.

    Font style [font-style]

    The property font-style defines the chosen font either in normal, italic or oblique. In the example below, all headlines marked with <h2> will be shown in italics.

    
    h1 {font-family: arial, verdana, sans-serif;}
    h2 {font-family: "Times New Roman", serif; font-style: italic;}
    

    Font variant [font-variant]

    The property font-variant is used to choose between normal or small-caps variants of a font. A small-caps font is a font that uses smaller sized capitalized letters (upper case) instead of lower case letters. Confused? Take a look at these examples:

    Four examples of fonts in small caps

    If font-variant is set to small-caps and no small-caps font is available the browser will most likely show the text in uppercase instead.

    
    h1 {font-variant: small-caps;}
    h2 {font-variant: normal;}
    

    Font weight [font-weight]

    The property font-weight describes how bold or "heavy" a font should be presented. A font can either be normal or bold. Some browsers even support the use of numbers between 100-900 (in hundreds) to describe the weight of a font.

    
    p {font-family: arial, verdana, sans-serif;}
    td {font-family: arial, verdana, sans-serif; font-weight: bold;}
    

    Font size [font-size]

    The size of a font is set by the property font-size.

    There are many different units (e.g. pixels and percentages) to choose from to describe font sizes. In this tutorial we will focus on the most common and appropriate units. Examples include:

    
    h1 {font-size: 30px;}
    h2 {font-size: 12pt;}
    h3 {font-size: 120%;}
    p {font-size: 1em;}
    

    There is one key difference between the four units above. The units 'px' and 'pt' make the font size absolute, while '%' and 'em' allow the user to adjust the font size as he/she see fit. Many users are disabled, elderly or simply suffer from poor vision or a monitor of bad quality. To make your website accessible for everybody, you should use adjustable units such as '%' or 'em'.

    Below you can see an illustration of how to adjust the text size in Mozilla Firefox and Internet Explorer. Try it yourself - neat feature, don't you think?

    Compiling [font]

    Using the font short hand property it is possible to cover all the different font properties in one single property.

    For example, imagine these four lines of code used to describe font-properties for <p>:

    
    p {
    	font-style: italic;
    	font-weight: bold;
    	font-size: 30px;
    	font-family: arial, sans-serif;
    }
    

    Using the short hand property, the code can be simplified:

    
    p {
    	font: italic bold 30px arial, sans-serif;
    }
    

    The order of values for font is:

    font-style | font-variant | font-weight | font-size | font-family

    Summary

    You have now learned about some of the possibilities related to fonts. Remember that one of the major advantages of using CSS to specify fonts is that at any given time, you can change font on an entire website in just a few minutes. CSS saves time and makes your life easier. In the next lesson we will look at text.

     

    Lesson 4: Fonts

    In this lesson you will learn about fonts and how they are applied using CSS. We will also look at how to work around the issue that specific fonts chosen for a website can only be seen if the font is installed on the PC used to access the website. The following CSS properties will be described:

    Font family [font-family]

    The property font-family is used to set a prioritized list of fonts to be used to display a given element or web page. If the first font on the list is not installed on the computer used to access the site, the next font on the list will be tried until a suitable font is found.

    There are two types of names used to categorize fonts: family-names and generic families. The two terms are explained below.

    Family-name
    Examples of a family-name (often known as "font") can e.g. be "Arial", "Times New Roman" or "Tahoma".
    Generic family
    Generic families can best be described as groups of family-names with uniformed appearances. An example is sans-serif, which is a collection of fonts without "feet".

    The difference can also be illustrated like this:

    Three examples of generic families and some of their family members

    When you list fonts for your web site, you naturally start with the most preferred font followed by some alternative fonts. It is recommended to complete the list with a generic font family. That way at least the page will be shown using a font of the same family if none of the specified fonts are available.

    An example of a prioritized list of fonts could look like this:

    
    h1 {font-family: arial, verdana, sans-serif;}
    h2 {font-family: "Times New Roman", serif;}
    

    Headlines marked with <h1> will be displayed using the font "Arial". If this font is not installed on the user's computer, "Verdana" will be used instead. If both these fonts are unavailable, a font from the sans-serif family will be used to show the headlines.

    Notice how the font name "Times New Roman" contains spaces and therefore is listed using quotation marks.

    Font style [font-style]

    The property font-style defines the chosen font either in normal, italic or oblique. In the example below, all headlines marked with <h2> will be shown in italics.

    
    h1 {font-family: arial, verdana, sans-serif;}
    h2 {font-family: "Times New Roman", serif; font-style: italic;}
    

    Font variant [font-variant]

    The property font-variant is used to choose between normal or small-caps variants of a font. A small-caps font is a font that uses smaller sized capitalized letters (upper case) instead of lower case letters. Confused? Take a look at these examples:

    Four examples of fonts in small caps

    If font-variant is set to small-caps and no small-caps font is available the browser will most likely show the text in uppercase instead.

    
    h1 {font-variant: small-caps;}
    h2 {font-variant: normal;}
    

    Font weight [font-weight]

    The property font-weight describes how bold or "heavy" a font should be presented. A font can either be normal or bold. Some browsers even support the use of numbers between 100-900 (in hundreds) to describe the weight of a font.

    
    p {font-family: arial, verdana, sans-serif;}
    td {font-family: arial, verdana, sans-serif; font-weight: bold;}
    

    Font size [font-size]

    The size of a font is set by the property font-size.

    There are many different units (e.g. pixels and percentages) to choose from to describe font sizes. In this tutorial we will focus on the most common and appropriate units. Examples include:

    
    h1 {font-size: 30px;}
    h2 {font-size: 12pt;}
    h3 {font-size: 120%;}
    p {font-size: 1em;}
    

    There is one key difference between the four units above. The units 'px' and 'pt' make the font size absolute, while '%' and 'em' allow the user to adjust the font size as he/she see fit. Many users are disabled, elderly or simply suffer from poor vision or a monitor of bad quality. To make your website accessible for everybody, you should use adjustable units such as '%' or 'em'.

    Below you can see an illustration of how to adjust the text size in Mozilla Firefox and Internet Explorer. Try it yourself - neat feature, don't you think?

    Compiling [font]

    Using the font short hand property it is possible to cover all the different font properties in one single property.

    For example, imagine these four lines of code used to describe font-properties for <p>:

    
    p {
    	font-style: italic;
    	font-weight: bold;
    	font-size: 30px;
    	font-family: arial, sans-serif;
    }
    

    Using the short hand property, the code can be simplified:

    
    p {
    	font: italic bold 30px arial, sans-serif;
    }
    

    The order of values for font is:

    font-style | font-variant | font-weight | font-size | font-family

    Summary

    You have now learned about some of the possibilities related to fonts. Remember that one of the major advantages of using CSS to specify fonts is that at any given time, you can change font on an entire website in just a few minutes. CSS saves time and makes your life easier. In the next lesson we will look at text.

    Lesson 6: Links

    You can apply what you already learned in the previous lessons to links (i.e. change colors, fonts, underline, etc). The new thing is that CSS allows you to define these properties differently depending on whether the link is unvisited, visited, active, or whether the cursor is on the link. This makes it possible to add fancy and useful effects to your website. To control these effects you use so-called pseudo-classes.

    What is a pseudo-class?

    A pseudo-class allows you to take into account different conditions or events when defining a property for an HTML tag.

    Let's look at an example. As you know, links are specified in HTML with <a> tags. We can therefore use a as a selector in CSS:

    
    a {
    	color: blue;
    }
    

    A link can have different states. For example, it can be visited or not visited. You can use pseudo-classes to assign different styles to visited and unvisited links.

    
    a:link {
    	color: blue;
    }
    
    a:visited {
    	color: red;
    }
    

    Use a:link and a:visited for unvisited and visited links respectively. Links that are active have the pseudo-class a:active and a:hover is when the cursor is on the link.

    We will now go through each of the four pseudo-classes with examples and further explanation.

    Pseudo-class: link

    The pseudo-class :link is used for links leading to pages that the user has not visited.

    In the code example below, unvisited links will be light blue.

    
    a:link {
    	color: #6699CC;
    }
    

    Pseudo-class: visited

    The pseudo-class :visited is used for links leading to pages that the user has visited. For example, the code below would make all visited links dark purple:

    
    a:visited {
    	color: #660099;
    }
    

    Pseudo-class: active

    The pseudo-class :active is used for links that are active.

    This example gives active links a yellow background color:

    
    a:active {
    	background-color: #FFFF00;
    }
    

    Pseudo-class: hover

    The pseudo-class :hover is used when the mouse pointer hovers over a link.

    This can be used to create interesting effects. For example, if we want our links to be orange and be italicized when the cursor is pointed at them, our CSS should look like this:

    
    a:hover {
    	color: orange;
    	font-style: italic;
    }
    

    Example 1: Effect when the cursor is over a link

    It is particular popular to create different effects when the cursor is over a link. We will therefore look at a few extra examples related to the pseudo-class :hover.

    Example 1a: Spacing between letters

    As you will remember from lesson 5, the spacing between letters can be adjusted using the property letter-spacing. This can be applied to links for a special effect:

    
    a:hover {
    	letter-spacing: 10px;
    	font-weight:bold;
    	color:red;
    }
    

    Example 1b: UPPERCASE and lowercase

    In lesson 5 we looked at the property text-transform, which can switch between upper- and lowercase letters. This can also be used to create an effect for links:

    
    a:hover {
    	text-transform: uppercase;
    	font-weight:bold;
    	color:blue;
    	background-color:yellow;
    }
    

    The two examples gives you an idea about the almost endless possibilities for combining different properties. You can create your own effects - give it a try!

    Example 2: Remove underline of links

    A frequently asked question is how to remove the underlining of links?

    You should consider carefully whether it is necessary to remove the underlining as it might decrease usability of your website significantly. People are used to blue underlined links on web pages and know that they can click on them. Even my mum knows that! If you change the underlining and color of links there is a good chance that users will get confused and therefore not get the full benefit of the content on your website.

    That said, it is very simple to remove the underlining of links. As you will recall from lesson 5, the property text-decoration can be used to determine whether text is underlined or not. To remove underlining, simply set the value of text-decoration to none.

    
    a {
    	text-decoration:none;
    }
    

    Alternatively, you can set text-decoration along with other properties for all four pseudo-classes.

    
    a:link {
    	color: blue;
    	text-decoration:none;
    }
    
    a:visited {
    	color: purple;
    	text-decoration:none;
    }
    
    a:active {
    	background-color: yellow;
    	text-decoration:none;
    }
    
    a:hover {
    	color:red;
    	text-decoration:none;
    }
    

    Summary

    In this lesson you have learned about pseudo-classes, using some of the properties from the previous lessons. This should give you an idea of some the possibilities CSS provides.

    In the next lesson we will teach you how to define properties for specific elements and groups of elements.

    Lesson 7: Identification and grouping of elements (class and id)

    Sometimes you want to apply a special style to a particular element or a particular group of elements. In this lesson, we will take a closer look at how you can use class and id to specify properties for selected elements.

    How can you color one particular headline differently than the other headlines on your website? How can you group your links into different categories and give each category a special style? These are just examples of questions we will answer in this lesson.

    Grouping elements with class

    Let's say that we have two lists of links of different grapes used for white wine and red wine. The HTML code could look like this:

    
    <p>Grapes for white wine:</p>
    <ul>
    <li><a href="ri.htm">Riesling</a></li>
    <li><a href="ch.htm">Chardonnay</a></li>
    <li><a href="pb.htm">Pinot Blanc</a></li>
    </ul>
    
    <p>Grapes for red wine:</p>
    <ul>
    <li><a href="cs.htm">Cabernet Sauvignon</a></li>
    <li><a href="me.htm">Merlot</a></li>
    <li><a href="pn.htm">Pinot Noir</a></li>
    </ul>
    

    Then we want the white wine links to be yellow, the red wine links to be red and the rest of the existing links on the webpage to stay blue.

    To achieve this, we divide the links into two categories. This is done by assigning a class to each link using the attribute class.

    Let us try to specify some classes in the example above:

    
    <p>Grapes for white wine:</p>
    <ul>
    <li><a href="ri.htm" class="whitewine">Riesling</a></li>
    <li><a href="ch.htm" class="whitewine">Chardonnay</a></li>
    <li><a href="pb.htm" class="whitewine">Pinot Blanc</a></li>
    </ul>
    
    <p>Grapes for red wine:</p>
    <ul>
    <li><a href="cs.htm" class="redwine">Cabernet Sauvignon</a></li>
    <li><a href="me.htm" class="redwine">Merlot</a></li>
    <li><a href="pn.htm" class="redwine">Pinot Noir</a></li>
    </ul>
    

    We can hereafter define special properties for links belonging to whitewine and redwine, respectively.

    
    a {
    	color: blue;
    }
    
    a.whitewine {
    	color: #FFBB00;
    }
    
    a.redwine {
    	color: #800000;
    }
    

    As shown in the example you can define the properties for elements which belong to a certain class by using .classname in the style sheet of the document.

    Identification of element using id

    In addition to grouping elements, you might need to identify one unique element. This is done by using the attribute id.

    What is special about the attribute id is that there can not be two elements in the same document with the same id. Each id has to be unique. In other cases, you should use the class attribute instead. Now, let us take a look at an example of a possible usage of id:

    
    <h1>Chapter 1</h1>
    ...
    <h2>Chapter 1.1</h2>
    ...
    <h2>Chapter 1.2</h2>
    ...
    <h1>Chapter 2</h1>
    ...
    <h2>Chapter 2.1</h2>
    ...
    <h3>Chapter 2.1.2</h3>
    ...
    

    The above could be headings of any document split into chapters or paragraphs. It would be natural to assign an id to each chapter as follows:

    
    <h1 id="c1">Chapter 1</h1>
    ...
    <h2 id="c1-1">Chapter 1.1</h2>
    ...
    <h2 id="c1-2">Chapter 1.2</h2>
    ...
    <h1 id="c2">Chapter 2</h1>
    ...
    <h2 id="c2-1">Chapter 2.1</h2>
    ...
    <h3 id="c2-1-2">Chapter 2.1.2</h3>
    ...
    

    Let us say that the headline for chapter 1.2 must be in red. This can be done accordingly with CSS:

    
    #c1-2 {
    	color: red;
    }
    

    As shown in the example above you can define the properties in a specific element by using #id in the stylesheet of the document.

    Summary

    In this lesson we have learned that through the use of the selectors, class and id, you are able to specify properties for specific elements.

    In the next lesson, we will take a closer look at two HTML-elements which is widely used in connection with CSS: <span> and <div>.

    Lesson 8: Grouping of elements (span and div)

    The elements <span> and <div> are used to group and structure a document and will often be used together with the attributes class and id.

    In this lesson, we will take a closer look at the use of <span> and <div> as exactly these two HTML elements are of central importance with regards to CSS.

    Grouping with <span>

    The element <span> is what you could call a neutral element which does not add anything to the document itself. But with CSS, <span> can be used to add visual features to specific parts of text in your documents.

    An example of this could be this Benjamin Franklin quotation:

    
    <p>Early to bed and early to rise
    makes a man healthy, wealthy and wise.</p>

    Lets us say we want what Mr. Franklin sees as the benefits of not sleeping your day away emphasized in red. For that purpose, we can mark the benefits with <span>. Each span is then added a class, which we can define in our style sheet:

    
    <p>Early to bed and early to rise
    makes a man <span class="benefit">healthy</span>,
    <span class="benefit">wealthy</span>
    and <span class="benefit">wise</span>.</p>

    The CSS belonging to it:

    
    span.benefit {
    	color:red;
    }
    

    Of course you may also use id to add style to the <span>-elements. Just as long as you remember, that you'll have to apply a unique id to each of the three <span>-elements, as you learned in the previous lesson.

    Grouping with <div>

    Whereas <span> is used within a block-level element as seen in the previous example, <div> is used to group one or more block-level elements.

    Aside from this difference, the grouping with <div> works in more or less the same way. Let us take a look at an example with two lists of U.S. presidents divided into their political affiliations:

    
    <div id="democrats">
    <ul>
    <li>Franklin D. Roosevelt</li>
    <li>Harry S. Truman</li>
    <li>John F. Kennedy</li>
    <li>Lyndon B. Johnson</li>
    <li>Jimmy Carter</li>
    <li>Bill Clinton</li>
    </ul>
    </div>
    
    <div id="republicans">
    <ul>
    <li>Dwight D. Eisenhower</li>
    <li>Richard Nixon</li>
    <li>Gerald Ford</li>
    <li>Ronald Reagan</li>
    <li>George Bush</li>
    <li>George W. Bush</li>
    </ul>
    </div>
    

    And in our style sheet, we can utilize the grouping in the exact same way as above:

    
    #democrats {
    	background:blue;
    }
    
    #republicans {
    	background:red;
    }
    

    In the examples above, we have only used <div> and <span> on very simple things such as text and background colors. Both elements have the potential to do much more advanced things. However this will not be introduced in this lesson. We will look into these later in this tutorial.

    Summary

    In lesson 7 and 8, you have learned about the selectors id and class and the elements span and div.

    You should now be able to group and identify, more or less, all parts of a document, which is a big step in the direction of mastering CSS. In lesson 9 we will introduce you to the box model.

    Lesson 9: The box model

    The box model in CSS describes the boxes which are being generated for HTML-elements. The box model also contains detailed options regarding adjusting margin, border, padding and content for each element. The diagram below shows how the box model is constructed:

    The box model in CSS


    The illustration above might seem pretty theoretical to look at, so let's try to use the model in an actual case with a headline and some text. The HTML for our example is (from the Universal Declaration of Human Rights):

    
    <h1>Article 1:</h1>
    
    <p>All human beings are born free
    and equal in dignity and rights.
    They are endowed with reason and conscience
    and should act towards one another in a
    spirit of brotherhood</p>
    

    By adding some color and font-information the example could be presented as follows:

    The example contains two elements: <h1> and <p>. The box model for the two elements can be illustrated as follows:

    Even though it may look a bit complicated, the illustration shows how each HTML-element is surrounded by boxes. Boxes which we can adjust using CSS.

    Summary

    In this lesson you have been introduced to the box model. In the following three lesson we will take a closer look at how to create and control elements in the box model.

    Lesson 9: The box model

    The box model in CSS describes the boxes which are being generated for HTML-elements. The box model also contains detailed options regarding adjusting margin, border, padding and content for each element. The diagram below shows how the box model is constructed:

    The box model in CSS


    The illustration above might seem pretty theoretical to look at, so let's try to use the model in an actual case with a headline and some text. The HTML for our example is (from the Universal Declaration of Human Rights):

    
    <h1>Article 1:</h1>
    
    <p>All human beings are born free
    and equal in dignity and rights.
    They are endowed with reason and conscience
    and should act towards one another in a
    spirit of brotherhood</p>
    

    By adding some color and font-information the example could be presented as follows:

    The example contains two elements: <h1> and <p>. The box model for the two elements can be illustrated as follows:

    Even though it may look a bit complicated, the illustration shows how each HTML-element is surrounded by boxes. Boxes which we can adjust using CSS.

    Summary

    In this lesson you have been introduced to the box model. In the following three lesson we will take a closer look at how to create and control elements in the box model.

     

    Lesson 11: Borders

    Borders can be used for many things, for example as a decorative element or to underline a separation of two things. CSS gives you endless options when using borders in your pages.

    The width of borders [border-width]

    The width of borders is defined by the property border-width, which can obtain the values thin, medium, and thick, or a numeric value, indicated in pixels. The figure below illustrates the system:

    Examples of border-width

    The color of borders [border-color]

    Colors

    The property border-color defines which color the border has. The values are the normal color-values for example "#123456", "rgb(123,123,123)" or "yellow" .

    Types of borders [border-style]

    There are different types of borders to choose from. Below are shown 8 different types of borders as Internet Explorer 5.5 interprets them. All examples are shown with the color "gold" and the thickness "thick" but can naturally be shown in other colors and thicknesses.

    The values none or hidden can be used if you do not want any border.

    Different types of borders

    Examples of defining borders

    The three properties described above can be put together by each element and thereby produce different borders. To illustrate this, we will take a look at a document where we define different borders for <h1>, <h2>, <ul> and <p>. The result may not be that pretty but it illustrates some of the many possibilities:

    
    h1 {
    	border-width: thick;
    	border-style: dotted;
    	border-color: gold;
    }
    
    h2 {
    	border-width: 20px;
    	border-style: outset;
    	border-color: red;
    }
    
    p {
    	border-width: 1px;
    	border-style: dashed;
    	border-color: blue;
    }
    
    ul {
    	border-width: thin;
    	border-style: solid;
    	border-color: orange;
    }
    

    It is also possible to state special properties for top-, bottom-, right- or left borders. The following example shows you how:

    
    h1 {
    	border-top-width: thick;
    	border-top-style: solid;
    	border-top-color: red;
    
    	border-bottom-width: thick;
    	border-bottom-style: solid;
    	border-bottom-color: blue;
    
    	border-right-width: thick;
    	border-right-style: solid;
    	border-right-color: green;
    
    	border-left-width: thick;
    	border-left-style: solid;
    	border-left-color: orange;
    }
    

    Compilation [border]

    As it is also the case for many other properties, you can compile many properties into one using border. Let us take a look at an example:

    
    p {
    	border-width: 1px;
    	border-style: solid;
    	border-color: blue;
    }
    

    Can be compiled into:

    
    p {
    	border: 1px solid blue;
    }
    

    Summary

    In this lesson you have learned about the endless options CSS gives you when using borders in your pages.

    In the next lesson, we will look at how you define the dimensions in the box model - height and width.

     

    Lesson 12: Height and width

    Up until now, we have not cared much about the dimensions of the elements we have worked with. In this lesson, we will take a look at how you easily can define the height and width of an element.

    Setting the width [width]

    With the width-property, you can define a certain width of an element.

    The simple example below provides us with a box wherein text can be typed:

    
    div.box {
    	width: 200px;
    	border: 1px solid black;
    	background: orange;
    }
    

    Setting the height [height]

    In the example above notice how the height of the box is set by the content of the box. You can affect the height of an element with the property height. As an example let us try to make the box in the example 500px high:

    
    div.box {
    	height: 500px;
    	width: 200px;
    	border: 1px solid black;
    	background: orange;
    }
    

     

    Lesson 13: Floating elements (floats)

    An element can be floated to the right or to left by using the property float. That is to say that the box with its contents either floats to the right or to the left in a document (or the containing box) (see lesson 9 for a description of the Box model). The following figure illustrates the principle:

    A left-floating box

    If we for example would like to have a text wrapping around a picture, the result would be like this:

    A left-floating box with a picture and text wrapped around it

    How is it done?

    The HTML code for the example above, look as follows:

    
    <div id="picture">
    	<img src="bill.jpg" alt="Bill Gates">
    </div>
    
    <p>causas naturales et antecedentes, 
    idciro etiam nostrarum voluntatum...</p>
    

    To get the picture floating to the left and the text to surround it, you only have to define the width of the box which surrounds the picture and thereafter set the property float to left:

    
    #picture {
    	float:left;
    	width: 100px;
    }
    

    Another example: columns

    Floats can also be used for columns in a document. To create the columns, you simply have to structure the desired columns in the HTML-code with <div> as follows:

    
    <div id="column1">
    	<p>Haec disserens qua de re agatur
    	et in quo causa consistat non videt...</p>
    </div>
    
    <div id="column2">
    	<p>causas naturales et antecedentes, 
    	idciro etiam nostrarum voluntatum...</p>
    </div>
    
    <div id="column3">
    	<p>nam nihil esset in nostra 
    	potestate si res ita se haberet...</p>
    </div>
    

    Now the desired width of the columns is set to e.g. 33%, and then you simply float each column to the left by defining the property float:

    
    #column1 {
    	float:left;
    	width: 33%;
    }
    
    #column2 {
    	float:left;
    	width: 33%;
    }
    
    #column3 {
    	float:left;
    	width: 33%;
    }
    

    float can be set as either left, right or none.

    The property clear

    The clear property is used to control how the subsequent elements of floated elements in a document shall behave.

    By default, the subsequent elements are moved up to fill the available space which will be freed when a box is floated to a side. Look at the example above wherein the text is automatically moved up beside the picture of Bill Gates.

    The property clear can assume the values left, right, both or none. The principle is, if clear, for example, is set to both for a box, the top margin border of this box will always be under the lower margin border for possible floating boxes coming from above.

    
    <div id="picture">
    	<img src="bill.jpg" alt="Bill Gates">
    </div>
    
    <h1>Bill Gates</h1>
    
    <p class="floatstop">causas naturales et antecedentes, 
    idciro etiam nostrarum voluntatum...</p>
    

    To avoid the text from floating up next to the picture, we can add the following to our CSS:

    
    #picture {
    	float:left;
    	width: 100px;
    }
    
    .floatstop {
    	clear:both;
    }
    
Member Login| Logout
 
East African Standard Time

Username
Password
Showbiz - Soulja Boy Tell'em - "Kiss Me Thru The Phone"
 
Exclusive
 

Adventure
 
Adventure with us today. Find out more on travel agents and other facilities around East Africa from here. Find out the most fascinating places that you can not miss to visit while in East Africa. Get all this from richcomputersolutions - your favorite website. Remember if you want to join this website, suggest a user name and password and send them to richymany2k@gmail.com or click on sin-up for more. Joining this website is a 100% free. Remember that we offer the best in repair of computers, scanners , printers. On top of that, reach us for quality photography, internet connections .graphic designing, website designing, coomputer training and many mor services. You are welcome to the sole source of information in East Africa.
Advertise here
 
 
There have been 41420 visitors (173650 hits) To this Website today
I welcome you all to his website, for all the best you need and enjoy all the benefits as a member. This website website is still under construction but always hope to find more here with Rich Computer Solutions. Always come back for more events. This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free