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

Home > Articles > Web Design & Development > CSS

This chapter is from the book

This chapter is from the book

Beached Styles

In this half of the project, we'll take a new document with a slightly different structure and give it a translucent-effect makeover. We'll use the principles explored in the first half of the project, but we'll use them in a more sophisticated way because in this document none of the elements will line up along the top edge.

Assessing Structure and Style

As always, our first order of business is to scout out the lay of the land, so to speak, by examining the document structure and any styles that may already exist. The basic structure looks like this:

<div id="main">
  <h1>Gathering Stormclouds</h1>
  <div id="content">
    [...content...]
  </div>
</div>

As it happens, there's already a basic style sheet embedded with the document, and this leads to the result shown in Figure 4.9.

<style type="text/css">
body {margin: 0; padding: 0;
  background: rgb(14%,26%,30%);}
#main {width: 600px; margin: 2em auto;
  border: 3px solid black;}
</style>

NOTE: Auto-Margins

By giving the main div's left and right margins the value auto and the width an explicit value, we've centered the div within its parent. Note that this will not work in versions of IE/Win before IE6 that improperly centered elements using text-align: center.

Figure 4.9Figure 4.9 Basic, if dark, beginnings for our new document.

Of course, we'll be filling in other backgrounds, so the text will be more legible by the time we're done. We've just set up these styles to start so that we can dive into the background positioning.

Now we need to use each of the three images shown in Figure 4.10: a basic image (storm-base.jpg), a faded version of that same image (storm-fade.jpg), and a washed-out version (storm-wash.jpg).

Figure 4.10Figure 4.10 The background images we have at our disposal.

For this design, we aren't going to apply any of these images to the body element. Instead, we'll use the main div and some of its descendants, but first let's style the text and place the elements.

Title Styles

The first thing we ought to do in setting up text and element styles is temporarily give the main div a background color. This will make it easier to see what we're doing.

#main {width: 600px; margin: 2em auto;
  border: 3px solid black;
  background: gray;}  /* temporary background */

Now we can get down to design. For the title of the page, we'll give it some size and height styles copied from the layout in the first half of the project.

#main {width: 600px; margin: 2em auto;
  border: 3px solid black;
  background: gray;}
#main h1 {font: 2em/1em "Times New Roman", serif;}
</style>

NOTE: Quoted Fonts

It's only necessary to quote font names if they contain spaces or non-alphabetic characters.

This will, as before, set the h1's text in relation to its parent (in this case, div#main) and make sure its line-height is exactly equal to its font-size. We'll also center and lowercase the text and spread out the letters just a bit, as demonstrated by Figure 4.11.

#main h1 {font: 2em/1em "Times New Roman", serif; letter-spacing: 0.1em;
  text-transform: lowercase; text-align: center;}

Figure 4.11Figure 4.11 A temporary background helps us see the changes to the page title.

Having the title all jammed up against the edges of the box feels a little claustrophobic, so let's add some margins to the h1.

#main h1 {font: 2em/1em "Times New Roman", serif; letter-spacing: 0.1em;
  text-transform: lowercase; text-align: center;
  margin: 1.25em 1em 0;}

To give some definition to the area of the design that holds the text, it might look kind of cool to add some thin borders around the title and the content. We'll start with the title but have borders appear only on the top, right, and left sides.

#main h1 {font: 2em/1em "Times New Roman", serif; letter-spacing: 0.1em;
  text-transform: lowercase; text-align: center;
  margin: 1.25em 1em 0;
  border: 1px solid black; border-bottom: none;}

Of course, this means that the title text is up against a border again, so we'll add in some padding.

#main h1 {font: 2em/1em "Times New Roman", serif; letter-spacing: 0.1em;
  text-transform: lowercase; text-align: center;
  margin: 1.25em 1em 0; padding: 0.5em 0.25em;
  border: 1px solid black; border-bottom: none;}

You may have noticed that we're sticking to margin and padding values that are evenly divisible by 0.25. This will make it much easier to do the math that's looming near the end of the project. In the meantime, let's see where we are now (see Figure 4.12).

Figure 4.12Figure 4.12 The addition of margins, padding, and a border makes the title stand out.

Content Styles

Bringing the main content of the page into line with the title shouldn't be too difficult. The first step is simple enough: We'll add a border to the content div that encloses all the text.

