eCard Layout Using Absolute And Relative CSS Positioning

Posted May 4, 2007 at 7:20 AM by Ben Nadel

Tags: HTML / CSS

Yesterday, after I opened my free ColdFusion eCard section, someone asked me how the cards were layed out. While, mine is a bit more complicated (since multiple cards share CSS rules and then overwrite for their own particular layout), this is the basis for all of the eCards:

  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  • <html>
  • <head>
  • <title>eCard Display Demo</title>
  •  
  • <!-- Set page styles. -->
  • <style type="text/css">
  •  
  • div.ecard {
  • border: 2px solid red ;
  • position: relative ;
  • height: 120px ;
  • width: 245px ;
  • }
  •  
  • div.ecard p {
  • font-family: verdana ;
  • font-size: 11px ;
  • margin: 0px 0px 0px 0px ;
  • overflow: hidden ;
  • position: absolute ;
  • }
  •  
  • div.ecard p.to {
  • border: 2px solid gold ;
  • left: 10px ;
  • top: 10px ;
  • width: 105px ;
  • }
  •  
  • div.ecard p.from {
  • border: 2px solid pink ;
  • left: 125px ;
  • top: 10px ;
  • width: 105px ;
  • }
  •  
  • div.ecard p.message {
  • border: 2px solid blue ;
  • left: 10px ;
  • top: 40px ;
  • width: 220px ;
  • }
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <p>
  • Some intro text.
  • </p>
  •  
  • <div class="ecard">
  •  
  • <p class="from">
  • To: Samantha
  • </p>
  • <p class="to">
  • From: Ben
  • </p>
  • <p class="message">
  • Hey Sam, did you catch the last episode of Heroes?
  • It's getting pretty cool. I can't wait for a real
  • serious face off between Peter and Sylar. That's
  • gonna be craaaazy!!
  • </p>
  •  
  • </div>
  •  
  • <p>
  • Some footer text.
  • </p>
  •  
  • </body>
  • </html>

If you were to browse that in another window, it would look this like this:


 
 
 

 
CSS Positioning Using Absolute And Relative Styles  
 
 
 

Notice that the HTML markup which is very "information oriented", once displayed, has a highly formatted layout. What is the secret to this black magic? Relative and absolute positioning. Understanding these two and how they interact has truly changed the way I build web page layouts.

When an element is defined as having an "absolute" positioning, this means that it is positioned absolutely in relation to its closest "positioned" parent. What does it mean for an element in the DOM to be "positioned"? Well, all nodes have a default position of "relative"; however, an element is not considered "positioned" until it has an explicit positioning. This CSS rule can be for relative or absolute positioning. See this graphic for some more detail:


 
 
 

 
CSS Positioning Using Absolute And Relative Styles  
 
 
 

Taking that, my eCard layout becomes very clean. The eCard DIV is positioned relatively. This allows its own layout to be inline with the natural flow of the document. The nested paragraphs have an absolute positioning. This allows them to be positioned absolutely relatively to the div (their closest positioned parent) which means that both the DIV and the nested P tags will have a highly formatted layout and yet still flow with the natural layout of the document.




Reader Comments

May 11, 2007 at 3:42 AM // reply »
2 Comments

Just a note, provide a proper, semantic test code since a lot of newcomers tend to copy it as is.


May 11, 2007 at 8:39 AM // reply »
11,314 Comments

@Zoffix,

What do you mean? If you tell me what a "proper, semantic test code" is, I will be more than happy to provide it.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools