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:
| | | | ||
| | ![]() | | ||
| | | |
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 »
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:
| | | | ||
| | ![]() | | ||
| | | |
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:
| | | | ||
| | ![]() | | ||
| | | |
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
Comments (0) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Project HUGE: Exercise List - A Journey Into Object Oriented Programing
Bulletproof Web Design By Dan Cederholm (Thanks Javier Julio!)
There are no comments posted for this web log entry.