I posted a codepen snippet on this just yesterday:
I will prefix this by saying that the code used to get these results is completely inefficient and again serves no real application, but hey it looks cool.
How this is accomplished is through a range of available media queries and SASS (CSS preprocessor) to do the grunt work of generating the thousands of styles required.
Lets take a look at a specific example:
That’s our HTML code. Its just a series of
<p> tags with a class that corresponds to the stat that are going to display. Lets take a further look at the orientation stat (this one is quite easy to show).
In our CSS compiled from a SCSS or SASS file we have:
We are using the media query of ‘orientation‘ to determine if the browser is in landscape or portrait mode. On that condition we want to apply a content element after (using the :after selector) the existing text already displayed. This example is quite easy because it only really has two options to choose from. When we start getting into statistics such height and width which have almost infinite possibilities that’s when the code becomes inefficient .
For example, lets look at width:
This is where SASS really helps out as we are actually generating an individual media query for each pixel within a range. First we set a variable of the highest value in the range, in this case 2000. Using a SASS for loop we generate the following code from a starting point of 500 through to our highest value of 2000 (these figures represent the width in pixels).
The code SASS generates is a media query that is set to min-width of that value (for example
min-width:500px is everything above and equal to 500px). Finally within this media query we set the content corresponding to that width. We set two more media queries to cover the screen sizes bellow and above the range of 500-2000px. I had actually set this range from 0-3000 for width and 0-2000 for height however codepen kinda freaked out and would not respond/timed out when they were this large (as they are literally generating 5000+ media queries at almost 3 lines each).
There are other media queries that I didn’t cover such as some TV specific ones for scan lines etc. I thought this was a fun little concept to display statistics in the browser with only CSS.