Beginning CSS: Cascading Style Sheets for Web Design

Beginning CSS: Cascading Style Sheets for Web Design

by Ian Pouncey, Richard York

View All Available Formats & Editions

Completely updated material and new examples show you what CSS can do

With the latest versions of Firefox, Safari, Internet Explorer, and other browsers released, CSS is more essential than ever. This beginner guide demonstrates how cascading style sheets can be used to define styles to items in Web pages, rather than format each item individually. Each


Completely updated material and new examples show you what CSS can do

With the latest versions of Firefox, Safari, Internet Explorer, and other browsers released, CSS is more essential than ever. This beginner guide demonstrates how cascading style sheets can be used to define styles to items in Web pages, rather than format each item individually. Each lesson in this full-color book has been methodically revised to be more concise and efficient, making your learning experience as productive as possible.

  • Covers the latest in CSS, including the new features of Internet Explorer, Firefox, Safari, and Google Chrome
  • Reviews the vast improvements to mobile browsers and how CSS can work with them
  • Provides helpful examples and walks you through real-world solutions to common hurdles
  • Discusses embedded fonts, compatibility tables, and cross-browser bug scenarios

Beginning CSS, Third Edition gets you completely up to date so that you can start using CSS in the newest Web and mobile browsers today!

Product Details

Publication date:
Sold by:
Barnes & Noble
File size:
16 MB
This product may take a few minutes to download.

Read an Excerpt

Beginning CSS

Cascading Style Sheets for Web Design
By Ian Pouncey Richard York

John Wiley & Sons

Copyright © 2011 John Wiley & Sons, Ltd
All right reserved.

ISBN: 978-0-470-89152-0

Chapter One

Introducing Cascading Style Sheets


* What CSS is * The history of CSS * How to create a CSS enhanced HTML document

Cascading Style Sheets (CSS) is a language designed for describing the appearance of documents written in a markup language such as HTML. With CSS you can control the color of text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out, what background images or colors are used, and a variety of other visual effects. One of the major benefits is that the same CSS can be used by more than one page, meaning that the style of an entire website can be adjusted without having to change each page individually.

The most common use for CSS is to style web pages, and in combination with HTML or XHTML (which is used to describe content) and JavaScript (which is used to add interactivity to a site), CSS is a very powerful tool.

The history of how CSS came to be isn't actually all that relevant to CSS authors of today, so you can skip the next bit if you're in a hurry. If, like me, you're interested in the nitty-gritty, read on.

In the early days of the Web, nine different proposals were made to the World Wide Web Consortium, the main standards organization for the Web which is more commonly known as the W3C, for a style sheet language to help separate the visual appearance of a document from its content. In 1994, Cascading HTML Style Sheets was proposed by Hkon Wium Lie, now CTO of Opera Software (a company you'll meet again later in this chapter), but at the time he was working at CERN with Tim Berners-Lee and Robert Cailliau, the two men who invented the World Wide Web. CHSS became CSS because CSS can be applied to more than just HTML, and in December 1996 the CSS level 1 Recommendation was published.

Since then, three more CSS specifications have been published by the W3C. CSS 2 became a recommendation in 1998, with CSS 2.1 (which fixes a few mistakes in 2), and CSS 3 currently existing as candidate recommendations.

Although CSS 3 is still under development, CSS 2.1 is likely to become a fully fledged recommendation in the near future and is well supported by all modern browsers. In this book, you will cover CSS 2.1 as it stands today and take a look at some of the new features in CSS 3 that you can use in browsers today.

In the rest of this chapter, you will learn the advantages of using CSS and then get started with your first Cascading Style Sheet.


By using CSS for the presentation of a document, you can substantially reduce the amount of time you spend composing not only a single document but an entire website As you'll discover, CSS is much more versatile than the styling mechanisms provided by HTML alone. The versatility of CSS, when harnessed effectively, can reduce the amount of hard disk space that a website occupies, as well as the amount of bandwidth required to transmit that website from the server to the browser. CSS has the following advantages:

* The presentation of an entire website can be centralized to one or a handful of documents, enabling the look and feel of a website to be updated at a moment's notice. In legacy HTML documents, the presentation is contained entirely in the body of each document. CSS brings a much needed feature to HTML: the separation of a document's structure from its presentation. CSS can be written independently of HTML. * Browsers are beginning to support multiple alternative style sheets, a feature that allows more than one design of a website to be presented at the same time. The user can simply select the look and feel that he or she likes most. This could only be done previously with the aid of more complex programming languages. * Style sheets allow content to be optimized for more than one type of device. By using the same HTML document, different versions of a website can be presented for handheld devices such as PDAs and cell phones or for printing. * Style sheets download much more quickly because web documents using CSS commonly consume less bandwidth. Browsers also use a feature called caching, a process by which your browser will download a CSS file or other web document only once, and not request that file from the web server again unless it's been updated, further providing your website with the potential for lightning-fast performance. * Users of a website can compose style sheets of their own, a feature that makes websites more accessible. For example, a user can compose a high-contrast style sheet that makes content easier to read. Many browsers provide controls for this feature for novice users, but it is CSS nonetheless.

These features, along with the power of the cascade, which you will read about in Chapter 4, makes the planning, production, and maintenance of a website simpler with Cascading Style Sheets than with HTML alone. By using CSS to present your web documents, you can cut days of development and planning time.


To write CSS, just as is the case when writing HTML source, you will need a text editor. Word processing programs such as Microsoft Word aren't ideally suited for CSS, because they automatically do lots of things that are helpful when writing a letter or book, such as correct spelling but get in the way when writing code.

Instead, you want something that doesn't make any changes that you don't want to what you type but lets you write and save plain text.

The Windows Notepad program is one example of a text editor that is ideal for composing source code. To launch Notepad, choose Start Run and then type Notepad in the Open textbox.

On Mac OS X, the Notepad equivalent is TextEdit, which can be found in the Mac OS X Applications folder.

There are more advanced text editors that will do useful things such as color your code so that it is clear which parts do what (this is known as syntax highlighting) or automatically complete code for you when you've started typing.

Editors available for Windows include:

* Notepad++: (free) * Crimson Editor: (free) * HTML-kit: (free)

And here are some alternative text editors that work with Mac OS X:

* TextWrangler: (free) * TextMate: (retail with 30 day free trial) * You can find more text editors suitable for CSS and HTML at wiki/List_of_HTML_editors

In addition, there is the very popular Adobe Dreamweaver (, which combines a text editor with a WYSIWYG (or What You See Is What You Get) code generator, which lets you use a graphical interface to create web pages in HTML and CSS. To follow along with the code in this book, you must use the code view of Dreamweaver or similar application rather than the WYSIWYG view.

You must create HTML files with the .html extension. If you use Notepad or TextEdit, beware of your files being saved with a .txt extension, which will not result in a web browser interpreting your file as ordinary text rather than HTML.

To ensure that your files are saved properly on Windows, choose Start Run and type Explorer (or right-click Start and choose Explore from the pop-up menu) to open Windows Explorer. After Windows Explorer is open, choose Tools Folder Options to open the Folder Options window, click the View tab, and uncheck the Hide Extensions for Known File Types box. Then click OK.

On Mac OS X, open Finder, and go to Finder Preferences. Select the Advanced tab, and check the box for Show All File Extensions.


The following example is designed to introduce you to what CSS is capable of. It will help you get your feet wet and get straight down to the business of writing style sheets.

NOTE You can find the images and source code for the following example at While for this example you should obtain the source code from, I recommend that for most chapters you type the example so that you can get used to writing the syntax and take in the different bits that come together in each example.

TRY IT OUT Create a Web Page with HTML and CSS

Example 1-1

To write your first CSS-enabled document, follow these steps.

1. In your text editor of choice, enter the following markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" ""> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> Example 1-1 <link rel="stylesheet" type="text/css" href="[PHRASE NOT REPRODUCIBLE IN ASCII]">

The gas giants


Jupiter is the fifth planet from the Sun and the largest planet within the Solar System. The Romans named the planet after the god Jupiter. It is a gas giant with a mass two and a half times the mass of all the other planets in our Solar System combined.

78,412,020 km     

71,492 km     

1,425,500,000,000,000 km3     

1,898,700,000,000,000,000,000,000,000 kg     
Jupiter Facts
Distance from the SunEquatorial RadiusVolumeMass

More Jupiter facts


Saturn is the sixth planet from the Sun and the second largest planet in the Solar System, after Jupiter. Saturn is named after the Roman god Saturn, equated to the Greek Cronus (the Titan father of Zeus), the Babylonian Ninurta, and the Hindu Shani.

1,426,725,400 km     

60,268 km     

827,130,000,000,000 km3     

568,510,000,000,000,000,000,000,000 kg     
Saturn Facts
Distance from the SunEquatorial RadiusVolumeMass

More Saturn facts


Uranus is the seventh planet from the Sun, and the third-largest and fourth most massive planet in the Solar System. It is named after the ancient Greek deity of the sky Uranus the father of Cronus and grandfather of Zeus.

78,412,020 km     

25,559 km     

69,142,000,000,000 km3     

86,849,000,000,000,000,000,000,000 kg     
Uranus Facts
Distance from the SunEquatorial RadiusVolumeMass

More Uranus facts


Neptune is the eighth and farthest planet from the Sun in our Solar System. Named for the Roman god of the sea, it is the fourth-largest planet by diameter and the third-largest by mass.

4,498,252,900 km     

24,764 km     

62,526,000,000,000 km3     

102,440,000,000,000,000,000,000,000 kg     
Neptune Facts
Distance from the SunEquatorial RadiusVolumeMass

More Neptune facts

2. Save the preceding file in a new folder of its own as example_1-1.html.

3. Open example_1-1.html in your browser. You will see a page that looks something like the image in Figure 1-1.

4. Create a new, blank document in your text editor, and enter the following CSS:

body { width: 650px; margin: 0 auto; background: #000; color: #FFF; font: 12px sans-serif; }

h1 { font-size: 24px; }

h2 { font-size: 18px; margin-top: 0; }

a { color: #FFF; }

a:focus, a:hover { text-decoration: none; }

table { margin-bottom: 10px; border-spacing: 0; }

caption { margin-bottom: 10px; font-size: 14px; font-weight: bold; text-align: left; }

th, td { padding: 0 10px 0 0; text-align: left; }

.planet { margin: 10px 0; padding: 20px 20px 20px 200px; border: 1px solid #FFF; background-position: 20px 20px; background-repeat: no-repeat; }

.jupiter { background-image: url(jupiter.jpg); }

.saturn { background-image: url(saturn.jpg); }

.uranus { background-image: url(uranus.jpg); }

.neptune { background-image: url(neptune.jpg); }

5. Save the preceding CSS in the same folder where you saved example_1-1.html, as [PHRASE NOT REPRODUCIBLE IN ASCII].

This code results in the image shown in Figure 1-2, when loaded into Safari on Mac OS X.

To see how example_1-1.html looks in other browsers, you can load it up by going to the File menu of the browser you'd like to view it in, selecting Open or Open File, and locating example_1-1.html on your hard disk.

How It Works

Example 1-1 is an introduction to a little of what CSS is capable of. This example is designed to get your hands dirty up front with CSS, as a preview of what you can expect throughout the rest of the book. With each new chapter, I introduce and explain the nuts and bolts that come together to make examples like the preceding one. In Figure 1-2, you see that CSS can be used to specify background images and other aesthetic aspects of an HTML document. I continue to revisit and explain the CSS that resulted in Figure 1-2 throughout the book.

If you are feeling adventurous, play around with the CSS used here — try changing some of the values to see what happens. Don't worry if things break, that's all part of learning.

Some times you might find that CSS requires some workarounds specifically to get older versions of Internet Explorer to display content in the same way as Safari, Firefox, Chrome, and Opera. Throughout this book, you also learn the hacks and workarounds that you need to make CSS-enabled web pages compatible with IE 6.


Excerpted from Beginning CSS by Ian Pouncey Richard York Copyright © 2011 by John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Meet the Author

Ian Pouncey is a web developer living in London, England, and working for the BBC. He has been working on the web for over 11 years, producing web sites from small sites for local businesses to the latest version of the Yahoo! home page. He is a member of the W3C Web Accessibility Initiative Education and Outreach Working Group and a web accessibility advocate.

Richard York is a web developer at Westlake Design, a web design and development firm, and an active contributing member of the PHP community. In addition to writing previous editions of Beginning CSS: Cascading Style Sheets for Web Design, he is also the author of Beginning JavaScript and CSS Development with jQuery.

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >