Web Design Stuff
HTML Tutorials    CSS Tutorials    Web Hosting   Resources
Create a Web Page 101
Making Web Pages Intro What is a Web Page? Why Make a Web Page? The History of HTML Learn HTML or XHTML?
Basic HTML
Basic HTML Tutorials Basic HTML Necessities How to Make a Web Page How to Edit a Web Page The Basics of HTML Tags Basic HTML Page Structure HTML Attributes
HTML Font Codes
HTML Font Codes Intro HTML Font Color Codes HTML Font Size Codes HTML Font Style Codes HTML Bold/Italic Codes Combining Font Codes
Formatting Text
Formatting Text Intro Making Paragraphs Miscellaneous Formatting Headings & Subheadings Creating Hyperlinks
Using Graphics
Using Graphics on the Web Add Graphics to Your Pages Graphics and Accessibility How to Align Graphics Page Color & Background Graphics as Hyperlinks Horizontal Rules
Creating Tables
HTML Tables Tutorials HTML Table Fundamentals Background & Border Color Table Frames & Rules Table Width and Alignment Cells 1 -Space & Alignment Cells 2 -Row Column Span Cells 3 -Width & Height
Making Lists
HTML Lists Tutorials Bulleted Lists Numbered Lists Definition Lists
HTML Frames
HTML Frames Tutorials Using Frames for Layout Advanced Frame Layouts Putting Hyperlinks in Frames Frame Border Width Color, Margin and Control Problems with Frames SmartFrames: A Solution SSI: An Alternative to Frames
Web Page Forms
Making Feedback Forms A Simple Feedback Form Installing NMS FormMail Debugging Your Setup My Web Host is Out to Lunch User Input Components Text Fields Checkboxes & Radio Buttons Dropdown Menus Push Buttons Layout and Presentation
Basic CSS
Basic CSS Tutorials What is CSS? Why You Should Use CSS How to Use CSS Inline Styles Embedded Style Sheets External Style Sheets Class Selectors ID Selectors Combining Selectors
CSS Properties
CSS Properties Intro Font Styles Width, Height & Spacing Borders Backgrounds Position Float & Alignment Hyperlinks
All About Web Hosting
Hosting Your Own Website What is a Web Host? Your Website's Home Page Building a Website Offline About Free Web Hosting Best Free Web Hosting Commercial Web Hosting How to Get a Domain Name Ecommerce Web Hosting Web Hosting Terminology
Free Web Design Tools
Best Free Website Tools Best Free Text Editors Best Free Graphics Editors Free Website Analysis Tools
Setting Up HTML Kit
HTML Kit Introduction How to install HTML Kit Screenshot Breakdown Basic Configuration Overall Appearance Shortcuts and Startup Editing Window Customizing Toolbars Using the Favorites Tab Making a New Actions Bar Odds and Ends
Free Templates
Free Website Templates Two Column Fixed Width Three Column Liquid Layout Miscellaneous Templates Dynamic Menu Effects Two Column Experimental Terms of Use About These Templates
Website Templates Help
Getting Started Template Zip File Download How to Edit Your Template What to Edit in the HTML How to Add Your Logo Making a Website
Web Design Tips
Web Design Basics Tables vs. Tableless Using Tables for Layout Example Table Layouts World's Crappiest Web Page
Twitter Backgrounds
Twitter Backgrounds Intro Cool Twitter Backgrounds Cool Twitter Backgrounds 2 Plain Twitter Backgrounds Dark Twitter Backgrounds Best Twitter Backgrounds Cute Twitter Backgrounds Music Twitter Backgrounds Music Twitter Backgrounds 2 Twitter Backgrounds 101 TERMS OF USE
All About Web Browsers
What is a Web Browser? Mozilla Firefox Internet Explorer Opera How to Set Up Firefox Top 5 Firefox Extensions
 
Contact
Post Some Feedback Send Me An Email Iron Spider Blog About Iron Spider... Site Conventions
 
 
 

 

CSS Properties - Position

 
The CSS properties in the table below can be used to define how various components on your web page are positioned and offset with respect to the normal flow. These properties are typically applied to block elements but you can also apply them to images and some form elements. These include the following:

p, div, blockquote, table, td, ul, ol, li, form, input, textarea, select


CSS Properties List 5 - Position
Property NameSome Possible ValuesWhat It Does
positionrelative | absolute | fixedSets type of positioning
top20px | 10%Offsets element from top
left20px | 10%Offsets element from left
bottom20px | 10%Offsets element from bottom
right20px | 10%Offsets element from right


