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

Home > Articles > Design > Voices That Matter

This chapter is from the book Fixed positioning

Fixed positioning

Now we have got to the final part of the chapter, covering fixed positioning. position: fixed; positions elements in a similar way to position: absolute; taking them out of the document flow. The difference is that instead of positioning them relative to the nearest positioned ancestor, it positions them relative to the browser window, so they will always stay in the same position inside the window regardless of how much you scroll the page (Figure 21.10).

Figure 21.10: Fixed positioning fixes the position of an element relative to the browser window, so now.img02 will always stay in the same place, no matter how much you scroll the content.

Figure 21.11 shows a good use of fixed position—this is a screenshot of, the blog of designers Jon and Leigh Hicks. The right-hand navigation has been positioned using position: fixed;, so it will always stay in the same position. Check the page out yourself, and subscribe to the Hicks’s RSS feed—there’s some great content on it.

Figure 21.11: A good use of position: fixed;—

Let’s try to create something similar. I will take the last example and add a navigation menu on the right hand side that always stays in the same position when the page is scrolled. I will also fix the position of the header so it does the same thing. You can see the finished example by loading up fixed_positioning1.html from the code download at

Making initial changes to the HTML

To make this design work, I had to make a few changes to the HTML. I first put the header inside its own <div>, outside of the main content wrapper:

<div id="header">
  <h1>Simple fixed positioning</h1>

It needs to go outside as I want to scroll the rest of the content underneath this, and I want to stretch it over the whole width of the <body>. CSS is then used to get this the way I want it looking:

