Adding a CSS file from a module
Situations arise where CSS files or CSS rules are to be added from a module. This recipe covers the steps required to do so.
Getting ready
We will be adding the CSS to the mysite
module as created in the previous recipe. Create two CSS files named mysite.css
and mysite_special.css
inside the mysite
module's folder and populate them with some sample rules.
How to do it...
Add the following code to the mysite
module:
/** * Implementation of hook_init(). */ function mysite_init() { // The path to the mysite module. $path = drupal_get_path('module', 'mysite'); // Include mysite.css. drupal_add_css($path . '/mysite.css'); // Include mysite_special.css, but do not preprocess. drupal_add_css($path . '/mysite_special.css', 'module', 'all', FALSE); }
Save the file and visit the site to check if the two CSS files are now included.
How it works...
The drupal_add_css()
function is used to add CSS files from a module. Its syntax is as follows:
drupal_add_css($path = NULL, $type...