Aim and shoot - Positioning with CSS

February 28th 2016

Introduction to CSS Positioning

Whereas HTML is a powerful language to structure content in a hierarchical way, CSS helps extremely well in styling the page to a visually appealing document. One of the most impactful css properties is position:. This attribute let's you define where on a page you want to place on the page. There are four types of positioning, which I will discuss below.

Bubble Shooter CSS Positioning

1. position: static;

By default, css will try to position any element in the top left corner of the web browser, and stack one element next or below the other. This default positioning is called "static" and is more or less what the position of the page will look like if there is no css style applied to the HTML.

One could compare the positioning somewhat to the bubble shooter game, in which you have to try to position bubbles in a certain way:

Bubble Shooter CSS Positioning
Fig1. - Static positioning: All html elements will align from left to right and from top to bottom.

With this positioning method, the different HTML elements cannot be influenced in any way, and will always follow the pattern of going to the first available spot in the top left corner of the page. Attributes such as top, bottom, left or right won't work in this position setting.

Bubble Shooter CSS Positioning Not Working
Fig2. - Static positioning: Even if you aim to a certain place on the page by using top, bottom, left or right, the element will keep within the natural flow of the document and land on the first available spot in the top left corner.

2. position: relative;

The position relative will place an element relative to its normal position. As a designer/coder, you can change the position by using top, bottom, left or right.

Bubble Shooter CSS Positioning - Relative
Fig3. - Relative positioning: By using the position relative, you can place an element relative to it's natural position by using top, bottom, left or right. As the figure illustrates, the slightly transparent red bubble is the natural place. I shifted one bubble to the right by using right: 10px;, another example shows one to the bottom by using bottom: 20px; and another example shifts the element 40px to the left and 60px down. You can also position an element upwards, which may it go out of the page etc. This is helpful when for example you want to overlay text over an image.

It is important to note however that any following element will keep considering the "natural" position of the element as a reference point. For example if I shift the red bubble down, the next bubble will not take it's place, but act as if the red bubble were in the right spot (static positioning).

Bubble Shooter CSS Positioning - Relative with next elements
Fig4. - Relative positioning: Elements following the "positioned element" will follow the natural flow of the document. This means that the original position of the first element will act as if they are taken.

3. position: fixed;

The position fixed lets you stick an element to anywhere on the page. If the user scrolls up or down, the element will always remain in the same place. This attribute value can be very helpful for sticking navigation bars to the top of the page or position chat-boxes in the bottom-left corner etc.

Bubble Shooter CSS Positioning - Fixed Position
Fig5. - Fixed positioning: The red bubble is fixed in the bottom right corner and will no try to move up to the top lef corner.

4. position: absolute;

The position absolute let's you position an element relative to the first positioned (any element that is not positioned 'statis') ancestor in the family tree. If all elements are static, and there are no ancestors, the element will be positioned relative to the

Bubble Shooter CSS Positioning - Absolute Position
Fig6. - Absolute positioning: Picture the HTML code of the "menu" bubbles. Each of these bubbles has no static positioning as they seem to fly around randomly. If however we want to position the text element inside this bubble, we might want to use the absolute positioning to keep it nicely centered and horizontally aligned in the middle.