NOTES:
  • Position ~ The position property can be used to define how an element is displayed with respect to the normal flow. The values you can use are described in the following:

    • relative - The element is positioned relative to the normal flow. This may be used in conjunction with the offset properties.
      (More about relative positioning...)
    • absolute - The element is removed from the normal flow and positioned with respect to its containing block. This may be used in conjunction with the offset properties.
      (More about absolute positioning...)
    • fixed - Same as absolute except the element is fixed in place (not supported by Internet Explorer 6).
    • static - The element is positioned normally. (Offset properties do not apply).

    WHAT IS THE NORMAL FLOW?

    Web browsers are designed to display HTML elements on a web page according to certain standards. Block elements, such as p, div, blockquote, are typically stacked vertically one on top of the other:

    Example 1 - NORMAL FLOW OF BLOCK ELEMENTS
    THE FIRST DIV ELEMENT.
    THE SECOND DIV ELEMENT.
    THE THIRD DIV ELEMENT.


    On the other hand, inline elements like span, code or A are typically lined up horizontally until available space runs out after which the content will break to a new line:

    Example 2 - NORMAL FLOW OF INLINE ELEMENTS
    THE THIRD SPAN ELEMENT.THE SECOND SPAN ELEMENT.THE THIRD SPAN ELEMENT.


    THE CONTAINING BLOCK

    Depending on the user's browser, the root element of a web page can be either the html or the body element. This root element represents the browser viewport and —by default— becomes the containing block for all other elements on your web page. Absolutely positioned elements are offset with respect to the border of this principal containing block.

    However, any block element on your web page may also be defined as a containing block for other elements inside of it. How do you do this? By simply using the position property and setting the value to anything but static.

    To illustrate, let's take our three div elements in Example 1 and put them all inside another div element. Then we'll use the position property, set it to relative and, presto... we have a containing block. For demonstrational purposes, let's give it a red border and set its width to 300 pixels. Elements inside this containing block may be now absolutely positioned with respect to its borders.

    Example 3 - A CONTAINING BLOCK (RED BORDER) IS ESTABLISHED USING position: relative;
    THE FIRST DIV ELEMENT.
    THE SECOND DIV ELEMENT.
    THE THIRD DIV ELEMENT.
    Example 3 - A CONTAINING BLOCK (RED COLOR CODE) IS ESTABLISHED USING position: relative;

    <div style="position: relative; border: 5px solid red; width: 300px;">
    <div style="background: blue; color: white;">THE FIRST DIV ELEMENT.</div>
    <div style="background: green; color: white;">THE SECOND DIV ELEMENT.</div>
    <div style="background: purple; color: white;">THE THIRD DIV ELEMENT.</div>
    </div>

     


    RELATIVE POSITIONING

    So exactly what is relative positioning? Well let's take Example 3 and offset the position of the second green colored div relative to the normal flow.

    First we'll use the position property on the green div and set it to relative. Now we can use the top and left properties to offset the div element from its normal position:

    The green div is relatively positioned (placed in the normal flow). It is then offset 5 pixels from the top and 10 pixels from the left of where it would normally appear.
    Example 4 - RELATIVE POSITIONING
    THE FIRST DIV ELEMENT.
    THE SECOND DIV ELEMENT.
    THE THIRD DIV ELEMENT.


    Example 4 - RELATIVE POSITIONING

    <div style="position: relative; border: 5px solid red; width: 300px;">
    <div style="background: blue; color: white;">THE FIRST DIV ELEMENT.</div>
    <div style="position: relative; top: 5px; left: 10px; background: green; color: white;">THE SECOND DIV ELEMENT.</div>
    <div style="background: purple; color: white;">THE THIRD DIV ELEMENT.</div>
    </div>

     


    ABSOLUTE POSITIONING

    So maybe you'd like to set some kind of coordinates on your web page and have a div element, for example, displayed exactly there regardless of what else is going on. This can be accomplished using absolute positioning. This kind of positioning will remove the element from the normal flow such that it may be offset from the edge of its containing block. Let's take the green div in Example 4 and simply change the value of the position property from relative to absolute. The following result is what we get:

    The green div is absolutely positioned (removed from the normal flow). It is then offset from the top/left sides of the containing block (red border).

    (Note that the width of the absolutely positioned green div no longer defaults to 100% of the available space but rather is displayed just wide enough to hold its content.)
    Example 5 - ABSOLUTE POSITIONING
    THE FIRST DIV ELEMENT.
    THE SECOND DIV ELEMENT.
    THE THIRD DIV ELEMENT.


    Example 5 - ABSOLUTE POSITIONING

    <div style="position: relative; border: 5px solid red; width: 300px;">
    <div style="background: blue; color: white;">THE FIRST DIV ELEMENT.</div>
    <div style="position: absolute; top: 5px; left: 10px; background: green; color: white;">THE SECOND DIV ELEMENT.</div>
    <div style="background: purple; color: white;">THE THIRD DIV ELEMENT.</div>
    </div>

     


  • Top
    Left
    Bottom
    Right


    These are offset properties that can be used in conjunction with any element that is positioned. An element is said to be positioned when the position property is set to anything but static. The offset properties shift the element away the specified side of the containing block according to a specified distance. Any CSS length measurement may be used as the value but it's probably easiest to stick with either pixels or percentages.

    Logically speaking, what you want to do is use a pair of offset properties that shift the element away from one corner. To accomplish this, you would simply combine the use of any horizontal and vertical side. Hence using the top and left properties together would shift the element away from the top/left corner. Likewise you could combine the use of the bottom and right properties together. Using the right and left properties together just simply wouldn't make sense.

    In the following example code, the div is absolutely positioned. Then it is offset 5 pixels from the top and 10 pixels from the left:

    <div style="position: absolute; top: 5px; left: 10px;">Some content...</div>

*   *   *


A major player in tableless designs is the use of the float property. The next page in this tutorial will give you the lowdown on how to create floats and how to align text...





Best Free Stuff
for webmasters

Free Text Editors
Free Graphics Editors
Website Analysis Tools
Free Website Templates

See also:

Best Free Web Hosting
How to Make a Web Page

 

If you need a .COM web address, you can get one quick and easy at...

www.GoDaddy.com
<~ BACK TOP NEXT ~>
 
HTML Tutorials |  CSS Tutorials |  Web Hosting |  Domain Names |  Web Design Resources
Iron Spider © Copyright 2004-2011 Robert Darrell