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

Home > Articles > Web Design & Development > CSS

Eric Meyer on CSS: Style at Dawn

📄 Contents

  1. Eric Meyer on CSS: Style at Dawn
  2. About This Article
It's easy to take two documents and add the CSS styles necessary to create translucent effects using ordinary JPEG images. These images make it fairly easy to create an attractive design.
Like this article? We recommend

Like this article? We recommend

Eric Meyer on CSS: Style at Dawn

We'll take the first of two documents and add the styles necessary to create translucent effects using ordinary JPEG images. As we'll soon see, the images in question make it fairly easy to create an attractive design.

Getting Started

As usual, our first step should be to look at the structure of the document. As we can see from Listing 1, there isn't a whole lot to it really, just a masthead div with a heading and a "main" div containing the text of the entry. Without any styles, we get the very plain rendering shown in Figure 1.

Figure 1Figure 1 The journal entry in its unstyled state.

Listing 1. The Basic Document Structure

<div id="masthead">
 <h1>Mourning in Mansfield</h1>
</div>
<div id="main">
 <h2>17 May 2003</h2>
 [...entry text...]
</div>

Now, with nothing more than what we already have in the document and some background images, we need to create a tasteful design that makes use of translucent effects. To aid us in this goal, we have the three images shown in Figure 2: a basic image (morn-base.jpg), a faded version of that same image (morn-fade.jpg), and a washed-out version (morn-wash.jpg).

Figure 2Figure 2 The three background images we have at our disposal.

To lay the groundwork, the first thing we want to do is strip the "gutter" from the body element itself. We'll do that with a familiar rule:

<style type="text/css">
body {margin: 0; padding: 0;}
</style>

The next thing to do is drop an image into the body's background. It's important to choose the right one since the background color will have to match the background image, and the body background is the one that will be seen throughout most of the design. For maximum readability, we'll pick the washed-out version of the image for the body. A quick check with a color-sampling tool reveals that the background value is a shade of gray that's 71% white, so we'll do the following:

body {margin: 0; padding: 0;
 background: rgb(71%,71%,71%) url(morn-wash.jpg);}

That's a start, but we'll need more. As this rule stands, the image will start out in the upper-left corner of the body's background and will tile infinitely both horizontally and vertically. For our design, we want the image to appear once and not repeat, and we want to place it in the top-right corner. We can do that by adding just a bit more to the background declaration, with the result shown in Figure 3.

body {margin: 0; padding: 0;
 background: rgb(71%,71%,71%) url(morn-wash.jpg) 100% 0 no-repeat;}

NOTE

Words Instead of Numbers

We could have used the keywords right top instead of the values 100% 0 for the background position, but since later backgrounds will be positioned using offsets, we're going to avoid the use of keywords throughout this project.

Figure 3Figure 3 Starting out with some basic styles for the body element.

Masthead Styles

The document's masthead is next to fall under our scrutiny. We'll apply the basic background image (morn-base.jpg) to the masthead itself, but let's consider for a moment exactly how.

Our aim is to have the background images line up with each other to create the illusion of translucency. To make that happen, we'll need to make sure the masthead's top edge is lined up with the body's top edge. We'll also need to get the background image to not repeat and to sit in exactly the same place as the body's. Thus, the values 100% 0 and no-repeat are going to make another appearance. A quick run through a color sampler gives us the background color we need to match the image.

body {margin: 0; padding: 0;
 background: rgb(71%,71%,71%) url(morn-wash.jpg) 100% 0 no-repeat;}
#masthead {background: rgb(2%,4%,4%) url(morn-base.jpg)
 100% 0 no-repeat;}
</style>

There is one more thing that needs to be done: We need to take the margins off the h1 element inside the masthead div. If we don't, the margins will actually stick out of the div in CSS-conformant browsers, which will push the top edge of the div down from the top of the document. By removing the margins, we get the result shown in Figure 4.

#masthead {background: rgb(2%,4%,4%) url(morn-base.jpg)
 100% 0 no-repeat;}
#masthead h1 {margin: 0;}
</style>

Figure 4Figure 4 A different background image is added to the masthead.

Thanks to the masthead and body lining up along the top edge, their backgrounds do the same thing, which gives us exactly the kind of effect we wanted. Although the masthead's background is actually "on top" of the body's, it looks as if there is a grayish translucent layer between us and the "page background." It's an illusion, but a very useful one!

Of course, we can't leave things as they are. The heading text is now basically black on a black background, so it isn't very readable. Let's change its color to a light color that reflects the "sunrise" theme and update its font styling to look a little more professional.

#masthead h1 {margin: 0;
 color: #EED;
 font: small-caps bold 2em/1em Arial, sans-serif;}
</style>

The font-size and line-height values were actually chosen with some care. By setting the font-size of the h1 to 2em, we've made the text twice as big as its parent element, the #masthead div, which inherits its font-size value from its parent, the body. With the line-height value of 1em, we've defined the height of the h1's content to be exactly equal to its font-size.

