🤯 New: Coding Assessments Practice your skills on real-world programming challenges

CSS Positioning Properties At-A-Glance Guide

    Nigel Peck

    Every day this week, we’ll be publishing an article that’s been hand picked by our editor, as part of CSS Theme Week.

    This article is aimed at experienced CSS developers who need a reference for the properties related to positioning in CSS 2. Each section of this article includes a link to the relevant section of the SitePoint CSS Reference and the W3C CSS 2 Specification.

    This article is not intended to cover in depth all the positioning-related information contained in the specification, but to describe only those properties and values that are implemented in some or all modern browsers, and are of real-world use to you today.


    First let’s look at a few definitions of concepts that need to be explained. You can skip ahead if you like, and use this list as a reference to check if you stumble on something. Of course, don’t forget that the SitePoint CSS Reference contains comprehensive information about all properties, not just those collated here.

    The Viewport


    The Viewport is the area of the screen in which your web page is displayed. This is not the same as the Initial Containing Block.

    The Initial Containing Block


    The Initial Containing Block is the entire space assigned to your document. In referring to the entire space, we include any bits of the page that users have to scroll to in order to see them (in the Viewport).

    It is possible to limit the size of the initial containing block by setting the width and height properties of the root element.

    The Root Element

    The root element is the html element. When we apply CSS Rules to the root element it is safer (allows browsers to do as they wish) to apply them to both the html and the body element:

    html, body { 
     /* Style Rules Here */

    Care must be taken with this approach, however, because the net effect has the potential to compound. For example, doing the following will result in 100px of padding being applied to every edge of the page – 50px for each element.

    html, body {  

    Containing Blocks


    Every element in your document has a containing block. This is the block element that it is inside, for example:

    <div id="content"> 
     <div id="example">

    The div#content is the containing block for div#example.

    Box Types

    Boxes come in a number of different types. The only types we are concerned with in this article are block boxes and inline boxes.

    Block Boxes

    Block boxes are the types of boxes generated (by default) by elements like p and div. A block box has space around it, and elements that come after start below the box instead of next to it.

    For example:


    will be displayed (by default) as:



    instead of:

    Test Text

    …as it would if p was (by default) an inline element.

    Inline Boxes

    Inline elements are displayed next to each other. For example:


    will be displayed (by default) as:

    Test Text

    instead of:



    …ignoring any other visual formatting these elements normally produce.

    Positioning Schemes


    There are five styles of positioning in CSS 2.

    Normal Flow


    Normal positioning is set according to the “normal” rules that have been around for nearly a decade.

    Relative Positioning


    Relatively positioned elements are positioned according to the normal flow and then moved. Elements that come after a relatively-positioned element behave as if the relatively-positioned element was in its ‘normal flow’ position, even if this means they occupy the same screen space.

    Float Positioning


    Boxes positioned using float are positioned using normal flow and then moved left or right as far as they can go. Elements that appear after them will move up to fill any gap left behind, but will flow around the box – they will not occupy the same screen space.

    Absolute Positioning


    Absolutely positioned elements are not affected by normal flow. They are positioned by specifying precise distances that will exist between their sides and their containing block.

    Fixed Positioning


    elements work in the same way as absolutely-positioned elements, except their position is relative to the Viewport. As such, they don’t move if the page is scrolled – they stay relative to the Viewport.

    CSS Visual Display Properties

    The display Property


    The display property is used to control the box type used for an element. This allows you to do things like:

    • set bold text to be block level,
    • have inline paragraphs, or
    • stop an element from being displayed or taking up screen space.


    Sets the elements selected to block level.


    Sets the elements selected to inline.


    Stops the display of the element to which it is assigned. The element is completely removed from the display – it doesn’t take up any screen space.

    There are a number of other values for the display property which we’re not concerned with in this article. They deal with lists, tables and other box types that we won’t cover here, but which are covered in detail in SitePoint’s online CSS reference.

    The position Property


    The position property is used to specify the following positioning schemes:

    • Normal Flow,
    • Relative Positioning,
    • Absolute Positioning, and
    • Fixed Positioning.

    This is done by setting position to one of the following values:


    Set this box to normal flow, the default for all elements.


    Use relative positioning for this box using the top, right, bottom and left properties.


    Use absolute positioning for this box using the top, right, bottom and left properties.


    Use fixed positioning for this box using the top, right, bottom and left properties.

    The top, right, bottom and left Properties


    All positioning schemes need you to set these in order to know how to carry out your positioning request. You should always set one of top or bottom and one of left or right. Setting both top and bottom or both left and right will not usually do what you want in Internet Explorer – usually, you’ll end up with only left or top being used respectively.

    You may use the following values for these properties:

    Fixed Length
    You may use a fixed distance such as 20px for 20 pixels.

    You may use a percentage value such as 20%, which is 20% of the containing block’s width, or height value (for left/right or top/bottom respectively).

    The float Property


    The float property takes three values. float is ignored if you have set the position property to either absolute or fixed.

    The box is floated to the right of the screen.
    The box is floated to the left of the screen.
    The box is not floated.

    The clear Property


    This property is used to give control of boxes that appear after a floated box. Setting this property on a box will ensure that it will appear below any floated boxes that come before it.

    This box will appear below any left-floating boxes that come before it.


    This box will appear below any right-floating boxes that come before it.


    This box will appear below any left-floating or right-floating boxes that come before it.


    This box will not clear any floating boxes that come before it.

    The z-index Property


    The z-index property is used to specify how boxes stack ‘on top’ of each other and is relevant when you have two boxes that occupy the same screen space. z-index accepts a value between 0 and 32767. In a situation where two boxes occupy the same screen space the box with the highest z-index is displayed ‘on top’. If both have the same value the one that comes last in the code is displayed on top.