CSS Background Images Using Percentage Positioning

Posted October 15, 2007 at 7:20 AM

Tags: HTML / CSS

One of the techniques that Dan Cederholm uses widely in Bulletproof Web Design is the positioning of background images using percentages. Up until now, I have only ever used either pixel placement or keyword placement using TOP or LEFT to position background images. As such, I was a bit confused as to how the percentage positioning even worked. To explore this, I set up a small demo page using this background image:


 
 
 

 
CSS Background Image With 75% White, 25% Green  
 
 
 

This image is 400 pixels wide. Vertical rules are placed every 100 pixels with the green background covering the last 25% of the image (300 - 400 pixel area). I have done this so that we could easily see where the 75% point in the image is. Then, I wrote this demo page:

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

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>CSS Background Position By Percent</title>
  •  
  • <style type="text/css">
  •  
  • div {
  • background-image: url( "./75_percent.gif" ) ;
  • background-position: 75% top ;
  • background-repeat: releat-y ;
  • border: 1px solid #666666 ;
  • font: 12px verdana ;
  • line-height: 50px ;
  • margin-bottom: 25px ;
  • }
  •  
  • div.w50 {
  • width: 50px ;
  • }
  •  
  • div.w100 {
  • width: 100px ;
  • }
  •  
  • div.w200 {
  • width: 200px ;
  • }
  •  
  • div.w300 {
  • width: 300px ;
  • }
  •  
  • div.w400 {
  • width: 400px ;
  • }
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <div class="w50">
  • 50
  • </div>
  •  
  • <div class="w100">
  • 100
  • </div>
  •  
  • <div class="w200">
  • 200
  • </div>
  •  
  • <div class="w300">
  • 300
  • </div>
  •  
  • <div class="w400">
  • 400
  • </div>
  •  
  • </body>
  • </html>

As you can see, every DIV on the page puts the background image at a left position of 75%. Running the code, we get the following output:


 
 
 

 
CSS Background Image Positioning With Left 75%  
 
 
 

When I saw this output, things became a bit more clear. Seeing the iterative behavior over different widths was more obvious than the one-off examples in Bulletproof Web Design. I have added some explanation text over the page output:


 
 
 

 
CSS Background Image Positioning With Left 75% Explained  
 
 
 

As you can see here, when using a percentage position for top or left of a background image, the process has two parts. The first part is placing the "thumbtack". The thumbtack is the point on the image that will be used for contextual placement. When using a left value of 75%, the thumbtack is placed at the 75% horizontal mark on the image, which on our image is where the green color starts. The second part of placement is the contextual placement within the DOM element background. This takes the thumbtack from step one and places it 75% from the left most point of the parent element.

To word it in english, positioning a background image at 75% left finds a point on the image that is 75% of the horizontal distance and then places that point at 75% of the horizontal distance of the parent element. This is a bit wordy, but when you understand this, making flexible layouts will become so much easier. I wish this is something that I knew a long time ago.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



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

Reader Comments

Aug 3, 2008 at 6:31 PM // reply »
2 Comments

Thanks for your information, was very useful..Nice site


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 19, 2010 at 12:14 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Eric, I think we should just agree that there are no cross-the-board rules on this. NULL values are good when they add value. That is highly contextual - there's nothing about NULL values that is ... read »
Mar 19, 2010 at 12:12 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Eric, By all means if you need referential integrity and foreign keys for things like "middle name", then you should use NULL values. In the applications I build, 99% of the time, I there is no ... read »
Mar 19, 2010 at 12:10 PM
Why NULL Values Should Not Be Used in a Database Unless Required
@Ben Nadel, Are you saying that correct data doesn't have any business value? Nonsense. What about referential integrity and foreign keys? ... read »
Mar 19, 2010 at 12:00 PM
Using jQuery To Leverage The OnChange Method Of Inputs
Thnx. FYI spelling error in your comment "// Add dirtry flag to the input in" ... read »
Mar 19, 2010 at 10:57 AM
Javascript Number.toFixed() Method
It doesn't have a base() method, but it can be put together simply with: Math.base = function(n, to, from) { return parseInt(n, from || 10).toString(to); }; ... read »
Mar 19, 2010 at 10:21 AM
ColdFusion Query of Queries Unexpected Data Type Conversion
@Hiren, Nice use of cast. ... read »
Mar 19, 2010 at 10:17 AM
Why NULL Values Should Not Be Used in a Database Unless Required
@Mike, If you enjoy using NULL values, then go for it; from a technical standpoint, there's certainly no reason to not use them if you like them. All I'm saying is that I think a lot of times the ... read »
Mar 19, 2010 at 10:12 AM
ColdFusion Path Usage And Manipulation Overview
@Sean, Did you ever figure out the "/" issue? Sorry I had no more ideas on that problem. ... read »