h1 {
   font-family: Futura, Helvetica, Arial, sans-serif;
   margin-top: 0;
   padding-top: 0.6em;
   padding-left: 0.6em;
   text-shadow: 2px 2px 2px #94d7e3;

#header {
   height: 100px;
   background-color: #00A6B9;
   width: 110%;
   margin-top: 0;

The next thing to do is add in the navigation menu, at the top of the main content:

<div id="navigation_menu">
      <li><a href="#">Home</a></li>
      <li><a href="#">Articles</a></li>
      <li><a href="#">Tour map</a></li>
      <li><a href="#">Photos</a></li>
      <li><a href="#">Contact</a></li>

Figure 21.12 shows how this currently looks.

Figure 21.12: Our new additions, near the beginning of the project.

I also added some extra content at the end of the page, to give you some more space to play with the scrolling effect.

Fixing the header and menu position

The next step is to fix the positions of the header and menu. This was done via the following rules:

#header {
   height: 100px;
   background-color: #00A6B9;
   width: 110%;
   margin-top: 0;
   position: fixed;

#navigation_menu {
  position: fixed;
  top: 5.5em;
  left: 36.5em;

There is not much to explain with the first rule—the #header rule has simply had a position: fixed; declaration added to it; we don’t want to change the position of it at all—we just want it to be fixed in place.

With the #navigation_menu, things are a bit more complicated. I’ve added position: fixed; to it, but I’ve also used top and left to make sure that it is positioned in the right-hand column.

This gets the elements in the position we want them, but there are a couple of problems (Figure 21.13). For a start, setting the header and navigation to have position: fixed; takes them out of the document flow, so the top of the content starts off hidden underneath the header! Second, when you start to scroll the content upwards, you’ll see that the images actually go over the top of the header!

Figure 21.13a: Our content starts off hidden.

Figure 21.13b: Now the images scroll over the top of the heading.

To start off, let’s tackle the content overlapping problem. This is easy to solve—I’ve added an ID of #first to the first paragraph of the content, and given it top padding to shift it downwards so it starts below the header:

#first {
   padding-top: 100px;

Now we’ll address the image problem. This brings us nicely on to the subject of z-index. “What on earth is z-index?” I hear you cry. Read on, brothers; read on, sisters...

Understanding z-index—the third dimension

As this is a pretty advanced topic that you’ll only really run into when dealing with positioning, it is only natural to cover it near the end of the book. If you thought that HTML and CSS web pages were two-dimensional, then you wouldn’t be completely accurate. In fact, different elements sit at different depths to one another, closer and farther away from your nose as you look at the screen; the z-axis, as it were. We are still talking about flat bits of content, but the order in which they’re put on the screen (the source order in the HTML) determines which overlaps which by default.

To explain this in a bit more detail, positioned elements (including relatively positioned elements) are rendered within something known as a stacking context. Elements within a stacking context have the same point of reference along the z-axis. You can change the position along the z-axis—also known as z-position or stack level—of a positioned element using the z-index property.

z-index takes a positive or negative integer as a value—the higher the integer, the closer the element to your nose. The default value is auto, which means the element will have the same stack level as its parent. You should note that you can only specify an index position along the z-axis—you can’t make one element appear 19 pixels behind or 5 centimetres in front of another! Think of it like a deck of cards: you can stack the cards and decide that the ace of spades should be on top of the three of diamonds.

Setting a really high z-index value will not cause your web pages to poke your site visitors in the eye.

If you specify the z-index as a positive integer, you assign it a stack level in front of other elements within the same stacking context that have a lower stack level. A negative integer value for z-index assigns a stack level behind the parent’s stack level. When two elements in the same stacking context have the same stack level, the one that occurs later in the source code will appear on top of its preceding siblings. The browser draws the elements on the page starting from the one with the lowest z-index, going up to the one with the highest z-index.

There can in fact be up to seven layers in one stacking context, and as many different z-indices in those layers as you want, but don’t worry—you are unlikely to have to deal with seven layers in a stacking context. The order in which the elements (all elements, not only the positioned ones) within one stacking context are rendered, from back to front, is as follows:

  1. The background and borders of the elements that form the stacking context.
  2. Positioned descendants with negative stack levels.
  3. Block level descendants in the normal flow.
  4. Floated descendants.
  5. Inline level descendants in the normal flow.
  6. Positioned descendants with the stack level set as auto or 0 (zero).
  7. Positioned descendants with positive stack levels.

The bold entries are the elements whose stack level we can change using the z-index property.

Going back to our example, to cause our images to behave and stay behind the header, we just need to set a higher z-index value on the header:

#header {
   height: 100px;
   background-color: #00a6b9;
   width: 110%;
   margin-top: 0;
   position: fixed;
   z-index: 3;

This has solved our problems (Figure 21.14).

Figure 21.14: Now our example is behaving itself.

Adding extra refinements

There are a couple of other features in the code that I haven’t yet covered, so let’s go over these now. First, I have given two of the navigation menu list items a class of shifted:

   <li><a href="#">Home</a></li>
   <li class="shifted"><a href="#">Articles</a></li>
   <li><a href="#">Tour map</a></li>
   <li class="shifted"><a href="#">Photos</a></li>
   <li><a href="#">Contact</a></li>

I then apply a rule to these to slightly displace them to the left, using relative positioning:

.shifted {
  position: relative;
  right: 10px;

I’ve also decided to style the menu items a bit more by removing the bullets and colouring them a silvery-gray colour:

#navigation_menu li a {
     color: gray;

#navigation_menu li {
   list-style-type: none;

This creates the effect seen in Figure 21.15. As I’ve mentioned a couple of times now in the chapter, this combination of positioning inside positioning can be used to create some very precise irregular layout/design features. As always, the best way to find out what is possible is to experiment with these techniques!

Figure 21.15: A jaunty navigation.

The second is somewhat of a hidden treasure. To find it, try narrowing the browser window slowly but surely. If you are using Firefox, Opera or Safari, eventually you’ll see the change documented in Figure 21.16.

Figure 21.16: Magic shifting menu action.

This is due to the fact that I have implemented a CSS3 media query at the bottom of my CSS:

@media all and (max-width: 760px) {

  #navigation_menu {
    top: 2.6em;
    left: 0em;
    z-index: 4;
    min-width: 600px;

    #header {
    min-width: 600px;
  #navigation_menu li {
     display: inline;

  #navigation_menu li a {
     color: grey;
  .shifted {
    right: 0px;


This is a CSS3 feature that allows you to selectively apply CSS rules depending on whether a certain criterion is met. In this case, the above block says “If the browser width goes below 760 pixels, apply the rules inside the @media block.”

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