CSS3
Introduction
Internet Explorer 9+, Chrome 17+, and Firefox 4+ support CSS3. It is time to see how CSS3 works.
CSS3 Introduction
CSS3 has some interesting new features. Some of them are:
- New pseudo-class selectors.
-
rem unit. The rem unit is similar to the em unit, but the rem unit ("root em") is relative to the computed value of the 'font-size' value of the root element. Usually, the root element is the <html> tag.
There is a great article at Font sizing with rem which describes a lot of what this section is about.
The standard em unit has a potential problem: Because is it a compounded (multiplied) unit, you can have a hard time keeping track of what the "current" (technically called "base") font size is.
The CSS3 specification added a new unit, rem, which stands for "root em unit".
When you use the rem unit as your font size, the number you specify is multiplied times the em unit value for the <html> tag. The value of em for the <html> tag is usually 12pt (about 16px, depending of course on the particular monitor being used).
So, if you are setting the font size for a tag which is nested several levels deep in the tag structure of your page, you can use the rem unit in your stylesheet to restore some sanity (somewhat) to your font-size rules.
- border-color, which allows you to create gradient colors and other color effects in borders. Firefox has already implemented this function.
- border-image and border-corner-image, which allow you to use images in place of regular borders.
-
border-radius, which allows you to have rounded corners on your
page elements.
- NOTE: Even if the tag that will have rounded corners has no border, you may still be able to use border-radius to good effect. For instance, if you put a box-shadow property on the same tag which has the border-radius property, the drop shadow will "follow" the rounded corner very nicely. You can even use border-radius on a div tag, for instance, which contains an image, and the image's corners will be rounded.
- box-shadow, which allows you to easily implement multiple drop shadows (outer or inner) on box elements, specifying values for color, size, blur and offset.
- background-origin, which is used to determine how the background-position of a background is calculated.
- background-clip, which is used to determine whether the background extends into the border or not.
- background-size, which gives you a way to specify the size of background images. You can specify this size in pixels, width and height, or in percentages.
- multiple backgrounds, which allows for multiple background images on one element.
- HSL colors, which allow you to use HSL (Hue, Saturation, Lightness) color values instead of RGB values.
- HSLA colors, which allows a fourth color value, which sets the Opacity (via the Alpha channel) of the element.
- opacity, which allows you to specify the opacity of the color in an element.
- RGBA colors, which allow you to specify an opacity value for an RGB color.
- text-shadow, which allows you to tell the browser how big to make, and how much blurring to use for, a shadow "behind" text.
- text-overflow, which allows you to tell the browser what kind of visual “hint” to give the user when text has been clipped.
-
text-transform, which allows you to change some of the features of the text
that it applies to. Some of these transforming property values are:
- uppercase -- Transforms all characters to uppercase.
- lowercase -- Transforms all characters to lowercase.
- capitalize -- Transforms the first character of each word to uppercase.
- none -- No capitalization. The text displays as it is. This is default.
- word-wrap, which allows long words to be broken and wrapped onto the next line.
- box-sizing, which lets you change the behavior of the browser in calculating the size of an element's box. By default, the browser calculates width and height by adding the border width and the padding to the size of the element's box. By setting this property to a value of border-box, you can force the browser to instead display the element's box with the specified width and height, and to add the border and padding inside the box.
- resize, which allows you to specify if a box is resizable.
- outline -- Outlines have been expanded in CSS3 to include the outline-offset property. This property allows the offset to be rendered away from the edge of the specified element.
- nav-up, nav-right, nav-down, nav-left -- These properties will indicate the element to which the focus is navigated to in response to directional navigation input, such as arrow keys.
- attribute selectors -- Three additional attribute selectors are provided for matching substrings in the value of an attribute.
- overflow-x, overflow-y, which allow content to be clipped on just one axis, meaning horizontally or vertically.
- content (Generated Content)
- media queries, which allow you to change stylesheets based on things such as the width and height of the viewport (display area). (Think about what this feature could mean for developing pages for smart phones!)
- multi-column layout, which allows you to let text be fitted into columns in one of two ways: by defining the width for each column, or by defining the number of columns.
- Web fonts -- The @font-face rule was first proposed for CSS2 and has been implemented in Internet Explorer since version 5! However, its implementation relied on the proprietary Embedded Open Type (.eot) format, and no other browsers decided to use this format. In CSS3 however, website makers can use any licensed TrueType (.ttf) or OpenType (.otf) font in their pages, as long as they are defined using this @font-face rule.
- speech -- The CSS3 Speech module removes some of the old properties that the current Working Draft of CSS2.1 had put into the media="speech" attribute, and adds new properties. All of them are now assigned to the speech media type. These properties are used for "styling" speech synthesis in aural user agents such as screen-reader browsers.
New Pseudo-Class Selectors
There are more pseudo-class selectors than just the ones that we will look at in the "Links" handout in a few weeks. (The ones specfically related to links are :link, :visited, :active, and :hover.)
The pseudo-class selectors which are new to CSS3 are:
- :first-child
- :first-of-type
- :last-of-type
- :only-of-type
- :only-child
- :nth-child(n)
- :nth-last-child(n)
- :nth-of-type(n)
- :nth-last-of-type(n)
- :last-child
- :root
- :empty
- :target
- :enabled
- :disabled
- :checked
- :not(selector)
- ::selection
The CSS Selectors reference page at W3Schools.com is a good place to find the details.
Further References
You can find some very nice free HTML5/CSS3 templates at http://www.zerotheme.com/1829/zrealestate-free-responsive-html5-template.html and other sites. Just do a Web search.
CSS3 Example
The following example will work properly only in Internet Explorer 9+, Chrome 17+, and Firefox 4+. You can see the example page running in your browser here... but you might need to upgrade your browser before you look at it!
This is page CSS3example.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS3 Example</title>
<link rel="stylesheet" type="text/css" href="CSS3style3.css" media="screen">
</head>
<body>
<div id="content">
<header>
<h2>CSS3 Example</h2>
</header>
<article>
<section id="disclaimer">
<h3>Disclaimer</h3>
<p>Don't take my word for it...</p>
</section>
<section id="examples">
<h3>Examples</h3>
<p>But here's how I would do it...</p>
</section>
<section id="closing_notes">
<h3>Closing Notes</h3>
<p>Well that was fun. I wonder if the spec will change next week?</p>
</section>
<aside class="LayeredEffect">
HTML5 and CSS3 are the future of Web pages.
</aside>
</article>
<footer>
<p>© Copyright 2014 by John Doe.</p>
</footer>
</div>
</body>
</html>
This is page CSS3style3.css:
body {
font-family: Arial, sans-serif;
font-size: 1.2em;
}
h2 {
text-shadow: 5px 5px 5px #AAA;
}
#content {
width: 600px;
margin: 0 auto;
border: 1px solid gray;
padding: 15px;
border-radius: 20px;
box-shadow: 10px 10px 10px gray;
}
article {
border: 1px solid gray;
border-radius: 10px;
margin: 15px 0;
position: relative;
padding: 15px;
}
header, footer {
border: 1px solid blue;
border-radius: 10px;
background-color: #AAF;
padding: 10px;
}
footer {
font-size: 0.8em;
}
aside {
padding: 10px;
background-color: #AFA;
border: 1px solid green;
position: absolute;
right: 15px;
top: 15px;
width: 200px;
font-size: 1.6rem;
}
.LayeredEffect {
border-radius: 10px;
box-shadow: 7px 7px 7px gray;
}
#disclaimer {
color: #00F;
}
#examples {
color: #0B0;
}
#closing_notes {
color: #F00;
text-transform: uppercase;
}
Please note these points about the above example:
- CSS3 features:
- The rounded corners;
- The "shadows";
-
The UPPER-CASE <h3> contents (courtesy of this declaration in
the #closing_notes selector:)
text-transform: uppercase;
Reading UPPERCASE TEXT
Please note this important information about reading UPPERCASE TEXT:
UPPERCASE TEXT is harder for experienced readers to read. You can use UPPERCASE TEXT for short segments of text, such as for headings or menu items, but NOT for meaningful text content. If you use UPPERCASE TEXT for meaningful text content, you remove important clues that your readers rely upon to read effectively.
If you doubt how much experienced readers rely upon these clues, you might want to look at these interesting sites:
dummies: "Recognizing the Shapes of Words"
You should note that we do, indeed, read UPPERCASE TEXT more slowly, but not for the reasons that most people say. Here is a brief excerpt from another article:
So, Is All Caps Harder to Read Then? We do actually read uppercase letters more slowly, but only because we don’t see them as often. Most of what we read is in mixed case, so we’re used to it. If you practice reading text in all capital letters you’ll eventually read that text as fast as you read mixed case. This doesn’t mean you should start using capital letters for all your text. Since peole are unused to reading that way, it will slow them down. And these days, text in all caps is perceived as "shouting".