Prior to Drupal 8, there was a single stylesheets[] property for theme .info files, which allowed to
- add new CSS files of a theme or base theme
- override (replace) CSS files that were previously added by a module or base theme in a theme
- remove CSS files of a module or base theme
The baked-in facility to override and remove CSS files was partially broken in that it caused the replacement CSS files to be loaded in a different order/position than the original, and all other meta/loading properties of the original CSS file got lost, since the file essentially was re-defined from scratch.
This rendered the override/remove facility of the stylesheets[] property useless for most cases and theme authors had to resort to writing relatively complex PHP code to achieve the correct behavior. This problem has been fixed in Drupal 8 by introducing dedicated and separate theme .info.yml file properties for adding, overriding, and removing stylesheets.
Drupal 7
mytheme.info:
; Add a new CSS file:
stylesheets[all][] = layout.css
; Override a CSS file:
; WARNING: The file is moved from its original position to the
; end of the CSS files stack! Use the PHP override code below.
stylesheets[all][] = system.theme.css
; Remove a module CSS file by *adding* it but without supplying
; the actual file in the filesystem:
stylesheets[all][] = node.csstemplate.php:
<?php
/**
* Implements hook_css_alter().
*/
function mytheme_css_alter(&$css) {
// Use our vertical tabs style instead of the default one.
if (isset($css['core/misc/vertical-tabs.css'])) {
$css['core/misc/vertical-tabs.css']['data'] = drupal_get_path('theme', 'mytheme') . '/vertical-tabs.css';
}
}
?>Drupal 8
mytheme.info.yml:
# Add a new CSS file:
stylesheets:
all:
- layout.css
# Override a CSS file:
stylesheets-override:
- system.theme.css
# Remove a CSS file:
stylesheets-remove:
- node.cssNotes
- All CSS files are "keyed" by their file (base)names, which means that you are free to put your theme's CSS files into a
./csssubdirectory and the override/remove properties still work as you'd expect; i.e., they do not care for the path/directory names and this works, too:# Override the system.theme.css file with our version in the ./css subdirectory:
stylesheets-override:
- css/system.theme.css
Inheritance
The override and remove logic applies to base themes, too. The behavior and logic is how you would expect it to be — the following table describes the effects:
| Description | Base theme | Sub theme | File used |
|---|---|---|---|
| Override combinations: | |||
| Base theme overrides a stylesheet, sub theme inherits it. |
stylesheets-override: |
→ | base-theme/file.css |
| Sub theme overrides a stylesheet. |
stylesheets-override: |
sub-theme/file.css | |
| Base theme adds a stylesheet, sub theme overrides it. |
stylesheets: |
stylesheets-override: |
sub-theme/file.css |
| Base theme removes a stylesheet, sub theme overrides it. | stylesheets-remove[] = file.cssstylesheets-remove: |
stylesheets-override: |
sub-theme/file.css |
| Remove combinations: | |||
| Base theme removes a stylesheet; sub theme inherits it. |
stylesheets-remove: |
→ | None |
| Sub theme removes a stylesheet. |
stylesheets-remove: |
None | |
| Base theme adds a stylesheet; sub theme removes it. |
stylesheets: |
stylesheets-remove: |
None |
| Base theme overrides a stylesheet, sub theme removes it. |
stylesheets-override: |
stylesheets-remove: |
None |
Comments
I'm confused, is this
I'm confused, is this compatible with http://drupal.org/node/1876152 ?
Yes
From what I understand, that is for modules, this is for themes.
Module .info files no longer handle theme related stuff.
Theme .info files can override currently attached css files.