Style Sheets
  Home arrow Style Sheets arrow Page 4 - Using Relative Positioning For Consistent ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Using Relative Positioning For Consistent Layout
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2005-06-23

    Table of Contents:
  • Using Relative Positioning For Consistent Layout
  • Relative Positioning Explained
  • A Basic Web Page
  • Now that’s Style!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Relative Positioning For Consistent Layout - Now that’s Style!


    (Page 4 of 4 )

    Fire up your favorite text or CSS editor, and create a style sheet (linked as "styles.css" in this example). I’ll go through the definition of the styles one element at a time.

    body

    {

          margin: 0px;

          padding: 0px;

    }

     

    Here we remove any default margins or padding that the browser creates for the page, giving us complete control of the layout.

     

    #all

    {

          padding: 10px 0px;

          width: 90%;

    }

     

    So that things aren’t crunched to the top or bottom of the screen, we create a little bit of padding. And to ensure that there will be no sideways scrolling (never a pleasant thing for UI design), we define all elements to be contained within 90% of the screen.

     

    #logo

    {

          position: relative;

          top: 10px;

    }

     

    So this is our first relatively positioned element. As we want the logo to remain centered –- which is its default position –- we can simply adjust the top position. This moves it down 10px, giving the page a comfortable starting point, which is not crunched too close to the top.

     

    #nav

    {

          position: relative;

          top: 75px;

          left: -280px;

          width: 150px;

          border-width: 1px;

          border-style: solid;

          border-color: #333;

          padding: 4px;

    }

     

    Here we move the navigation box over to the left. Instead of using float, which would throw it to the extreme left of its container, the preference is to control its exact location. We could achieve this by assigning a value of 280px to the "right" property, but I find it’s easiest on the brain to work with "top" and "left." Assigning a negative value (-280px) has the effect of moving it the opposite of the left property.

     

    #main_content

    {

          position: relative;

          top: -70px;

          left: 80px;

          width: 500px;

          text-align: left;

          border-width: 1px;

          border-style: solid;

          border-color: #333;

          padding: 6px;

    }

     

    As for the main content, we move it a little to the right by increasing its left property slightly. This gives the page layout a slightly more balanced appearance. Also, we assign a negative top value, to move it up. This is necessary, because by default it would simply appear below the navigation box. Something to keep in mind here is that, if you add links to the main navigation area, you may need to adjust the top value of the main content, because the change in size of the navigation box would affect the default and relative positioning of all elements after it.

     

    #footer

    {

          position: relative;

          width: 160px;

          left: 220px;

          top: -50px;

          text-align:right;

          font-size: 10px;

          border-width: 1px;

          border-style: solid;

          border-color: #333;

          padding: 3px;

    }

     

    We move the footer inconspicuously off to the right. We also need to move it up, because its position is still affected by the navigation box.

    So if you copy this line for line, it will produce something that looks like the following, no matter what browser, OS, or resolution you’re using:

    Conclusion

    The main thing to keep in mind is that when you define all elements’ positions as relative, changing the properties of one element can change the positions of all elements, and thereby the layout of the entire page. This is unlike absolute positioning, wherein each element is positioned independently of every other element on the page.

    I used only a few basic elements in my example. To see this in practice, check out http://justincook.sytes.net. You can see how everything flows nicely, even with varying amounts of main content in each page.

    Once you get a handle on relative positioning, you can add and configure as many main elements as you’d like. Just remember the order in which they’re rendered on the page. This is what determines where they should be located.

    Relative positioning is just one key element in a multi-environment-friendly world of Web design. Hopefully this article has been helpful to you in solving your Web standards frustrations!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · I appreciate what you tried to achieve here, but if you want to write this kind of...
       · I too appreciate what you've tried to achieve, that is shameless self...
       · [quote]I too appreciate what you've tried to achieve, that is shameless self...
       · If you really are a writer here, I'm quite surprised that you would post these...
       · I think there's another way of design web pages that use absolute positioning (thus...
       · Absolute positioning means absolute to the page. I don't believe that method would...
       · Great explanation of CSS relative positioning. You isolate the problem at hand and...
       · "But it's downright frustrating to realize that the positioning you've chosen...
       · Clear and simple, they way few in the industry can keep things.You saved my...
       · I'm not sure what this author was trying to achieve. This is not a flexible layout,...
     

    STYLE SHEETS ARTICLES

    - Improving the Visual Presentation of a CSS D...
    - Fixing Browser Incompatibilities in a CSS Dr...
    - Building Clean Drop-Down Menus with CSS
    - Creating Hybrid Web Page Layouts with Negati...
    - Creating Three-Column Web Page Layous with N...
    - Swapping Column Positions in Web Page Layout...
    - Creating Web Page Layouts with Negative Marg...
    - Creating Gradients for Individual Containers...
    - Creating Gradients for Web Page Headers with...
    - SEO Scrolling Frames Problem Solved
    - Building Cross-Browser Background Effects wi...
    - CSS: Pseudo
    - Using PNG Images to Build Background Effects
    - CSS: Continuing the Clarification of CSS Cla...
    - CSS: Top Secret Classification






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT