Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it appropriate to place a stylesheet within the <body> tag

Tags:

html

css

Splitting my pages into parts (eg. header and content), I can't avoid placing links to external stylesheets within <body> tags.

I don't want to place all styles in one big file or enclose all stylesheets within the <head> tag because page-specific stylesheets shouldn't be loaded unnecessarily.

In this case, is it appropriate to place a stylesheet within the <body> tag? Is there a better alternative?

like image 549
Dave Avatar asked Aug 09 '10 09:08

Dave


People also ask

Can I put stylesheet in body?

A <link> element can occur either in the <head> or <body> element, depending on whether it has a link type that is body-ok. For example, the stylesheet link type is body-ok, and therefore <link rel="stylesheet"> is permitted in the body.

Where should I put my stylesheet in HTML?

Each HTML page must include a reference to the external style sheet file inside the <link> element, inside the head section.

Can we link CSS in body tag?

No, it is not okay to put a link element in the body tag. See the specification (links to the HTML4.

Should you style body tag?

Any styling you want to do on the <body> should be put into your CSS. And, in fact, <body> is a great place to put all the CSS that should affect the document display as a whole: things like fonts and typography, text and background colors, and other other “default” styling.


2 Answers

Maybe it's not recommended, but yes, it's possible and it's working in all modern browsers. You can place link or inline style tags inside body.

like image 74
Sergei Avatar answered Sep 28 '22 17:09

Sergei


you shouldn't place styles within the body tag, but assuming you're using a server side script (PHP?) to link both header and content, why not write your own class to generate your pages; you could have a method that add styles to the header, and one that outputs the page for instance.

like image 35
darma Avatar answered Sep 28 '22 16:09

darma