Presentation to the Triangle Drupal Users Group (TriDUG) July meeting. This is a brief overview of what CSS selectors are and how they can be used to target CSS at specific parts of Drupal pages.
Note: This was done using the Fusion Starter theme in Drupal 7 but is applicable to D6 and other "major" themes.
Sorry, the slide animations did not come thru... but only the before and after slide really got effected.
Gen AI in Business - Global Trends Report 2024.pdf
Intro to CSS Selectors in Drupal
1. An Introduction to CSS
Selectors and Drupal
Understanding CSS selectors and
how Drupal allows you to use
them to style pages is a key
Drupal skill
2. Audience Knowledge Quiz
How many…
…know what CSS stands for
…have “cut & pasted” CSS into sites
…have written their own CSS rules from
scratch
…know what a CSS Selector is
…are experts, just here to heckle
5. What are Selectors
Patterns used to select the parts of a
document to apply the styles to.
The CSS3 spec define these patterns as:
A chain of one or more sequence of simple
selectors separated by combinators
Lets break this down…
6. Sequence of Simple Selectors
The most common simple selector types are:
.<class> select all elements with class=“<class>”
<element> match a specific HTML <element> (A, H2)
#<id> match element with id=<id> attribute
:<pseudo> “pseudo class” selectors like :hover, :visited
These can be combined into sequences to target
specific document element like:
A.menu:hover
H1
#header
Note: There are a lot of other simple selectors, especially with CSS3. See:
http://www.w3.org/TR/css3-selectors/#selectors
7. What are Combinators
Combinators let you combine sequences of
simple selectors (S3) and target very specific
areas of a document.
Combinators are the most important CSS item
to understand with Drupal!
They let you target almost anything on a Drupal
page
8. Combinators
escendant – S3 S3…
Elements that are contained inside another
element. For example, the selector:
IV.menu A:hover
ould define the hover style for any anchor tag
inside a div with class menu, even if there are
other tags between.
9. Combinators
hild – S3 > S3…
Describes elements that have a direct parent
and child relationship:
L.menu > LI.leaf
ould target ONLY <LI class=“leaf”> elements
that were directly under a <UL class=“menu”>
element.
ibling – S3 + S3… or S3 ~ S3
10. Specificity (The Great Gotcha)
Complex CSS selectors will overlap with more
than one rule applying to an element.
Specificity defines how browsers should
calculate the “weight” of rules. The heavier ones
win.
Drupal tends to have LOTS of rules so chances
are your carefully crafted selector may not work.
Here’s how specificity is calculated:
http://www.w3.org/TR/css3-selectors/#specificity
11. Something !Important
One trick to remember if your selector is not
specific enough is the !important property
declaration.
E.g., if these rules point to the same DIV, the text
color will be red (#id is highly specific).
Div#more_specific { color: red; }
Div.less_specific { color: green; }
If you add, !important to the properties, the color
will be green.
Div#more_specific { color: red; }
Div.less_specific { color: green !important; }
12. Drupal Areas
Drupal pages have a wide variety of CSS addressable
“areas” that are created by the system and themes.
•Page info
•Theme regions
•Nodes (Content type/Specific)
•Blocks (General / Specific )
•Menus (General / Specific )
•Views (General / Specific )
•Fields - Views & CCK (General / Specific )
•Form elements (General / Specific )
13. Page Info
The body tag on Drupal pages will have
several useful classes.
Front/Not-Front: Front page or not
Logged-in/not-logged in: User or Anonymous
Node-Type-<node-type>: Class based on Node Type