Skip to main content

HTML CSS Trick for Displaying Code Snippets: Wrap Long Lines with PRE Tag

The <pre> tag that defines preformatted text is the only tag in HTML that respects and preserves whitespaces like line-breaks, blanks, tabs and multiple spaces between words. Whatever is enclosed inside the pre element will be displayed as-it-is on the web browser.

The pre tag is therefore an excellent choice when you like to show a code snippet in your blog preserving the tabs and line breaks. Infact, most of the Adsense Javscript code snippets that you see on this blog are displayed using the PRE tag.

The only issue with pre tag in IE is that if the a particular line is too long, it could push the right sidebar (if you have any) to the bottom of the page. Firefox is immune to this but it will do extend the line into the sidebar without wrapping it.

Luckily, T. Longren has discovered a neat CSS hack to solve this pre text problem that will just wrap the long lines of code. All you have to do is insert the following code in the style element of your webpage or the external CSS stylesheet file if you have one.

pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}


Your webpage layout will look as beautiful as before even with those lengthy lines of Javscript code and PHP scripts.

Wrapping Text Inside PRE Tags [Thanks, Lorelle VanFossen]

Popular posts from this blog

How to Download Contacts from Facebook To Outlook Address Book

Facebook users are not too pleased with the "walled garden" approach of Facebook. The reason is simple - while you can easily import your Outlook address book and GMail contacts into Facebook, the reverse path is closed. There's no "official" way to export your Facebook friends email addresses or contact phone numbers out as a CSV file so that you can sync the contacts data with Outlook, GMail or your BlackBerry. Some third-party Facebook hacks like "Facebook Sync" (for Mac) and "Facebook Downloader" (for Windows) did allow you to download your Facebook friends' names, emails, mobile phone number and profile photo to the desktop but they were quickly removed for violation of Facebook Terms of Use. How to Download Contacts from Facebook There are still some options to take Friends data outside the walls of Facebook wall. Facebook offers the Takeout option allowing you to download all Facebook data locally to the disk (include

Digital Inspiration

Digital Inspiration is a popular tech blog by  Amit Agarwal . Our popular Google Scripts include  Gmail Mail Merge  (send personalized emails with Gmail ),  Document Studio (generate PDFs from Google Forms ) and   File Upload Forms ( receive files  in Google Drive). Also see  Reverse Image Mobile Search , Online Speech Recognition and Website Screenshots , the most useful websites on the Internet.

PhishTank Detects Phishing Websites by Digg Style Voting

OpenDNS, a free service that helps anyone surf the Internet faster with a simple DNS tweak , will announce PhishTank today. PhishTank is a free public database of phishing URLs where anyone can submit their phishes via email or through the website. The submissions are verified by the other community members who then vote for the suspected site. This is such a neat idea as sites can be categorized just based on user feedback without even having to manually verify each and every submission. PhishTank employs the "feedback loop" mechanism where users will be kept updated with the status' of the phish they submit either via email alerts or a personal RSS feed . Naturally, once the PhishTank databases grows, other sites can harness the data using open APIs which will remain free. OpenDNS would also use this data to improve their existing phishing detection algorithms which are already very impressive and efficient. PhishTank | PhishTank Blog [Thanks Allison] Related: Google