Sharing CSS stylesheets across different domains

I’m working on two different WordPress sites that are in many ways similar but are skinned differently.

For example, let’s say I’m working on two ‘magazine’ sites, which share the same CSS layout e.g. grid system, margin’s etc., but different CSS decorational properties e.g. gradients, colours, shadows.

Read More

What is the best way to get them to share the same base CSS layout, but different decorational CSS?

Initially I thought something like …

<link rel="stylesheet" type="text/css" media="all" href="LINK-TO-BASE-CSS-ON-PRIMARY-DOMAIN.css" />
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'template_url' ); ?>/style.css" /> <!-- This would be the 'top-up' CSS -->

This doesn’t seem particularly clean to me though. I admit though, there is also a very similar question here.

Is this still considered the best way?
Are there any disadvantages to linking to another domain? I’ve read that this may even be an advantage because of cross-domain loading.

Related posts

Leave a Reply

3 comments

  1. What you suggested is fine. I’m not sure that there is an advantage called “cross-domain loading”, but hosting your style sheets in another server is a very common practice. This other server is known as a CDN, or Content Delivery Network. Here’s some information about CDNs and their advantages:

    http://www.sitepoint.com/7-reasons-to-use-a-cdn/


    Also, pro-tip: do some DNS prefetching if you’re going to use a separate domain to host your files. It’s as easy as:

    <link rel="dns-prefetch" href="//myexternalcdn.com" />
    
  2. try to separate layout structure and templates and call everything from the same domain

    <link rel="stylesheet" type="text/css" media="all" href="fileserverdomain/css/structure.css" />
    <link rel="stylesheet" type="text/css" media="all" href="fileserverdomain/css/firsttamplatename.css" />
    

    OR

    <link rel="stylesheet" type="text/css" media="all" href="fileserverdomain/css/structure.css" />
    <link rel="stylesheet" type="text/css" media="all" href="fileserverdomain/css/secondtamplatename.css" />
    

    Benefit of this solution is that you can adtionaly offer switching templates 😀

  3. You can use this filter, to filter out styles with any condition or alternate output href string, example:

    add_filter( 'style_loader_src', function($href){
    if(strpos($href, "name-of-allowed.css") !== false) {
    return $href;
    }
    return false;
    });