#main h1 {font: 2em/1em "Times New Roman", serif; letter-spacing: 0.1em;
  text-transform: lowercase; text-align: center;
  margin: 1.25em 1em 0; padding: 0.5em 0.25em;
  border: 1px solid black; border-bottom: none;}
#content {border: 1px solid black;}
</style>

That isn't sufficient, of course. If we left things there, the content div would be as wide as the main div, and the edges wouldn't line up with the edges of the title. Thus, we'll need to give the content div some margins. But how big?

Well, the h1 was given a top margin 1.25em tall and side margins 1em wide. Recall, however, that the font-size value of the h1 is 2em. Since its em-based margins are calculated with respect to its calculated font-size, this means that we'll need to double the values for margins on our content div. That gives us 2em right and left margins and a 2.5em bottom margin. Since we want the content border right up against the title, that means no top margin.

#content {margin: 0 2em 2.5em;
  border: 1px solid black;}

As with the title, we don't want the content text getting too close to the borders around it. We could double the padding used on the title, but instead, let's give the content div a generous padding of an em and a half.

#content {margin: 0 2em 3em; padding: 1.5em;
  border: 1px solid black;}

Finally, let's add some styling to the content text. The first thing is to take the top margin off of all the paragraphs; this will ensure that the first paragraph is as close to the top of the content div as the div's padding will allow while still keeping all paragraphs separated by an em. We'll also fully justify the text, as seen in Figure 4.13.

#content {margin: 0 2em 3em; padding: 1.5em;
  border: 1px solid black;}
#content p {margin: 0 0 1em; text-align: justify;}
</style>

At this point, we've done enough that we can start adding in the background images. So let's get started!

Figure 4.13Figure 4.13 The main content integrates with the title.

Adding the Backgrounds

Looking at the design in Figure 4.13 and the images we have available, let's take this approach: The main div will get the basic image (storm-base.jpg), the title will get the washed-out image, and the content div will get the faded image. This will necessitate changing the text color to something light, but we'll get to that in a minute. First, here's the main div's background.

#main {width: 600px; margin: 2em auto;
  border: 3px solid black;
  background: rgb(7%,13%,15%) url(storm-base.jpg) 0 0 no-repeat;}

Next, we'll add a background to the title.

#main h1 {font: 2em/1em "Times New Roman", serif; letter-spacing: 0.1em;
  text-transform: lowercase; text-align: center;
  margin: 1.25em 1em 0; padding: 0.5em 0.25em;
  border: 1px solid black; border-bottom: none;
  background: url(storm-wash.jpg) 0 0 no-repeat;}

Finally, the content div gets its background.

#content {margin: 0 2em 2.5em; padding: 1.5em;
  border: 1px solid black;
  background: rgb(4%,8%,9%) url(storm-fade.jpg) 0 0 no-repeat;}

NOTE: Top Left Zeroes

The background position value 0 0 is equivalent to top left. We're using numbers here because we're going to have to change some of them to negative values, and it isn't possible to combine keywords and numbers. Thus, something like left 50px is forbidden in CSS, but 0 50px is not. (This restriction was removed in CSS2.1, but some browsers still obey the restriction.)

Alert readers will already have caught the problem: All of these elements set the background to start in their upper-left corner, but the elements don't line up. This means the backgrounds won't line up either, as we can see in Figure 4.14.

Figure 4.14Figure 4.14 The backgrounds make their appearance, but they don't line up yet.

So now we have to figure out the offsets needed to get the background images to line up. The main div can be left alone, of course, so we'll work on the title first. It's actually pretty simple. We know that it has a top margin of 1.25em and a left margin of 1em, so those are the offsets we'll use because the em for margins is the same em that's used for background offsets.

#main h1 {font: 2em/1em "Times New Roman", serif; letter-spacing: 0.1em;
  text-transform: lowercase; text-align: center;
  margin: 1.25em 1em 0; padding: 0.5em 0.25em;
  border: 1px solid black; border-bottom: none;
  background: url(storm-wash.jpg) -1em -1.25em no-repeat;}

For the content div, the situation is a bit more complicated. The horizontal offset is easy: It's 2em, the same as the left margin. For the vertical, though, we have to figure out how far it is from the top of the content div to the top of the main div.

NOTE: Position and Order

We didn't get those backwards: background-position length and percentage values always put the horizontal offset before the vertical offset. Thus, -1em -1.25em offsets the image 1em to the left and 1.25em upwards. With keywords, the order is less important; top left and left top are equivalent, although the latter can trip bugs in the Netscape 6.x line.

