Nothing is for sale here. Freewill tips keep the site running. Want to help? → Tip via Paypal

CSS Positioning

One of the drawbacks in the early days of web design was the difficulty in positioning elements precisely where you wanted them. This problem was overcome with CSS positioning.

Keep in mind when positioning elements you are working with the box model. There are three positioning schemes:

Normal flow Normal flow (how the page is drawn by default) includes block formatting of block boxes, inline formatting of inline boxes, relative positioning of block and inline boxes, and positioning of compact and run-in boxes.
Absolute In the absolute positioning model, a box is removed from the normal flow entirely and positioned with respect to a containing block. If an absolute positioned element has no containing block it uses the document body, and moves along with the page scrolling.
Floats In the float model, a box is first laid out according to the normal flow, then taken out of the flow and shifted to the left or right as far as possible, depending on which side you choose to float it. Content will flow along the side of a float as long as there's enough space available.

Don't worry if you don't understand all that yet, things should clear up by the end of this tutorial. If not, send me a dollar and I promise I won't tell anyone.

The first decision is to choose a positioning scheme. If you don't want the text and other content to flow alongside the positioned element, choose either a normal flow position or absolute position. Choose the float scheme if you do want text and other content to flow beside the positioned element.

This tutorial covers normal flow and absolute positioning. Floating elements are covered in the floats tutorial because it's coded in a different way and, my dumb jokes aside, it also helps to keep this tutorial as concise as possible.

There are two parts to CSS positioning. The first part specifies which type of positioning you want to use. The possible values are listed below:

relative | absolute | fixed | static | inherit

To establish the type of positioning to be used, the first part of the code is written like this:

position: relative;

If you want absolute positioning instead you'd type absolute in place of relative in the code, or substitute whichever positioning type you want. Here's what each of those values mean:

relative
This value positions an element relative to its default position (where it would normally be positioned in the content flow).
absolute
This value uses numerical coordinates to position an element relative to the parent element (or the document body if there is no parent element).
fixed
This value also uses numerical coordinates to position an element, only the element doesn’t scroll with the page, it stays "fixed" in place.
static
This is the default setting. It allows browsers to place elements in the flow of content as they appear in the source code.
inherit
With this value an element inherits the position type from its parent element.

The other part of positioning an element using CSS positioning to name the coordinates where it will be positioned. Let's take a look at a few examples.

Relative Positioning

The above "Relative Positioning" heading uses ... wait for it ... relative positioning! Shocking, I know. Normally that heading would be positioned in line on the left with the rest of the content. I simply moved the "Relative Positioning" heading 30 pixels to the right. Here's the CSS code I used:

<h4 style="position: relative;
left: 30px;">Relative Positioning
max-width: 200px;</h4>

Notice the code reads "left" and the heading moved right. That's not a typo. It means your adding the distance to the left side of the element. If I coded top: 40px it would add 40 pixels to the top of the element, meaning it would push the element 40 pixels down the page. It may seem a little bit counterintuitive but that's the way it works.

I added the max-width: 200px because without it, the heading created a short horizontal scroll bar on small screens. If you use a responsive design and have the same problem with a positioned element, now you know how to fix it.

Here are the directional values you can use:

left | right | top | bottom

Instead of left I could have moved the position right, up, or down. Quite often an element will be positioned using two coordinates rather than just one as I just + did. The previous example was an inline style. Here’s how to code two coordinates in an embedded or external style sheet:

h4 {position: relative;
      left: 50px;
      top: 20px;}

To use two coordinates, use either top or bottom, and either left or right. Obviously you can't use conflicting values like up and down or you'll cause a traffic jam in Idaho.

Note that setting a relative position property causes it to be moved from its normal position. Other content will not be moved to fill the gap, nor will other content move out of the way. If you position one element where other content sits, they will overlap.

A positive integer moves the element in the opposite direction of the named coordinate. A negative integer moves the element in the same direction of the named coordinate. Use a large enough negative number and you can move elements off the page.

