Thursday, January 11, 2018

Create a Responsive Banner Ads Using HTML 5 and CSS 3

Banner Ads and Responsive. What is it? 

Responsive web design is a major leap forward for the internet. No longer are we chained to the age-old 'printed page' metaphor where content is static and paginated into regular-sized chunks. Now the internet can live and breathe, and adapt to fill whatever space it has available from mobile phone screens to massive cinema displays. This is how the internet was meant to be.

But there is a slight problem. Websites often have banner advertising and traditional banner ads are not flexible. Both flash and animated gif banners come with fixed pixel dimensions so they are not compatible with todays responsive layouts. We need a new way to do banner advertising. We need responsive banner ads...

A new format for banner ads

The only way to make banner ads as responsive as our HTML5 layouts is to make them with HTML5 too. This might at first sound like a crazy idea but I assure you it's not. Actually there are many benefits to making ads with HTML.
  • HTML ads are fully accessible and semantic markup makes them screen reader compatible.
  • Text, Images, Video, Javascript, and forms can all be used within an ad just the same as any webpage.
  • Banners can use dynamic server-side scripting and databases if required.
  • Changes can be made to an ad after it has been deployed just like any webpage.
  • HTML ads can be very small in file size.
  • Banner serving is essentially the same as web hosting.
  • No new technology for web developers to learn - its the same as normal web development.
  • and of course HTML5 ads can be made adaptable to any size with CSS3 media queries - this is exactly what we need for responsive banners!

So how do we make one?

Firstly, an ad is created as a responsive HTML5 page. We add any text, images, and links to the page that are required within the ad and style it with CSS. Secondly, the ad can be displayed on any website using an iframe. The only trick is making the iframe dimensions dynamic via CSS3 media queries, I'll cover that shortly... But basically that's it!

Time for a quick demo

Here is an example HTML5 ad shown at the popular 125x125 pixel size:




And here is the same ad with a flexible width:



Notice how the second ad responds when you change the size of your browser window... Pretty neat eh! :)

A new convention for banner sizes

Responsive layouts require page elements to have variable widths so banners must follow this convention now too. The height of a banner doesn't really matter in responsive design so we can use any height we like. But choosing a height doesn't mean our ad is stuck at that height, we can have multiple heights defined for each ad!
To maintain backwards compatibility, responsive ads should use the same pixel heights as traditional banner sizes. We could in theory create responsive banners that work with any width or height but that's not practical to build or test. I suggest we stick to a minimum width of 88 pixels and have the following set of standard heights:
31px "micro"
micro bar (88 x 31)
60px "button"
button 2 (120 x 60)
half banner (234 x 60)
full banner (468 x 60)
90px "banner"
button 1 (120 x 90)
leaderboard (728 x 90)
125px "small rectangle"
square button (125 x 125)
250px "medium rectangle"
vertical banner (120 x 240* Close enough!)
square pop-up (250 x 250)
medium rectangle (300 x 250)
400px "large rectangle"
vertical rectangle (240 x 400)
600px "skyscraper"
skyscraper (120 x 600)
wide skyscraper (240 x 600)
half-page ad (300 x 600)
The seven pixel-heights I've chosen above give us a nice range in vertical size and with variable widths we cover the most popular ad sizes in use today. Of course as with traditional banners you can create custom sizes if you need them.
The best thing about responsive ads is we have just reduced 14 conventional banner sizes down to only seven heights, and all of these can be displayed by a single HTML5 ad! Not only that, but my demo ad comes in at less than 25k for the whole thing (HTML, CSS and the JPG image). This is smaller than the maximum file size for a single small banner!
Try out these new banner sizes using my responsive ad checker - you can use it to test your own ads too. Make sure you change your browser window size to see how they all adjust.

Resizing iframes with CSS media queries

Sometimes you may want a responsive height for an ad, to do this you need to change the iframe size via CSS media queries. I find the best way to do this is to set the iframe's width and height to 100% and put it inside a div with specific dimensions set in CSS. Here's what this looks like:
<div id="ad">
   <iframe
      src="ad.html"
      border="0"
      scrolling="no"
      allowtransparency="true"
      width="100%"
      height="100%"
      style="border:0;">
   </iframe>
</div>
And here is the CSS:
/* default height */
#ad {
   height:60px;
}
@media only screen and (height:90px) {
   /* 90 pixels high */
   #ad {
      height:90px;
   }
}
@media only screen and (height:125px) {
   /* 125 pixels high */
   #ad {
      height:125px;
   }
}
View my responsive banner ad CSS file to see a complete working example.

Tracking impressions & clicks

The best thing about HTML5 ads is they can be tracked with Google Analytics just like normal websites. This actually gives you a lot more data than traditional banner serving systems. Not only can you reliably track impressions but also referrers, browsers, screen resolutions, mobile devices, popular countries and cities, and a whole lot more!
It's also a trivial task to track clicks on your ad by using one of the many free URL shortening services. I personally prefer 10-d.tk . If your ad has multiple links you can track them individually. Just remember to use a target="_top" on your links so your ad busts out of the iframe and the link fills the full browser window.

Recording banner sizes in META tags

Responsive ads can be made to support any number of sizes but rather than trawl through the CSS to discover what heights are supported I suggest we record them in a META tag. Here's an example:
<meta
   name="displayheight"
   content="31, 60, 90, 125, 250, 400, 600" />
This is good because META tags are computer readable. Your browser might need to know alternate sizes to display an ad in some situations.

In summary

We need variable-width banner ads for our responsive layouts and I think HTML5 is the best way forward. With a little CSS3 trickery we can create a single ad that adjusts to fit all the common banner sizes in use today. Download my example banner files and give it a try. Don't forget to test out your creations on my responsive ad checker. You can also support the cause if you want to by including a responsive banner on your blog or website, simply copy and paste the code below (change the size to whatever you like):
<div id="ad" style="width:100%;height:90px;" >
   <iframe
      src="http://matthewjamestaylor.com/responsive-ads/ad.html"
      border="0"
      scrolling="no"
      allowtransparency="true"
      width="100%"
      height="100%"
      style="border:0;">
   </iframe>
</div>
Comment here if you have any suggestions or bug fixes. I haven't had a chance to test in IE yet... I have an eleven day old son and a 20 month old daughter so I don't have a lot of spare time... It's amazing I managed to get this article done!! :)