NOTE

Default Line Heights

Why bother with the explicit 1em value for line-height? Because that isn't the default—most browsers default to a line-height value somewhere around 1.2. That makes the height of each line a little taller than the font-size value. Our 1em value overrides that default behavior, which will come in handy later on.

Actually, now that we've cleaned up the masthead text, it might be nice to increase the height of the masthead by dropping some top padding on the masthead div.

#masthead {padding: 2.5em 0 0;
 background: rgb(2%,4%,4%) url(morn-base.jpg) 100% 0 no-repeat;}

By doing this, we're basically wedging a space between the top border of the masthead div and the top margin edge of the h1. By making the top padding value 2.5em, we allow the spacing to stay in proportion to any changes in text size, no matter how they happen.

With this done, we can see a lot of improvement in Figure 5, although we aren't home free just yet.

Figure 5Figure 5 Padding the masthead and styling the heading bring drastic improvements.

The light text is now on a background that's mixed light and dark, which will make the text very difficult to read no matter what color we assign. We could put the text over to the right side so that it's more over the light portion of the background, but the dark tree will still prevent us from changing the text to be dark.

What we need is a background for the heading itself that's more consistent, and fortunately, we already have one: morn-fade.jpg. We can add that to the background of the h1 element.

#masthead h1 {margin: 0;
 background: rgb(4%,4%,4%) url(morn-fade.jpg) 100% 0 no-repeat;
 color: #EED;
 font: small-caps bold 2em/1em Arial, sans-serif;}

Since we now have a darker background for our light text, we can actually shift it over to the right side of the design. At the same time, we'll pad out the h1 element a little bit so that the text doesn't get too close to the edge of the dark area.

#masthead h1 {margin: 0; padding: 0.25em 0.33em;
 background: rgb(4%,4%,4%) url(morn-fade.jpg) 100% 0 no-repeat;
 color: #EED;
 font: small-caps bold 2em/1em Arial, sans-serif;
 text-align: right;}

The quarter-em top and bottom padding will nicely center the text in the dark background, and the third-em padding on the right and left will keep the title text from getting too cozy with the edge of the browser window, as illustrated in Figure 6.

Figure 6Figure 6 The third background is added, although it's out of alignment.

Whoops—there's a problem. The background image we just added isn't lining up with the other images, and this is creating a discontinuous tree. That's because the top edge of the h1's background area isn't lined up with the tops of the other two.

We could fix this by moving the h1 to the top of the page, but that wouldn't look very good. Instead, let's bring the h1's background into alignment with the others but leave the element where it is now. To pull this off, we'll have to do a little math.

We know that the h1's top edge is 2.5em from the top of the masthead, thanks to the top padding we gave the masthead div. However, the h1's font-size is twice that of the masthead div's, due to the 2em value in the font declaration, so we have to divide that number in half to get the right offset. Therefore, if we position the background image of the h1 so that its top edge is actually [ms]1.25em above the top edge of the h1's background area, then all the backgrounds should line up.

#masthead h1 {margin: 0; padding: 0.25em 0.33em;
 background: rgb(4%,4%,4%) url(morn-fade.jpg) 100% -1.25em no-repeat;
 color: #EED;
 font: small-caps bold 2em/1em Arial, sans-serif;
 text-align: right;}

One more touch should make the masthead look more polished, and that's a solid black border along the top and bottom of the dark background. However, doing this means that the background area of the h1 will be effectively shifted downward by a pixel (thanks to the top border), which will cause the images to go out of alignment in CSS-conformant browsers. We can fix that with a negative one-pixel top margin.

#masthead h1 {margin: -1px 0 0; padding: 0.25em 0.33em;
 background: rgb(4%,4%,4%) url(morn-fade.jpg) 100% -1.25em no-repeat;
 color: #EED; border: 1px solid black; border-width: 1px 0;
 font: small-caps bold 2em/1em Arial, sans-serif;
 text-align: right;}

There's just one problem: IE/Win goes completely bonkers when the negative margin is introduced. So we'll actually need to change the margin back to what it was, and use another of IE's bugs against it to hide the negative margin.

#masthead h1 {margin: 0; padding: 0.25em 0.33em;
 background: rgb(4%,4%,4%) url(morn-fade.jpg) 100% -1.25em no-repeat;
 color: #EED; border: 1px solid black; border-width: 1px 0;
 font: small-caps bold 2em/1em Arial, sans-serif;
 text-align: right;}
html>body #masthead h1 {margin: -1px 0 0;}
</style>

Thanks to the html>body part of that rule, Explorer will just skip right over the new rule. 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.

Although these new styles may be off by a pixel in some browsers, it's an acceptable price to pay. The difference will hardly be noticeable one way or the other, as Figure 7 illustrates.

Figure 7Figure 7 With a little negative positioning, the backgrounds are lined up nicely.

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