If you're thinking of using negative positioning to enhance your page's search engine optimization, don't. The search engines caught on to this trick long ago. Some old advice still says it works, but it doesn't. The search engines have staff that check for this and other well-known tricks.

Absolute Positioning

1
2
The green box (1) is uses a relative position. The gold box (2) uses absolution positioning. Notice that the position of the gold box is measured from its parent element (the green box). This example is actually coded into the page, it's not an image.

While relative positioning is measured from where an element would be in the normal flow of content, absolute positioning is measured from edge or edges named. So with the gold box, it's positioned 5 pixels from the right side and 10 pixels from the bottom because those were the named positions.

Here’s the code for those two boxes:

<div style="position: relative; 
                    max-width: 180px; 
                    height: 140px; 
                    background-color: #cad6b6; 
                    border: 5px solid #607241;">
1
<div style="position: absolute; 
                    right: 5px; 
                    bottom: 10px; 
                    width: 100px; 
                    height: 50px; 
                    background-color: #f3d383; 
                    border: 2px solid #c7a398;">
2
</div></div>

Note that the first division was left open as I added the second division, then closed them both at the end. That's how the parent element was established. It's simply one division nested inside another.

1
2
Now watch what happens if I give box 2 negative values.

That's not an image either. It's the exact same code as before, except I used negative coordinates, -25 for the right coordinate and -10 for the bottom coordinate.

As you can see, it's possible to "stack" elements using positioning. You can also stack elements using the z-index property, which is explained in this stacking elements tutorial.

Fixed Positioning

A fixed position means an element is “fixed” in place—in other words, it stays where you put it and doesn’t scroll with the rest of the page.

You can see an example of this with the corner banner in the upper right-hand corner of the page. You can see it stays there even when you scroll. I'll show you the code, but realize that depending on your page layout, it doesn't always work as you expect.

Sometimes an image may not be positioned as you might expect, depending on what you have going on with your site design. Things like elements with overflow: hidden; can affect the positioning. You can try to track down what is keeping it from going where you coded it to go, which might mess up your layout if you change it, or you can try moving the element closer to where you want it positioned. That can place it outside of the code that is preventing it from executing like you expect.

There's a lot going on with this design, which caused the problem I just outlined. I just moved the image code from the bottom of the page to just after my top menu. Problem solved. Beats spending hours trying to figure out what the hang up is. Here's the code for that:

<img src="images/corner-banner.png"
style="max-width: 30vw; position: fixed; top: 0; right: 0; z-index: 9999">

The max-width: 30vw is so the banner image would scale down on smaller screen sizes. The "vw" is short for viewport width. The "30" is for 30%. The whole thing means the maximum width the image can be is 30% of the screen of the viewing device. The makes it scale down on smaller screens, and limits how big it can be on larger screens.

NOTE

If you like the corner banner, there's also a corner ribbon tutorial that essentially does the same thing, except is all done with CSS—no images are used. Handy for those without image creating skills. It's also great for re-using just by changing the text.

Sticky Positioning

Sticky is like a combination of relative and fixed positioning. You can code an element and it will scroll with the page like a relative positioned element, but when the scrolling reaches a point you decide, the element sticks in that place.

As you scroll down this element you'll come to a division labeled "sticky" in bold text. As that hits the top of this element, it should stick in place at the top of the window, while the rest of the content keeps scrolling.

Sticky

Here's some more text to show how the "sticky" division stuck to the top of the element while this text just scrolls on by.

Other than that, I have nothing else to say. Wow, that doesn't happen often!

I'm going to rattle on a little bit more though so there's enough content, such as it is, to give a good show of the sticky division being stuck.

That should do it.

Neat trick, eh? Here's the basic code:

div.sticky { position: -webkit-sticky; position: sticky; top: 0;}


The webkit line is for older browsers. The top: 0 is where the sticky division is to stop. You could change that and have it stop at a different position if you had a good reason for it. I guess you could do it if you had a bad reason, too.

OK, you're sounding a little wild with your wanting to do things for bad reasons, so I'm going to end this madness right here.