14

Well this question came after this one

The reasons are pretty much the same, to keep the stuff cleaner, how bad is it to call (being sure the css doesn't collide with overwriting tags)

To put <link rel="stylesheet" type="text/css" href="mycss.css"> in the middle of the body? (if it works)

So that in some cases I could have specific css for a specific module include.

YakovL
  • 5,213
  • 10
  • 46
  • 71
fmsf
  • 33,514
  • 48
  • 140
  • 190
  • It really isn't clearer. CSS is presentation and separate from your content. I feel your pain, but once you get used to it the HEAD tag does feel better. – MrChrister Feb 26 '09 at 00:23

6 Answers6

20

While theoretically you should only put CSS in the head tag, in practice this is often not feasible, or just not worth the pain.

If you do put it in the middle of the document, one of the following two things will happen:

  • The browser will start loading (and displaying) your page, and then once it gets up to the new CSS, the existing content will change or move around

  • The browser will just take a bit longer to display your content.

The first thing seems bad, but it generally isn't a problem. Well it hasn't been for me anyway, because 9 times out of 10, if you're putting CSS in the middle of the page, the CSS is only "built for" the content that comes after it, and doesn't affect the things above it, so they won't move or change anyway

So to answer your question of "how bad is it", I'd say - not bad at all, just be wary of writing CSS that affects parts of the page that got loaded before the CSS file did.

Orion Edwards
  • 113,829
  • 60
  • 223
  • 307
16

<link> is only permitted in the <head> section of a document: Document relationships: the LINK element

Chad Birch
  • 69,230
  • 22
  • 145
  • 148
6

Honestly, if you are doing just once or a couple times in a big project, and that poor layout saves you days of recoding a new solution... go for it. You are right, it works.

You will get answers that it is bad because it isn't valid, because that just isn't right and if you do it too many times, I agree you should reconsider and fix the real problem. But once? I say go ahead; the web remains a pretty imperfect place.

Just don't apply to work with me, we don't put up with that crap here.

EDIT: Just looked at your other question and the site you were talking about. If this is related to your weekend project, do it the right way. Develop good habits early and often.

MrChrister
  • 3,375
  • 4
  • 19
  • 33
  • yeah it is related to my weekend project, having fun with it. The part non online is far more advanced than what's there. But it's not good yet to go online. Just having some fun with web development. – fmsf Feb 26 '09 at 00:52
4

It may work now, but as a link tag is only allowed in the head, there is no guarantee that it will work in the future; a browser update can lead to the css not being used any more.

But then again, perhaps not.

Anyway, I would definitely not recommend it.

Edit: Everybody is always complaining that IE is not adhering to the standards, let´s not start doing that as developers too.

jeroen
  • 88,615
  • 21
  • 107
  • 128
1

Stylesheets should be included in the head tag. Else you might get weird rendering 'flicks', depending on the browser. If you really want to use per module css stylesheets you should provide hooks to your template's head tag.

TomHastjarjanto
  • 5,431
  • 1
  • 27
  • 42
0

Theoreticaly - if you need load css styles into body you may use php include. But i'm not sure it - if it won't work - try change your css file to php:

style.php:

<style>
body {}
</style>

or use jquery load construction and create new style element & append it into body.

DerMike
  • 13,362
  • 12
  • 45
  • 60
alex
  • 1