Most of that distance is occupied by the h1 element, so let's figure it out first. Within its own frame of reference, the content of the h1 is effectively 1em tall, and it has top and bottom padding of 0.5em. It also has a top margin of 1.25em (and no bottom margin), and all that together adds up to 3.25em tall. We have to double that, remember, because of the doubled font-size of the h1. Therefore, in terms of the content div's ems, the h1 is 6.5em tall. With that information and a light red color in hand, we can update the rule appropriately.

#content {margin: 0 2em 2.5em; padding: 1.5em;
  border: 1px solid black;
  color: rgb(210,185,150);
  background: rgb(4%,8%,9%) url(storm-fade.jpg) -2em -6.5em no-repeat;}

That should do it—or should it? We've forgotten one thing: the top borders of the h1 element and the content div, which cause a 2-pixel push downward for the background area of the content div. In fact, the h1's background is also offset by a pixel, but we can let it slide since it isn't nearly as noticeable.

As it turns out, Explorer has forgotten those two pixels as well. Thanks to the way it places background images, the backgrounds already line up. So, if we were to change the offset to fix alignment in CSS-conforming browsers, the alignment would be thrown off in Explorer. So here's what we'll do: We'll leave the rule as it is and add in a new one that Explorer won't understand but more current browsers will.

#content {margin: 0 2em 2.5em; padding: 1.5em;
  border: 1px solid black;
  color: rgb(210,185,150);
  background: rgb(4%,8%,9%) url(storm-fade.jpg) -2em -6.5em no-repeat;}
html>body #content {margin-top: -2px;}
#content p {margin: 0 0 1em; text-align: justify;}

As in the previous half of the project, Explorer will just skip right over the rule we've just added. Other browsers, such as Safari, Opera, and Gecko-based browsers like Mozilla, will understand and apply the rule. Thus, we get a good cross-browser layout, as shown in Figure 4.15.

Figure 4.15Figure 4.15 The backgrounds are now lined up quite nicely.

Pros and Cons

As powerful as this technique can be visually, it does make for pages that are weightier than they might otherwise be. In addition to the HTML and CSS, designs like this also require the loading of multiple backgrounds, most of which will only be seen in part. Thus, some of the bandwidth required for the page to download might be considered wasted since it contains image portions that the user will not see.

This is admittedly a drawback and one worth considering. In the examples used for this project, the background images were not too large, plus the faded and washed-out versions were highly compressed, which kept the weight down. A design that relied on five variations of a background image, with each one 800x600 or bigger, would create a monstrously weighty page!

Downloading full images and placing them as we did in this project carries a benefit: If the text in the document is resized, the translucency effects will still hold true (that is, unless text starts wrapping in unexpected ways). For example, in the "stormclouds" example, if the title text wrapped to two or more lines, the alignment of the content div would be thrown off quite a bit. We could in theory avoid the problem with fixed-attachment backgrounds, but Explorer for Windows doesn't support such techniques.

NOTE: Fixed-Background Layout

For more information on fixed-attachment layout techniques, see Project 12 of Eric Meyer on CSS.

We could also get around the alignment problems caused by wrapping text with a simple change in structure. Instead of putting the content div after the h1, we could put the h1 inside the content div. This, along with a suitable rearrangement of the CSS, would prevent misalignment if the title text ends up wrapping to multiple lines.

A much simpler and less weighty approach, at least in cases in which you want to lighten or darken a background, is to use semi-opaque PNG graphics for the elements that need to be faded or lightened. The problem, again, is Explorer for Windows, which supports PNG images but not the alpha channel, which wrecks the translucency. Instead, Explorer just shows a fully opaque version of the PNG. It's also the case that you can really only achieve lightening or darkening effects with PNGs, which limit the range of visual effects. With the offset-alignment approach we've discussed, you could use a basic image, one with the image contours traced, and another with a rippled-glass effect.

At any rate, it's important to employ the techniques explored in this project with care. They are most often useful in situations such as mastheads, section titles, or other places where the content is short and the area to be decorated is both wide and short.

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.

Overview


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.

Surveys

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.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

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.

Security


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

Children


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

Marketing


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 customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


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: www.peachpit.com/u.aspx.

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 NevadaDesignatedRequest@pearson.com.

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.

Links


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