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

Site Performance tutorials

  • Chrome PageSpeed insights
  • Google custom search with lazy loading
  • Install and monitor memcache for php on Ubuntu
  • Internal vs external css
  • Javascript in header vs footer
  • PHP apc - setup and performance benchmarks
  • Php apc vs memcache
  • Using inline image for site performance
 
  • Home
  • > Tutorials
  • > Web Development
  • > Site Performance

Internal vs external css

By admin | Last updated on Jan 27, 2016

There are two ways to include a css on a site. In case you are using a wordpress site, many plugins involving frontend ui element have a css file which gets included externally. I’ll use wordpress as context for this article but this article is relevant to any site.
internal-vs-external-css

Internal css

Internal css can be included on a page using html similar to the following code between <head> and </head> tags:

<style type="text/css">
  h1,h2,h3,h4,h5,h6 {font-family: Georgia, Times new Roman, serif;}
</style>

External css

External css can be included on a site using the html similar to the following code between <head> and </head> tag:

<link rel='stylesheet' href='/path/to/file.css' type='text/css' media='all' />

The href can either be complete url with same of different domain (e.g. http://somedomain.com//path/to/file.css) or current domain relative url (e.g. /path/to/file.css).

Comparison of external and internal css

Here are few things you may want to consider before deciding if some css chunk should go in external file or internal css in page itself:

Css caching

External css gets cached (assuming you are using right http expire headers). It eats some cache space of browser though. It is not such a big concern but something worth keeping in mind with mobiles becoming prominent. In case you have lots of repeat activities by same user, then you get lot of benefit from external css file caching. With css caching, when same user visits a page with shared external css, that css gets loaded from browser cache.

DNS lookup and extra round trip

External cache causes extra roundtrip and potentially one extra DNS lookup. This causes some latency at user end.

Css fetching and page rendering

The page rendering also gets blocked till all external css files have been fetched.

Recommendation

If you do not have many repeat visits by same user, its a good idea to avoid too many external css files. For small css files, its best to put them in internal css. You can also use Google page speed to detect such css files.

How to use internal css in wordpress

In case you already have a plugin you can piggy back that plugin of write a new one. Here is the code you can use to include internal css in wordpress:

function my_style_inline() {
  $content = file_get_contents(ABSPATH . 'wp-content/plugins/PLUGINNAME/CSSFILENAME.css');
  echo '<style type="text/css">' . "\n";
  echo $content;
  echo "</style>\n";
}
add_action('wp_head', 'my_style_inline');

In case you want to dequeue some already queued css, you can use the following code:
function my_style_dequeue () {
  wp_dequeue_style('stylename_used_by_enqueue');
}
// 1001 or some value high enough to ensure dequeue happens after enqueue
add_action('wp_enqueue_scripts', 'my_style_dequeue', 1001);

Suggested posts:

  1. How to setup wordpress custom query params with pretty url format
  2. How to view desktop site from android mobile
  3. How to use Google custom search with lazy loading approach
  4. Online tools to create comic strips
  5. How to install wordpress on Amazon AWS-EC2 Classic Ubuntu Linux micro instance
  6. How to upgrade EC2 Ubuntu Linux micro instance to small
  7. How to and why disable wordpress cron
  8. CSS – inline-block and baseline alignment
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, Page Speed, Site Performance, Tutorials, Wordpress
  • 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