Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Create modern Web sites using HTML5 and CSS3

Implementing the canvas and video elements in HTML5

Joe Lennon, Software developer, Freelance
Photo of Joe Lennon
Joe Lennon is a 24-year-old software developer from Cork, Ireland. Joe is author of the forthcoming Apress book Beginning CouchDB, and has contributed several technical articles and tutorials to IBM developerWorks. In his spare time, Joe likes to play football (soccer), tinker with gadgets, and work on his Xbox 360 gamerscore. He can be contacted via his Web site at www.joelennon.ie.

Summary:  Since the World Wide Web emerged in the early 1990s, HTML has evolved to become a relatively powerful markup language, which, when backed up by its close partners JavaScript and CSS, can be used to create visually stunning and interactive Web sites and applications. This tutorial serves as a hands-on introduction to HTML5 and CSS3. It provides information about the functionality and syntax for many of the new elements and APIs that HTML5 has to offer, as well as the new selectors, effects, and features that CSS3 brings to the table. Finally, it will show you how to develop a sample Web page that harnesses many of these new features. By the time you have finished this tutorial, you will be ready to build Web sites or applications of your own that are powered by HTML5 and CSS3.

Date:  02 Mar 2010
Level:  Intermediate PDF:  A4 and Letter (342 KB | 33 pages)Get Adobe® Reader®

Activity:  317248 views
Comments:  

New features in CSS3

This section introduces you to the new features that can be found in the CSS level 3 specifications—including new CSS selectors such as structural, state-based, and negation pseudo-classes, as well as other new types of selectors. It also shows many of the effects that CSS3 offers that would previously have required images to be created using a separate application and saved as GIF, JPG, or PNG. Such effects include drop shadows on text and boxes, rounded corners on borders, and the use of opacity to create a translucent appearance. Many of these features (such as opacity and rounded corners) are relatively widespread in use due to the fact that they degrade very gracefully in legacy Web browsers. Next, you will learn about the new multicolumn layouts that can be created using CSS3. These layouts are a throwback to the newspaper layout where text will stretch over a set number of columns or to a particular column width as required. Another feature that will be discussed is the issue of including non-standard Web fonts using the @font-face tag. Finally, some of the other new CSS3 features will be introduced, such as support for HSL (Hue, Saturation, and Lightness) and RGBA (Red, Green, Blue, and Alpha) color models.

New selectors

A CSS selector refers to the manner in which HTML element(s) are styled using a stylesheet. For example, to put a border around all <div> elements you would use the selector div: div { border: 1px solid #000; }.

To apply a background color to all elements with the class highlight you would use the selector .highlight: .highlight { background-color: yellow; }.

Finally, to change the width of an element with an ID attribute value of myDiv, you would use: #myDiv { width: 250px; }.

Of course, these selectors can be combined, so to select all <div> elements with the class highlight, you would use div.highlight, or to select the <div> element with the ID myDiv you would use div#myDiv.

In addition to these straightforward selectors, CSS also includes (and has done since previous versions) a series of more complex selectors. For example you can use the selector input[type="text"] to select only the input elements that contain an attribute type with the value of text. Or you can use the pseudo-class :hover to style an element when the mouse is over it, for example: a:hover { color: red; }.

There are many more of these selectors, all of which are provided to make it easier to select an element to style. With CSS3, even more new selectors have been added to the mix, all of which make developers' lives easier and reduce the amount of HTML and JavaScript they need to write.

Attribute selectors

E[foo^="bar"]
Select an element, E, whose foo attribute begins exactly with the string bar
E[foo$="bar"]
Select an element, E, whose foo attribute ends exactly with the string bar
E[foo*="bar"]
Select an element, E, whose foo attribute contains the string bar

Structural pseudo-classes

E:root
Select an element, E, the root of the document (the <html> tag)
E:nth-child(n)
Select an element, E, the n-th child of its parent element
E:nth-last-child(n)
Select an element, E, the n-th last child of its parent element
E:nth-of-type(n)
Select an element, E, the n-th sibling of its type
E:nth-last-of-type(n)
Select an element, E, the n-th last sibling of its type
E:last-child
Select an element, E, whose is the last child of its parent element (Note that E:first-child was previously defined in CSS2)
E:first-of-type
Select an element, E, the first sibling of its type
E:last-of-type
Select an element, E, the last sibling of its type
E:only-child
Select an element, E, the only child of its parent element
E:only-of-type
Select an element, E, the only sibling of its type
E:empty
Select an element, E, that has no children (including text nodes)

The target pseudo-class

E:target
Select an element, E, who is the target of the referring URI

UI element states pseudo-classes

E:enabled
Select a user interface element, E, which is enabled
E:disabled
Select a user interface element, E, which is disabled
E:checked
Select a user interface element, E (a radio button or checkbox), which is checked or selected

Negation pseudo-class

E:not(s)
Select an element, E, which does not match the simple selector s

General sibling combinator

E ~ F
Select an element, F, which is preceded by an element, E

Browser support for the new attribute selectors and the general sibling combinator is excellent, as they work on almost all modern Web browsers. Support for the new pseudo-classes is included with the latest versions of most browsers, but you may need to include fallbacks for older browsers such as Internet Explorer 6/7 and Firefox 3.0.


New effects

Although the new selectors are probably the features that reduce developers' headaches the most, the enhancements people most want to see are the shiny new effects that are available. These are facilitated through a slew of new CSS properties, including:

  • background (now supports multiple backgrounds)
  • background-clip
  • background-origin
  • background-size
  • border-radius (rounded corners)
  • border-image
  • border-color (gradient borders)
  • box-shadow (drop shadow on boxes without images)
  • box-sizing
  • opacity
  • outline-offset
  • resize
  • text-overflow
  • text-shadow
  • word-wrap

The sample HTML5/CSS3 Web page you'll create at the end of this tutorial will show some of these new effects in action.


Multicolumn layouts

CSS3 multicolumn layouts allow for text to be spread over a number of columns, like you might find in a newspaper. This can be done in two ways, either using the column-width property, where you define how wide each column should be (the number of columns is determined by the space available to the container), or using column-count, where you define the number of columns that should be used. The width will then vary based on the space available to each column.

Other features of multicolumn layouts include the column-gap property, which allows the developer to define the space that should be present between columns when the column-width method is used. Another useful addition is the column-rule property, which allows a border-style rule to be placed between columns. Finally there is the column-space-distribution property, which determines how left over space should be allocated between the columns.

Multicolumn layouts are currently supported by Mozilla and WebKit browsers. At present, these are implemented by means of temporary proprietary properties prefixed with -moz or -webkit, respectively. When the specification has been finalized, these browsers will eventually move to the CSS standard properties.


Web fonts

Web fonts were actually proposed for CSS2 and have been available in Microsoft Internet Explorer since version 5. Unfortunately, this required the use of the proprietary .eot (Embedded Open Type) font format, and none of the other browser vendors chose to implement it. As a result, Web fonts never really took off on CSS2-based Web sites.

With the latest versions of Firefox, Safari, Chrome, and Opera, however, you can now use the @font-face rule to use any licensed .ttf (TrueType) or .otf (OpenType) font on your Web page. An example of the @font-face rule is as follows: @font-face { font-family: Alexa; src: url('Alexa.otf'); }.

You can now use this font in your own CSS rules, such as: article p { font-family: Alexa, Arial, Helvetica, sans-serif; }.

Remember that fonts are like images—if they are not your own you may need permission to use them on the Web. Alternatively, you can pay for (or download for free) some royalty-free fonts that can be included on your Web pages as you please.


Other new features

CSS3 also includes many other new features, including support for new color values, notably HSL (Hue, Saturation, Lightness) and two color values with alpha properties—RGBA (Red, Green, Blue, Alpha) and HSLA (Hue, Saturation, Lightness, Alpha). Media queries will allow you to define different styles for different devices based on their viewport size. For example, you can provide specific styles for devices with a viewport of less than 500 pixels (such as a smartphone, PDA, or other mobile device). CSS3's speech module allows you to control properties of the speech of a screen reader, including voice volume, balance, rate, stress, and more.

3 of 8 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development
ArticleID=470908
TutorialTitle=Create modern Web sites using HTML5 and CSS3
publish-date=03022010
author1-email=joe@joelennon.ie
author1-email-cc=bwetmore@us.ibm.com