I get asked all the time how to add a CSS file to Drupal, its seems like the most rudimentary thing to do in a modern website, however Drupal has more than one way to add CSS files so here's an in depth explanation of the different ways you can add your files and why you might choose a particular method.
There are at least two main ways you can add CSS - the theme or module info file, and drupal_add_css(). For adding conditional CSS files (for Internet Explorer) there is only one way - drupal_add_css(). First lets look at themes and using the .info file.
Using the info file to add CSS
This is the easiest method - all you do is declare your stylesheets with a path relative to your theme or module root, and you're done. You can declare the media type as well, such as print, screen or all. The following declarations would place the stylesheets in the root directory. This can get a bit messy if you have a lot of stylesheets so you can use sub directories if you want.
stylesheets[all][] = all.css
stylesheets[screen][] = screen.css
stylesheets[print][] = print.cssUsing drupal_add_css() for Modules and Themes
drupal_add_css() is of course a function and you're going to have write some PHP if you want to use it. However, its quite powerful and could be the only way to add CSS depending on your requirements. You can read the documentation to get a full insight how to use this function, here I will give some examples and a brief explanation of why we might use it.
Add a CSS file to your module. Note that I am using the drupal_get_path() function to get the path - this takes to arguments - the type (module or theme) and the name, which is the machine name of the module or theme.
drupal_add_css(drupal_get_path('module', 'my_cool_module') . '/cool-module.css', array('group' => CSS_DEFAULT, 'type' => 'file'));Add a CSS file to your theme. Place this in your template.php file. You might use this if you want to conditionally add the CSS file for a particular path or template.
drupal_add_css(drupal_get_path('theme', 'my_groovy_theme') . '/groovy-theme.css', array('group' => CSS_THEME, 'type' => 'file'));Add inline CSS, this appears in the head. Usefull if you want to override default styles or the CSS is dynamic.
drupal_add_css('body {background: #000;}', array('group' => CSS_THEME, 'type' => 'inline'));Add an external stylesheet. You need the absolute path to the external stylesheet.
drupal_add_css('http://example.com/css/reset.css', array('group' => CSS_THEME, 'type' => 'external'));Adding Conditional Stylesheets for Internet Explorer
There are two ways of doing this - you can either use drupal_add_css() with the optional browser type parameter, or go the easier route and use the Conditional Styles module which allows you to add CSS files via your themes info file. First lets use drupal_add_css() with the browser type. You should use this in themename_preprocess_html() (which goes in your themes template.php file) such as this:
<?php
function my_groovy_theme_preprocess_html(&$vars) {
drupal_add_css(drupal_get_path('theme', 'my_groovy_theme') . '/lte-ie-8.css', array(
'group' => CSS_THEME,
'browsers' => array(
'IE' => 'lte IE 8',
'!IE' => FALSE
),
'preprocess' => FALSE
));
}
?>Which is of course absurdly complicated just add a conditional CSS file, so I'm not even going to try and explain what all that actually means - you can just play around with the important bit (see the next code snippet) - the first line is declaring the conditional statement - here this is targeting IE8 or less. The second line you don't have to worry about, just don't change it.
'browsers' => array(
'IE' => 'lte IE 8',
'!IE' => FALSE
),There is a much easier way to declare conditional stylesheets in Drupal - use the Conditional Styles module. This module allows you to declare conditional CSS in your themes info file, just as easily as the very first examples in this article. Just install the module and you can do stuff like this in your themes info file:
stylesheets-conditional[lte IE 8][all][] = lte-ie-8.cssSo that's a basic round up of how to add CSS to your Drupal projects - I hope this helps you get started with Drupal theming a bit easier. Just one last comment - if you are building a module please think twice about adding CSS for it - more often than not module CSS is not required, unless you are building something like Toolbar or Overlay which really demands it. Just let the theme layer handle the style - adding module CSS can often make the themers life that much harder.
Comments
#attached is another way
If you're generating a render array, then you can add CSS or JS by using the '#attached' property, documented here:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
Which works in all render arrays, not just forms.
CSS for a single page in Drupal
What are the possible ways to add CSS for a single page, not the whole site or theme, in Drupal?
You can use drupal_add_css()
You can use drupal_add_css() and place a condition around it, however this is generally a bad idea and its better to use a body class to target selectors just for that one page - in other words just put the CSS with all your other CSS and allow it to be aggregated with all the other CSS - this is generally faster and more performant than trying to target a stylesheet at one page.
Example please?
Can you please provide an example of using "a body class to target selectors" for just one page? Forgive my ignorance of what may be a basic premise, but where do you put the CSS - in an existing css file or in a new one and in which directory should I put it? I'd find an example very useful of how to make the background color of any table on a page be some particular color. Thank you very much, Jeff!
Drupal will write "page-node
Drupal will write "page-node-7" type classes on the body element, if you're using a more sophisticated starter theme like Adpativetheme or other they may have additional body classes for site sections, Views pages, Panel pages or what-have-you - it all depends on your theme - just view the page source and look at the body tag to see if there is something specific to that page.
So lets say you have this table on node 7, you could use Drupals standard body class to make it red:
.page-node-7 table {background: red}adaptive theme is not working with skinr
i am looking for skinr help in adaptive themes in drupal 7.
You're better off asking in
You're better off asking in either the Skinr or Adpativetheme issue queue on Drupal.org - D7 Skinr is still in heavy development so its basically impossible for me to say, I havent used Skinr in a commercial project since the release of Drupal 7 and until such time it has a rock solid stable release I will refrain from relying on it to deliver features.
:hover Selector
I was trying to use the first method for adding the :hover Selector in my drupal site. However,nothing worked. I created a css file which I stored in the css folder of my selected theme(in this case, bartik)...so the path was themes/bartik/css
Then I added the stylesheets[name_of_css_file][] = name_of_css_file.css to bartik.info file. However, there was no change when I clicked the link. Could you please advice?
never mind actually. I dug
never mind actually. I dug around a bit more and found that in Drupal 7.8, the theme\css\style.css has the hover selectors already set up. So, if one needs to make changes, you just need to go there and make changes.
Hope this helps someone. :)
Any One help me out
Hii can any one help me
how can i create a page that has css code using html
foe xample i created the below code in normal notepad but when i wrote the same code in drupal i could get the reqiured output.Plz help me out
<html><head>
<style type="text/css">
#content
{
margin:10px;
float: left;
width:250px;
height: 100px;
}
.text_line
{
clear:both;
margin-bottom:2px;
}
</style>
</head>
<body>
<h3>Sea Fish</h3>
<div id="content">
<img src="http://localhost/fishes/sites/default/files/SpanishRainbowMackerel-01.jpg" width="250" height="100" title="Spanish Rainbow Mackerel">
</div>
<div id ="content">
<img src="http://localhost/fishes/sites/default/files/Bullet Tuna.jpg" width="250" heigth="100" title="Bullet Tuna">
</div>
<h3 class="text_line">Crabs</h3>
</body>
</html>
Great
This article is kinda helpful, I just wish we have more information about adding CSS on a single page. Thanks!
Thnk you. It helped me
Thnk you. It helped me
so many thanks!
the best tip through net
Add new comment