The CSS Pseudo-Element :empty and Google Maps

I use Feedly.com as my RSS reader and have a good amount of content coming in there that I browse each day or so. I certainly don’t remember each and everything I read, but even skimming the content and articles means that one day when I run into a work problem, I might remember “Oh yeah, there’s a solution out there just for this.” So such a thing happened and wanted to share the fix with you.

The problem showed up in a Real Estate website I oversee. The site pulls content from a national data feed and then populates the page(s) based on that data. The site’s issue was that sometimes the Google Map container on the page would show up empty (leaving just an empty frame on display). This was either because the feed data was entered incorrectly, or the address being sent to Google Map’s API just couldn’t be found as perhaps the data only referred to a Lot number and not a proper street address, or the address was just too rural for Google Maps to identify. Anyways, empty boxes on a website look broken and unprofessional, so something had to be done.

Normally, being in control of a site’s content, I would just check to see if variable data existed and then echo it:

But in this odd case that the data itself was being loaded through a API call the page wouldn’t know ahead of time if the Google Map API found a real address or not (ie. the page gets rendered and the correct information may appear in the map container… or may not).

I played a little with the Javascript for the Google Map API call thinking that I may be able to ascertain whether or not Google found a real address (and therefore would display the map properly) but was unsuccessful. Either because I don’t know my way around Javascript that well or because there was still data in the returned API call even if it was a “failed” address, I couldn’t get that way to work. And then I remembered a trick I read through my RSS feed weeks back that referenced a CSS pseudo-element for not displaying empty table cells (:empty).

As it turns out this worked exactly as expected, and exactly as I needed it to!

When the map api didn’t find a “known street address” nothing was returned:

When it found the address and built the map it would of course fill in the content for the map to be displayed:

So for me to solve the issue of having an empty box appear on the page when an address wasn’t found all I had to so was add the CSS:

This exact example might not be applicable to you. But in my case I had a border, min-height, and outer glow on the map containter so it looked quite bad when the API returned nothing. Now I’m just waiting for more uses for the pseudo-element :empty!

TL;DR: The CSS pseudo-element :empty will be applied to an empty element on the page.

Tags: , ,

Leave a Reply

Other tags used in my blog:

 (1)  (6)  (1)  (16)  (1)  (1)  (17)  (1)  (16)  (1)  (1)  (7)  (1)  (4)  (15)  (5)  (2)  (27)  (9)  (1)  (11)  (4)  (4)  (4)  (8)  (1)  (6)  (1)  (4)  (1)  (1)  (1)  (3)  (1)  (3)  (4)  (7)  (2)  (3)  (4)  (1)  (1)  (2)  (1)  (7)  (9)  (11)  (2)  (6)  (1)  (3)  (16)  (2)  (3)  (1)  (5)  (1)  (26)  (4)  (4)  (9)