- A Short History of Style for the Web
- What Is CSS?
- How Is CSS Used?
- Basic CSS Syntax
- The Basics of Selectors
- Basic Properties: Font Styles
- Inheritance
- Getting Specific: class and id Selectors
- Common Text Layout Properties
- Combinators: Descendant and Child Selectors
- Common Background Properties
- Dynamic Selectors
- Basic Page Layout
- Advanced Page Layout
- Positioning
- Advanced Selectors
- Display Types
- More Properties
- Media Types
- Importing Style Sheets
- Quality Assurance
- Specific Challenges and Techniques
Basic Page Layout
CSS1 introduced the fundamental model of page layout for CSS, along with a number of whitespace properties, and borders. CSS2 extended these properties, with a new model of positioning. We'll look at the basic CSS page layout properties here, and more advanced properties shortly. We also look at the issue of creating page layouts in significant detail in Chapter 9, so here we'll focus on the properties and their values.
Block vs. Inline Elements
In the last chapter, we saw that HTML has two kinds of elements: block elements, which may contain other block elements as well as inline elements; and inline elements, which may only contain other inline elements.
CSS has two main ways of displaying elements, block and inline, which are related to the HTML concepts of block and inline elements, but not exactly.
The CSS display property lets us specify different ways in which an element should be laid out on the page by the browser. The two main ways are referred to as display: block, and display: inline. But be careful, because although setting a display of block for an element like strong (which is an inline element according to the HTML specification) is quite valid, it does not change the rules of HTML—the strong element may still only contain other inline elements. So what does display do? It changes how the element is displayed in the page layout.
Elements with a display value of block create a new layout block on the page and cannot have content alongside them (unless they are given a float value, which we'll see shortly). Common examples of block elements are headings, and paragraphs. Figure 4.11 shows this in action.
4.11 Elements with a display of block, like the heading and paragraph, create new blocks on the page. Elements with a display of inline don't create a new block.
Elements with a display value of inline don't create a new block. Rather, the browser places them alongside the inline elements that come before and after it in the HTML document. Common examples include strong elements and anchors.
Changing the display property value of an element will change how the browser lays out the page, but it doesn't, as we said, change the rules of HTML. We'll look at some other aspects of the display property shortly.
The Box Model
When a browser lays out the elements in a document, each element gets its own box. The rules of how these boxes are laid out can get quite complicated, but for now we'll focus on the box of an element. It has four main components, as shown in figure 4.12: a margin, border, padding, and content.
4.12 The box model for an element
Working from the inside out, almost every element has content whose width and height can be determined in a couple of ways. It can be automatically calculated by the browser based on the actual contents (text, images, and so on), the width and height of the window, and other elements on the page. Or, it can be specified by the developer, using the width and height properties of CSS, which can take length or percentage values.
Next comes the padding of an element. This can be specified as either a length or percentage value, on all sides or individual sides of an element. As we saw earlier, the background of an element (image or color) fills the padding box of an element, as well as the content box, but that's as far as it goes. Outside the padding box lies the border, which is also part of the element's box. The border can be on all four sides of the element or one or more sides individually.
Lastly, outside the border, but still part of an element's box, is the margin, which too can be on all four sides of the element or on individual sides.
width
The width property specifies an explicit width for an element. We can use either percentage values (the element will be this percentage of the current width of its parent element) or length values. For example, we could specify that paragraphs are 40em wide like so:
p{ width: 40em } |
Specifying widths of elements in ems means that they remain the same number of characters wide regardless of the size users choose for their text. As we'll see in Chapter 9, this can help make content much easier to read.
However, there's a complication. Inline elements don't take an explicit width (or height). Specifying a width for them has no effect on the flow of the document. In fact it's slightly more complicated still. Some inline elements—specifically, "replaced" inline elements (most commonly images, but also embedded content like video)—can take a width and height that affect the line in which the element appears.
height
The height property specifies an explicit height for an element. height can be specified as length values such as pixels or ems, or as percentages, though percentage heights are tricky: a percentage height only has effect when the containing element for the element has an explicitly specified height. If the containing element's height is calculated automatically by the browser based on the element's content, or if it is specified as a percentage, percentage height won't work on the contained element. This means that centering elements vertically is very difficult. (We'll come back to this in Chapter 9.)
Overflowing Content
If we can specify the width and height of an element, what happens if the area of the element is not large enough to contain the contents of the element? The overflow property allows us to specify what happens in these situations. overflow can take the following keyword values:
- visible—Although the size of the element and its impact on the page layout doesn't change, the content overflows its element.
- hidden—Any content which overflows its element is "clipped" and can't be seen.
- scroll—Horizontal and vertical scroll bars are added to the element that enable scrolling to display all the content of the element. Note both scroll bars are shown even if scrolling is only required in one dimension and not the other, and even if the content doesn't overflow its element.
- auto—The browser will display scrollbars only for the dimensions that need them—which may mean no scrollbars are shown, or only a horizontal or vertical scrollbar, or both, depending on how the content overflows the element.
The advent of browsers like those found in the iPhone and Palm Pre, which don't feature scrollbars, means that you can't rely on scrollbars being displayed to allow access to overflowing content in all browsers.
margin
The margin of an element is the space between its border and the edge of adjacent elements' margins. We've seen that background colors and images aren't seen behind the margin—so in essence the margin is transparent.
We can specify the margin for an element, either on all sides or on individual sides, using length values like pixels and ems, as well as percentages. When we use ems, the margin width and height is proportional to the size of the font for that element as it is currently being displayed. This allows margins that grow and shrink as the user increases and decreases the font-size, thus maintaining the proportions of font-size to whitespace. Percentages specify horizontal and vertical margins as a percentage of the overall width of the element—including margin, padding, and border.
There are four margin-related properties: margin-top, margin-left, margin-bottom, and margin-right. We can also use just the shorthand margin.
The shorthand can take one, two, or four values separated by spaces. Where it takes a single value, this is the value for each margin—top, left, bottom, and right. Where it is two values, the first value is for the top and bottom margin, and the second value is for the left and right margin. Where it takes four values, we have to be a little more careful. The four values apply to the different edges in the following order—clockwise around the element—top, right, bottom, and left.
Other shorthands, padding in particular, work in similar ways.
Gotcha: Collapsing Margins!
One very confusing aspect of vertical margins is that the top and bottom margins of elements that adjoin so that the two margin edges touch will "collapse" to be not the combined height of the two margins, but the height of the larger of the two. Suppose we have an h2 element and immediately below it a paragraph element [4.13] with the following CSS:
p { margin-top:3em } h2 { margin-bottom:2em } |
4.13 Adjacent vertical margins
Logically, we'd expect the total space between the two to be 5em. But, because the margins collapse according to the CSS specification, the total space between them is just 3em, the larger of the two widths (if the two widths had been 2em, the width would collapse to 2em).
We revisit this issue in Chapter 7, where we look at various browser bugs, because in some situations, in some versions of Internet Explorer, margins that should collapse, don't.
border
Inside the margin of an element is its border. By default, elements have a border with no style, so in effect no border. Borders are specified using three properties: border-style, border-width, and border-color.
border-style
Browsers can draw borders in a number of different styles, specified with keywords. There are three one-dimensional (solid, dashed, and dotted), and four two-dimensional styles, which seem out of date and are very rarely used (groove, ridge, inset, and outset). The CSS3 properties box-shadow and border-image, both discussed in Chapter 13, essentially make the two-dimensional styles obsolete.
border-color
We can specify a border-color in the same ways in which we specify text and background colors—and like background colors, borders can be transparent. If no color is specified for a border, the border is the color of the text in the element (not black, as people often assume).
border-width
The third border property is border-width, and it can take three keywords—thin, medium, and thick—the exact widths of which aren't specified and can vary from browser to browser. border-width can also be specified using length values, but not percentages.
So, we could specify a .1em-wide solid red border for a paragraph with this CSS:
p{ border-style: solid; border-width: .1em; border-color: red } |
.1em is typically the smallest em value or increment that browsers recognize.
We can also use the border shorthand, with these three values in any order separated by spaces, for instance:
p{ border: .1em red solid } |
Individual Borders
Just as we can specify individual margins for each edge of an element, we can specify individual borders for each edge, with different styles, colors, and widths. It's also possible to specify borders on some but not all edges.
The only complicating factor is that there are a number of different shorthands for doing so. The longhand properties look like this:
- border-top-style
- border-top-color
- border-top-width
But, we can also use a single shorthand for each edge, which takes the three space-separated width properties, for example:
border-bottom: .2em groove blue |
We'll see in Chapter 13 that we can also specify that the corners of our borders be rounded, using the CSS3 border-radius property.
padding
The last of our box properties is padding, which is inside the border, and behind which background color and images are seen.
Padding is otherwise very much like the margins of an element. We can specify different padding on different edges, using percentages (as with margin, the width of both horizontal and vertical padding will then be a percentage of the overall width of the element), or length values. Padding can be specified using the padding properties padding-top, padding-bottom, padding-left, and padding-right, or using the padding shorthand, which works just as margin does, taking one, two, or four values. (Refer to the preceding section on margins for how these shorthands work.)
How Wide and Tall Is an Element?
It may seem obvious, but just how wide is an element with a width of 50 em? The logical but not necessarily correct answer is "50em." The width and height properties specify not the entire width and height of an element's box, but only the width and height of the content area of the element. So, the total width of an element is the width of any left and right margins, plus left and right padding, plus any left and right borders, plus the content width. Similarly, the total height is the content height, plus any top and bottom padding, plus any top and bottom border, plus any top and bottom margin.
So, we have to be very careful when we specify an explicit width or height, along with padding, margin, or borders. The height or width we set won't match the actual height or width of the element's box. We'll see in Chapter 7 how this is particularly problematic due to a bug in older versions of IE, where width does specify the total width of the element's box. CSS3 in fact has a property, box-sizing, supported in all contemporary browsers, including IE 8, which lets us specify which of these two models—width and height apply to the whole block, or only the content area—the browser should use to lay out a page.