Wrapping Code Samples on Mobile Devices
One part of being a technical blogger that I've had to come to grips with is code samples and small mobile device screens. I was amazed when I saw a double-digit percentage of visits to this blog were from mobile phones -- mental! I started paying more attention to detail on said devices and I realized that code samples required loads of horizontal scrolling: yuck. By utilizing CSS white-space, we can make code wrap and avoid arm-numbing scrolling on small screens:
pre {
white-space: pre-line;
}
I like using PrismJS so that requires a different selector:
pre[class*='language-'], code[class*='language-'] {
white-space: pre-line;
}
Thankfully white-space lets me help you all avoid horizontal scrolling on mobiles. Of course you'll need to choose which media query you want to apply that to, but I'll let you do that. In some cases it may be difficult to read the line-broken code, but that's surely better than all that crazy scrolling.
Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...
I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener
and attachEvent
. Times have changed but there are still a few functions each developer should...
In case you weren't aware, CSS animations are awesome. They're smooth, less taxing than JavaScript, and are the future of node animation within browsers. Dojo's mobile solution, dojox.mobile
, uses CSS animations instead of JavaScript to lighten the application's JavaScript footprint. One of my favorite effects...
Google recently debuted a new web service called the Font API. Google's Font API provides developers a means by which they may quickly and painlessly add custom fonts to their website. Let's take a quick look at the ways by which the Google Font...
I’m also using PrismJS. Any tips to get the line-numbers to adjust?
Also, by default tab size is about 4 i believe, this snippet brings it back a little…
That is, assuming you’ve got tabs over spaces (which you should of course).
Interesting post. I have thought about this for quite some time. The thing is that code is much more readable, in my opinion, when not wrapping it like this.
Since we are used to large screens, reading wrapped code like this is hard and unfamiliar. We do not code on small screens and therefor it makes more sense to scroll horizontally when viewing code then to force line breaking/wrapping.
No offense, but I would rather go with all the crazy scrolling. I honestly think it looks nicer than the broken up code, and it is easier to read and understand code samples.