Showing posts with label Abhilasha Sharma. Show all posts
Showing posts with label Abhilasha Sharma. Show all posts

Thursday, January 11, 2007

DIVs and Background Images...

So, basically, they give you this really cool feature in DIVs which lets you put any image in the background so that your page could look exactly how you wanted it...

But then, that would be too eazy right ??

So, here is the catch - THE BACKGROUND IMAGES OF THE DIVs ARE NOT PRINTABLE.

Oh YES !! Exactly the nightmares I used to have....!!!!

Here is this heavenly perfect page I made and woopsie..the header and the footer of the page dont show up in the printouts...because by default, the browsers have the print feature disabled for the background images....

Not a very fun feeling, trust me...

So how to fix the problem ....I am still trying to work out a perfect solution but here are some of my attempts...
  • Using the print style sheet -

@media print {

#title { letter-spacing:-1000px; }

#title:before { content: url(banner.jpg); }

}

  • Using the DIV layering in the page


<div id="Body" style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px;
PADDING-BOTTOM: 0px; PADDING-TOP: 0px; HEIGHT: 800px" align="center"><p></p><p><img
height="100%" src="images1/background_image.jpg" width="1014"
border="0" /></p><p><div id="LeftNevigation"
align="left">Menu goes here</div><p></p><p><div
id="TopBanner" align="left">Banner goes here</div><p></p><p><div
id="CenterBody" align="left">Main Text Goes Here</div><p></p><p></p></div><p></p><p></p>

  • Using the Line-Style Hack

I found another, non-HTML based solution (example) though, and list-style-image is the keyword. I explain.Instead of including our banner graphic via the background-image hook, we use CSS' "lists" functionality instead.Suppose you want the browser to print an image in the place of an h1 with id="title", then the following piece of print CSS does the trick:

#ti\tle {
/* 6. */display: list-item; /*
1. */list-style-image: url(banner.jpg);
/* 2. */list-style-position: inside;
/* 3. */letter-spacing: -1000em;
/* 4. */font-size: 1pt;
/* 5. */color: #fff;
/* 5. */}

Some annotations:
We give our h1 the characteristics of a list-item.
We pretend our banner image is a list-style-image.
Firefox wants you to put the image inside.
We make the text disappear into a black hole by means of Malarkey's Image Replacement (MIR).
As MIR doesn't work in Opera, we set the font-size to 1pt and make the text white. This works fine with Opera's default print settings. Other image replacement techniques rely on moving or hiding mechanisms, all of which would also hide our image. Hence small, white text.
As list-style-image is not supported in IE5 and IE5.5, we exclude these browsers with a simple escaping hack.

When the things are different from what they seem

In PHP, there is this handy feature called array_unique which creates an array of unique values, as the name indicates...

I sure was glad to see the function and decided to use it in my code while parsing some really big and dirty arrays...and sure enough, I had Unique values in the resulting Arrays...

here is how i used it -

$agency = array_unique($agency); //where agency is the array having the redundant values...
$count_agency = count($agency); //total number of distinct values
asort($agency); //to sort the values in acending order...

And sure enough, there was this little catch...-

The array preserved the location of the unique values...And I had the unique array with lots of null values...

There are two possible workarounds to this -

1. The quick and dirty way - Create a new array and use it
$count = 0;
foreach($agency as $var)
{
if($var!=NULL)
{
$new_agency[$count] = $var;
$count++;
}
}
$agency = $new_agency;
2. Use foreach in every calling of the array
Or you could just use foreach to loop through the array and use it that way...

But always remember, the comparison of any string to NULL in PHP is a bad idea. It not only slows your code down but also, leads to some false positives in matching the data.

So, the lesson for the day -
Clear the NULLs before you use the array_unique results.

Cheers