Situations arise where CSS files or CSS rules need to be added from a module. This could be useful, for example, to style features added by the module, or to style content added by some other module. When compared with including stylesheets through a theme's .info
file, modules provide greater control and flexibility.
This recipe demonstrates how to add CSS files using the mysite module.
We will be using the mysite
module created in the previous recipe. Within its folder, create a subfolder named css
and subsequently, within it create two CSS files named mysite.css
and mysite_special.css
and populate them with some sample rules.
Add the following code to the file mysite.module:
/** * Implements hook_init(). */ function mysite_init() { // The path to the mysite module. $path = drupal_get_path('module', 'mysite'); // Include mysite.css. drupal_add_css($path . '/css/mysite.css'); // Include mysite-special.css, but do not preprocess...