InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search
  • Home
  • > Tutorials
  • > CSS cookbook

How to use html pre tag to display code

By admin | Last updated on Mar 25, 2016

Html pre tag can be used to display code on WordPress blog or any site. HTML pre tag represent preformatted text. Text within this element is typically displayed in a non-proportional font exactly as it is laid out in the file. This article will cover few simple css styles which you can use along with pre to display code.

Defined width and no word wrap

Here is css style code which can be used to in case you donot want any wrapping of words for long lines.

<style type="text/css">
pre {
  width: 40%;
  max-height: 400px;
  color: #444;
  padding: 10px;
  background: #eee;
  border: 1px solid #ccc;
  border-radius: 5px;
  word-wrap: normal;
  overflow: auto;
}
</style>
<pre>
#!/usr/bin/python

## print hello world multiple times
print "Hello World! Hello World! Hello World! Hello World!";
</pre>
refresh done
try it online

Few points to note:

  1. border-radius (HTML5 feature) does not work in IE8 but does not cause any issue also.
  2. We have used width in percentage. You can also use fixed width.
  3. text wraps only at line breaks.

Defined width and wrap words for long lines

Here is css style code which can be used to in case you want wrapping of words for long lines.

<style type="text/css">
pre {
  width: 40%;
  max-height: 400px;
  color: #444;
  padding: 10px;
  background: #eee;
  border: 1px solid #ccc;
  border-radius: 5px;
  word-wrap: normal;
  white-space: pre-wrap;
  overflow: auto;
}
</style>
<pre>
#!/usr/bin/python

## print hello world multiple times
print "Hello World! Hello World! Hello World! Hello World!";
</pre>
refresh done
try it online

Note that css white-space: pre-wrap causes longs lines to be wrapped whenever needed along with line breaks.

Additional notes

  1. Note that pre can also be mimicked by using div with the following style:
    div {
      font-family: courier, monospace;
      white-space: pre;
    }
  2. There are couple of javascript based code prettify libraries like Google code prettify which are worth looking at. These will have some overhead of extra js and css though.

Suggested posts:

  1. Running php eval on code with tags
  2. CSS text-overflow – truncate text with three dots
  3. How to use google custom search for wordpress site
  4. CSS – margin collapsing
  5. Javascript – catch errors using window.onerror
  6. Edit and debug css in Chrome
  7. Install nginx and php in docker ubuntu container
  8. CSS list-style – shorthand for list marker type, image and position
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, CSS cookbook, HTML, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress