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,246 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
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools