CSS Background Images Using Percentage Positioning

Posted October 15, 2007 at 7:20 AM by Ben Nadel

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:

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



Reader Comments

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

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


Jan 19, 2011 at 2:23 PM // reply »
1 Comments

and does it work when you say releat?


Nov 18, 2011 at 9:45 AM // reply »
1 Comments

This is very clever. I didn't think it was possible. I had to read your article three times to actually get it.

This is a great technique when you have a background image with two colors say one to the left and one the right. And you want to place the dividing line between the two colors at a percentage position. The only thing to know is that for each percentage position you want you'll need to create a separate background image.


Jun 28, 2012 at 9:20 PM // reply »
1 Comments

I can't find the words to thank you.


Aug 12, 2012 at 8:42 PM // reply »
1 Comments

This has been confusing me and I have a much better understanding of this now. Thanks. The thumbtack analogy is excellent!


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