eCard Layout Using Absolute And Relative CSS Positioning

Posted May 4, 2007 at 7:20 AM

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:

 Launch code in new window » Download code as text file »

  • <!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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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 »
6,516 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 Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »