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  |  Other Searches  |  Print Page




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 »
7,572 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
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »