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.
One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest
, wasn't really made for what we've been using it for. We've done well to create elegant APIs around XHR but we know we can do better. Our effort to...
If you follow me on Twitter, you know that I've been working on a super top secret mobile application using Appcelerator Titanium. The experience has been great: using JavaScript to create easy to write, easy to test, native mobile apps has been fun. My...
We've all inherited rubbish websites from webmasters that couldn't master valid HTML. You know the horrid markup: paragraph tags with align attributes and body tags with background attributes. It's almost a sin what they do. That's where dwMarkupMarine comes in.
It's best practice to provide an indicator of some sort when performing an AJAX request or processing that takes place in the background. Since the dawn of AJAX, we've been using colorful spinners and imagery as indicators. While I enjoy those images, I am...
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.