Ok, so on a site like this one, there is a tremendous amount of 'code' that I share styled with CSS fonts and using the pre, code, var, tt, samp, and kbd html tags. When using a program like VIM to view/write the code it will be shown with a beautiful font and even excellent colors. So how then can a website like this be configured in such a way that a program called a 'browser' such as Firefox or Chrome would also show the code beautifully?

Here is a screenshot of my VIM in action. (It only looks this good thanks to my crazy awesome .vimrc)

So what is the solution? How to enable beautiful code fonts?

Table of Contents

  1. Introduction
    1. Google Web Fonts
      1. Select Fonts
        1. Caveats and Recommendations
      2. CSS for Showing Code
      3. Live Examples
        1. Inconsolata
        2. Source Code Pro
        3. Droid Sans Mono
        4. Share Tech Mono
        5. Ubuntu Mono
        6. Anonymous Pro
        7. Consolas
        8. Lucida Console
        9. Droid Sans Mono
        10. Anonymous Pro
      4. CSS: white-space
      5. More Info and Software

Google Web Fonts

Yet another killer free product provided by Google that is basically a font file server, an A+ product! It will serve different versions of a font depending on the user-agent making the request. Brilliant!

Select Fonts





You can use the google webfonts wizard to select and add any fonts.
Then grab the code to add them to your site. The webfonts wizard will show you 3 different methods for adding the fonts to your site. I recommend adding it using the method the way I am doing it on this site.

Here is the one I use:



Caveats and Recommendations

  1. Change the href= from https:// to // to prevent mixed ssl warnings.
  2. Add at the top of your above your other css so that the fonts will be available as soon as possible for the page rendering.

CSS for Showing Code

Here are the 2 css rules I use.

kbd, samp, tt, pre, code, var, .code, pre {
    font-family: "Ubuntu Mono","Droid Sans Mono",Inconsolata,Courier,monospace;
    font-style: normal;
    font-variant: normal;
    font-weight: normal;
    word-wrap: break-word;
}

pre {
    background: none repeat scroll 0 0 #F3F5F7;
    border-color: #AAAAAA #AAAAAA #CCCCCC;
    border-image: none;
    border-style: solid;
    border-width: 1px;
    color: #000000;
    letter-spacing: normal;
    margin: 1em auto 2em 0;
    max-width: 97%;
    min-width: 50%;
    overflow: auto;
    padding: 0.5em 0.1em 0.5em 1em;
    text-transform: none;
    white-space: pre-wrap;
    word-spacing: normal;
}

Live Examples

Experiment with different browsers. FYI, the ascii "askapache" in all the below pre elements was created with my free online ascii art tool. Check it out! Also, each of the below examples use inline css to specify the font-family, and the text after Queen and Jack is appended with javascript (just fyi in case you do a view source).

Inconsolata

Grumpy wizards make toxic brew for the evil Queen and Jack

Source Code Pro

Grumpy wizards make toxic brew for the evil Queen and Jack

Droid Sans Mono

Grumpy wizards make toxic brew for the evil Queen and Jack

Share Tech Mono

Grumpy wizards make toxic brew for the evil Queen and Jack

Ubuntu Mono

Grumpy wizards make toxic brew for the evil Queen and Jack

Anonymous Pro

Grumpy wizards make toxic brew for the evil Queen and Jack

Consolas

Grumpy wizards make toxic brew for the evil Queen and Jack

Lucida Console

Grumpy wizards make toxic brew for the evil Queen and Jack

Droid Sans Mono

Grumpy wizards make toxic brew for the evil Queen and Jack

Anonymous Pro

Grumpy wizards make toxic brew for the evil Queen and Jack

BTW, the Grumpy Wizards sentence is called a pangram or a holalphabetic sentence because it contains all the letters of the alphabet. Of course google developers would be that smart to use a pangram for the wizard.

CSS: white-space

The CSS specification defines the white-space property in 16.6 White space.

The white-space property declares how white space inside the element is handled.

Values normal | pre | nowrap | pre-wrap | pre-line | inherit
Initial value normal
Applies to All elements
Inherited Yes
  • normal
    This value directs user agents to collapse sequences of white space, and break lines as necessary to fill line boxes.
  • pre
    This value prevents user agents from collapsing sequences of white space. Lines are only broken at newlines in the source, or at occurrences of "\A" in generated content.
  • nowrap
    This value collapses white space as for 'normal', but suppresses line breaks within text.
  • pre-wrap
    This value prevents user agents from collapsing sequences of white space. Lines are broken at newlines in the source, at occurrences of "\A" in generated content, and as necessary to fill line boxes.
  • pre-line
    This value directs user agents to collapse sequences of white space. Lines are broken at newlines in the source, at occurrences of "\A" in generated content, and as necessary to fill line boxes.
  • inherit
    Takes the same specified value as the property for the element's parent.

More Info and Software

  • Google Web Fonts: Getting Started
  • CSS Font Families Example at W3
  • Monospace/Fixed Width Programmer's Fonts
  • WinFontsView v1.10 - View samples of Windows fonts installed on your system

Google Web Fonts and CSS for displaying beautiful Programming and Code

Charles Torvalds
6 Feb 2013

font, Google

  • Site Map WireShark GNU Non-GNU Tor Project cURL TLDP - Documentation
  • Htaccess Files Hacking Htaccess Javascript Linux Optimization PHP Security Shell Scripting WordPress
  • Base64 Image Converter Raw HTTP Header Debugger Graphical ASCII Text Generator Mac Address Vendor Lookup Who Am I – Your IP Information Request Method Security Scanner .htpasswd file Generator Compress CSS DNS Tracer
Copyright © 2025 AskApache
  • Site Map
  • Htaccess Files
  • Hacking
  • Htaccess
  • Javascript
  • Linux
  • Optimization
  • PHP
  • Security
  • Shell Scripting
  • WordPress
  • Base64 Image Converter
  • Raw HTTP Header Debugger
  • Graphical ASCII Text Generator
  • Mac Address Vendor Lookup
  • Who Am I – Your IP Information
  • Request Method Security Scanner
  • .htpasswd file Generator
  • Compress CSS
  • DNS Tracer
Exit mobile version