tags:

views:

107

answers:

3

Is it a bad idea to link CSS files inside the body?

I've read that the browser is forced to start CSS rendering over again if it finds another css file outside of head, just because it might need to apply styles to elements its already rendered. Also I don't think the html will validate properly (I need to confirm this).

Are there any other reasons?

Edit:

Sorry, I should of said having < link> inside of body. I corrected the question.

+3  A: 

The HTML standard (at least HTML 4 that I looked at) mandates that the <link> tag must be in the <head>. There is no telling what browsers will do with a <link> in the <body>. If I were building a browser I'd probably ignore it.

And why on earth would you even want this?

Thomas
I've got a feeling some will load it and others won't
David Caunt
A: 

You either link to external stylesheet or write it directly, both in head, it won't work if you'll try differently.

Phil
+3  A: 

The only reason I've seen people use to justify doing this is when they're using certain templating engines, and they can't arbitrarily change the stylesheets linked in the head when loading new content or a particular view (in the case of MVC frameworks).

Either way, this should be avoided at all costs as it's sloppy, and improper. Instead, always develop your projects in such a way that you can get an arbitrary stylesheet into the head at any time. I generally do this by cycling through a stylesheet array in the head. That way, if I need to add a new stylesheet, I simply add its pathname to the array to be printed in the head.

<?php

  $styles   = array();
  $styles[] = "css/main.css";
  $styles[] = "css/text.css";

?>

<head>
  <?php foreach ($styles as $style) { ?>
    <link href="<?php print $style; ?>" rel="stylesheet" type="text/css" />
  <?php } ?>
</head>
Jonathan Sampson