Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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.


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:

  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.


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:

  • visibleAlthough the size of the element and its impact on the page layout doesn't change, the content overflows its element.
  • hiddenAny content which overflows its element is "clipped" and can't be seen.
  • scrollHorizontal 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.
  • autoThe 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.


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 {

h2 {

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.


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.


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.


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).


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:

  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:

  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.


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.

  • + Share This
  • 🔖 Save To Your Account

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive:

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020