hi everybody!

this is easy question hopefully, but i was unable to find an answer to it.

how to use drupal_add_css function? i placed my style.css in sites/all/files and used

drupal_add_css(  '/sites/all/files/style.css');

but why this doesn't work?

Comments

nathanjo’s picture

where did you put your drupal_add_css? can you paste your code here?

http://api.drupal.org/api/function/drupal_add_css/6

helavissa’s picture

thanks for reply,
i put it in the body section of a page. there isn't any code there, just to test if it works:

drupal_add_css(  '/drupal3/sites/all/files/style.css');
hello!

that's all. may be the location of style.css isn't right?

jefkin’s picture

If you're using 6.x for instance, it might be better to put your drupal_add_css() in your template.php file.

I'm almost certain if you put it inside of block in your page.tpl.php or even your node.tpl.php, it is probably no good to use, since the theme engine has already created the "php" code for your page and therefor you won't see any effect.

Even if you picked the wrong url to your css file, you should check the page source to see if you have html like:

<link type="text/css" rel="stylesheet" media="all" href="/sites/all/files/style.css?t" />

If you're using 5.x, then I can't help you, I never messed with theeming much for 5.x.

Jeff

helavissa’s picture

thanks for reply!

version 6.
ok, actually i put that function into a body of a page (as in create content->page). looks like it was wrong..

it works when i put

<link type="text/css" rel="stylesheet" media="all" href="/drupal3/sites/all/files/style.css" />

into a head section of page.tpl.php, (but then it will applied to every single page? i only want those styles on one page).

then question is why do i need drupal_add_css at all?

rschwab’s picture

It seems like drupal_add_css is meant for module developers, and not so much for themers or using in your tpl.php files.

What you can do (and what I think I will do) is just add the html into a tpl.php file directly:
<link type="text/css" rel="stylesheet" media="all" href="/drupal3/sites/all/files/style.css" />

You can use the Devel module to figure out what you should name your tpl.php file for styling only the one page.

- Ryan

jcmc’s picture

Hi

I try to explain you with another words or other start point.

Wehn you want to develop acording the code standars you have to use "drupal_add_css".

You use in your posted example only a path but, wat is with the BASE PATH, your drupal can be installed in a subdirectory from localhost or in the root directory your virtualhost. (this here is only intro)

Now
when I use drupal_add_css( )
the next wat your drupal need is the path to: a theme folder or a module folder or the path to the files folder
the best way is to use the tools from DRUPAL logically.
here i recomend to use the function drupal_get_path() <- this function need to know two things: 1. wat for a path? to the themes or to the modules
and 2. wich theme or wich module. If your style.css is placed in the files directory I recomend to use the function file_directory_path().
Wehn you use the functions above drupal find secure the right path to your file.

Here your test
drupal_add_css( '/sites/all/files/style.css');

Here acording to the drupal coding standards wehn in files directory:
drupal_add_css(file_directory_path() .'/style.css' ;

and here acording to the drupal coding standards wehn in themes or modules directory:
drupal_add_css( drupal_get_path('theme', 'theme_name') .'/style.css');
drupal_add_css( drupal_get_path('module', 'module_name') .'/style.css');

I hoppe it help you to understand