i'd like to add CSS inline code to theme pages. This will be done dynamically to alter for e.g. themes page width and other CSS settings. The inline code must be added after the last CSS file. It should be aggregated and compressed, but only as the last style to overwrite some standard theme values.

Why does drupal_add_css() do not allow to add inline CSS?

Comments

Crell’s picture

Priority: Normal » Minor

Because inline CSS is nasty. :-) It has to be resent on every page load. It is all around a bad idea for a production site.

If there's some pattern to what CSS you include when, you're better off using more specific CSS rules and putting a class on the body element. See the Zen theme for an example of how to do such a thing.

If you want completely arbitrary CSS values, then you're probably not thinking about the problem correctly in the first place. However, you may be able to get away with drupal_set_html_head() and adding a complete <style> block manually. (I can see a few use cases where it might be appropriate, but not many.)

hass’s picture

i thought about drupal_set_html_head() too, but this requires a change on the page.tpl.php. drupal_set_html_head() inserts code into $head variable, isn't it?

so the current order will not work:

  <?php print $head ?>
  <?php print $styles ?>
  <?php print $scripts ?>

and need to be changed into:

  <?php print $styles ?>
  <?php print $scripts ?>
  <?php print $head ?>

i have no clue if this may destroy something...

Gurpartap Singh’s picture

Title: CSS: inline code not possible » Add 'inline' option to drupal_add_css()
Priority: Minor » Normal

CSS inline could be allowed and then compress all into one(i.e. this too) with our css aggregator. But's it's just an opinion, their might be reasons to advocate both sides. But however it's always suggested to use files to make the best page load times while the css aggregation is turned off.

Crell’s picture

Status: Active » Closed (won't fix)

Seems nothing is going to happen on this front, which is probably a good thing.

cangeceiro’s picture

though inline css is nasty, and i wouldnt recommend its use for an actual website. This feature could come in handy greatly for the theming of newsletters. Most webmail clients will only render html email if the css is inline.

currently im beating my head up against a wall trying to figure out how to inject css inline to elements for this very reason. I know at least in this scenario it would make alot of peoples lives a little easier.

AltaVida’s picture

alan d.’s picture

Component: base system » theme system
Status: Closed (won't fix) » Closed (duplicate)

As per link in #6 above, but for D7. Very unlikely to be back-ported in D6.

alan d.’s picture

For a hint, use the phptemplate_preprocess_page() to add css and you can place it where you want.

While I must agree that it is very bad to do this, sometimes you just have to. We have a site with a color field on every page (inherited from the parental menu item if not set). The 10 lines of inline CSS appears to be the best option here.