Home arrow Style Sheets arrow Page 4 - Using Relative Positioning For Consistent Layout
STYLE SHEETS

Using Relative Positioning For Consistent Layout


Converting a site to Web standards can be quite a struggle. It’s difficult enough to achieve a satisfying layout with positioning, floating, and all that. But it’s downright frustrating to realize that the positioning you’ve chosen doesn’t render consistently in different resolutions, or even multiple browsers in the same resolution! Well, take heart; this article will explain why relative positioning is a trustworthy friend in this case!

Author Info:
By: Justin Cook
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
June 23, 2005
TABLE OF CONTENTS:
  1. · Using Relative Positioning For Consistent Layout
  2. · Relative Positioning Explained
  3. · A Basic Web Page
  4. · Now that’s Style!

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.

blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...
- CSS3 Animated Image Gallery: Transitions
- CSS3 Properties: Fixed Heights with box-sizi...
- CSS3 Properties: Altering Strokes and 3D Eff...
- CSS3 Properties: Text-Stroke
- CSS3 Transitions: Width and Height Properties
- Creating a Drop Down Menu in CSS3
- Intro to CSS Transitions

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials