admin管理员组

文章数量:1396461

I want to external CSS files side by side of html code in the body.

Why?

I am developing some templates that get included inside a page, but from those templates I cannot modify the head or add JS/CSS references to the html head section.

What I currently have is:

<html>
<head>
<!-- this i cannot edit -->
</head>
<body>
<!-- some html code -->
<!-- now es what i can edit --> 
    <style>
    #mywidget {
     color: red;
    }
    </style>
    <div id="mywidget">
    hello
    </div>

<!-- end of section i can edit -->
<!-- other html code -->
</body>
</html>

I would like turn that into something like:

<html>
<head>
<!-- this i cannot edit -->
</head>
<body>
<!-- some html code -->
<!-- now es what i can edit --> 

    <!-- LINK TO AN EXTERNAL CSS FILE -->

    <div id="mywidget">
    hello
    </div>

<!-- end of section i can edit -->
<!-- other html code -->
</body>
</html>

But I dont want the overhead of loading all the css every time the page loads, so i want to put it into an external file.

I thought of loading the external css (it will be hosted within the same domain) using javascript, then create a element and insert the content somehow...

I think this should work, however if not I think I can hack some style parser together using jquery which applies the styles at least for basic definitions.

But I am sure there must be a better way!

Any hint is appriciated!

I want to external CSS files side by side of html code in the body.

Why?

I am developing some templates that get included inside a page, but from those templates I cannot modify the head or add JS/CSS references to the html head section.

What I currently have is:

<html>
<head>
<!-- this i cannot edit -->
</head>
<body>
<!-- some html code -->
<!-- now es what i can edit --> 
    <style>
    #mywidget {
     color: red;
    }
    </style>
    <div id="mywidget">
    hello
    </div>

<!-- end of section i can edit -->
<!-- other html code -->
</body>
</html>

I would like turn that into something like:

<html>
<head>
<!-- this i cannot edit -->
</head>
<body>
<!-- some html code -->
<!-- now es what i can edit --> 

    <!-- LINK TO AN EXTERNAL CSS FILE -->

    <div id="mywidget">
    hello
    </div>

<!-- end of section i can edit -->
<!-- other html code -->
</body>
</html>

But I dont want the overhead of loading all the css every time the page loads, so i want to put it into an external file.

I thought of loading the external css (it will be hosted within the same domain) using javascript, then create a element and insert the content somehow...

I think this should work, however if not I think I can hack some style parser together using jquery which applies the styles at least for basic definitions.

But I am sure there must be a better way!

Any hint is appriciated!

Share Improve this question asked Aug 4, 2012 at 16:32 The SurricanThe Surrican 29.9k24 gold badges126 silver badges168 bronze badges 10
  • Have you tried just to include <style> tags? – hazzik Commented Aug 4, 2012 at 16:34
  • @hazzik this is what I am doing right now, however i wuold like to get rid of the overhead of loading it each time – The Surrican Commented Aug 4, 2012 at 16:35
  • Why can't that style be included in the head? It's not a <style scoped>, is it? – Bergi Commented Aug 4, 2012 at 16:35
  • @bergi i just dont have access to the head section. the code must be contained within one file that gets included and from there i cannot reference the head and somehow modify it. only using javascript or something like that. – The Surrican Commented Aug 4, 2012 at 16:36
  • 2 @hazzik, AFAIK you can use <link /> only in the <head>, I could be wrong though – Adi Commented Aug 4, 2012 at 16:41
 |  Show 5 more ments

3 Answers 3

Reset to default 4

You can use CSS's @import rule

<style type="text/css">
  @import url("external.css");
</style> 

putting style tags inside the body is indeed invalid according to html standrds (though it might work in some browsers)

you can put script in the body though, and you can make the script insert the style link in your head. jquery pseudocode would look like this

<body>
  ...
  <script type="text/javascript">
    $(document).ready(function() {
      $('head').append('<link rel="stylesheet" type="text/css" href="style.css">');
    });
  </script>
  ...
</body>

<link rel="stylesheet" type="text/css" href="style.css">-- you can put this in the <body> tag, too; it works just fine in chrome.

本文标签: javascriptIs it possible to reference external stylesheets in the html bodyStack Overflow