With IE finally being removed from Drupal core (#3252084: [meta] Remove support for Internet Explorer), we finally get a chance to modernize Drupal’s CSS.

This includes things like

  • CSS Custom Properties (aka CSS variables)
  • Use of CSS Grid
  • Use of CSS Logical Properties
  • :is() and :where() selectors
  • Selector arguments within :not()
  • Proper CSS resets for things like settings tray and contextual links though the all: revert property and value.

Drupal’s CSS Custom Properties will be a new API in core

The biggest and most important thing is CSS Custom Properties (aka CSS variables). We need to decide the following

  • When and where to implement them
  • How to name them
  • How much to abstract them
  • Can we mark certain custom properties as internal? Should we? How?
  • The process of deprecating custom properties. Can we use JavaScript to read them and then trigger deprecation warnings?
  • How does this fit into the bigger picture of backwards compatibility for markup and CSS?

Because of Drupal’s “Easy Upgrades Forever”, we need to get this right the first time. Properly implemented, CSS custom properties have the potential to dramatically ease the themer’s workload. However, because themes will become reliant on these custom properties, we have to be very intentional about the implementation, as we may not get a second chance.

Opportunities

We have tons of opportunities for improvements here.

Dangers

  • Potential to overcomplicate the CSS
  • Potential to over-abstract the variables

What we need

  • Core committer buy in
  • Core committer time – there’s a LOT to do here, and not much time before Drupal 10 comes out.
  • Some awesome developers to help out and submit patches / MRs

Want to help?

Great!

  • Comment here, and share your thoughts.
  • Join us in the #CSS channel in Drupal Slack!

Related Issues

Issue fork drupal-3254529

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mherchel created an issue. See original summary.

mherchel’s picture

Issue summary: View changes
shaal’s picture

I'm very excited about this initiative!

When working on the new CSS Modernization Initiative, I suggest we use design-tokens.

Introduction to Design Tokens - https://specifyapp.com/blog/introduction-to-design-tokens

Here are some additional ideas regarding design tokens:
We can use a system like Style Dictionary - https://amzn.github.io/style-dictionary/#/README
And a Figma plugin https://github.com/lukasoppermann/design-tokens

mikemai2awesome’s picture

I second tokens. Especially global tokens for color, spacing, and typography.

saschaeggi’s picture

Huge +1 on this.

However, because themes will become reliant on these custom properties, we have to be very intentional about the implementation, as we may not get a second chance.

This is the crucial point. As we're currently refactoring a lot of CSS/JS code in Gin I can totally relate to this 😉😅 As almost everything will be CSS3 variables for example (a lot already is) to have typography, colors, spacings, shadows, sizes etc. easily overwritten with CSS3 vars.

andy-blum’s picture

There are several things coming in CSS that are extremely exciting but make me nervous to do a CSS modernization right now.

  1. Cascade Layers
  2. Container Queries
  3. When/If logic
  4. Native CSS Nesting

If we really view this as a one-shot-to-get-it-right, I would consider waiting until we have some of these major architectural changes available to us. For those who may not be aware of each of these:

Cascade Layers
Cascade Layers is a proposal to allow CSS writers to define new layers of CSS, effectively creating a specificity boundary within the code. This would pair perfectly with the way Drupal's libraries are already set up using SMACSS-style base/layout/component/state/theme categorization. In a system using the new @layer setup, no rule in base - regardless of specificity - would override a rule in any of the lower layers.

Container Queries
Most CSS authors are already familiar with @media queries to change property values based on the with of the viewport, but container queries will allow us to change property values based on the width of an element's container. Using these rules, one could change the layout of regions (header/content/sidebar) based on the width of of the viewport, and change the layout of content & blocks based on the width of their region containers.

When/If logic
Right now, it's not possible to set mutually-exclusive media queries. For example:

@media (min-width: 600px) {
  /* ... */ 
}
@media (max-width: 599px) {
  /* ... */
}

In the above, it is possible to write conflicting CSS that gets resolved using specificity and the location of the ruleset. It's also a scenario in which we have one break point written twice. If we want to update the breakpoint we need to remember to update both 600px and 599px. With the new proposal, we can write this like a traditional if/else block:

@when media(min-width: 600px) {
  /* ... */ 
}
@else {
  /* ... */ 
}

Native CSS Nesting
This should be fairly straightforward from where we are now thanks to the way core's PostCSS is setup

shaal’s picture

@mherchel, could we add a link to the recording of your Design4Drupal presentation?
You covered a lot of upcoming CSS modernization that we'll be able to leverage.

"We need to get this right the first time" will probably be true again when Drupal 11 is released (~ 2 years after Drupal 10).

Drupal 10's major refactoring will be to modernize CSS, which will bring so many new features, and to take advantage of CSS variables (hopefully structured with design tokens).

In time, more CSS features will be available, allowing us to make our code more efficient. Once we get our modernized-CSS foot in the door, we'll be able to improve the code in smaller steps.

fhaeberle’s picture

When thinking about modernising the overall CSS in core I highly recommend to build a design operation (DesignOps) strategy beforehand and secondly talk about possible new CSS features to incorporate.

This is the most missing thing I would line out in the design for Drupal space, as with a good designops strategy, changes later become really easy to decide and to do in most cases.

For this strategy to succeed properly and long-term, it's best to have an orientation and inspiration on existing tools like style dictionary or some Figma plugin tooling as well as best practices around frontend dev, but do not rely on something too far away of "the drupal space".

At best, we should not have an extra burden of maintaining the needed improvements, as they feel simply as the new normal or replace the old counterparts.

In my opinion this strategy would focus on which parts of the CSS is relatively easy to replace and therefore a "quick win" with a high outcome (e.g. logical properties) rather then which things are hard and make barely a difference (grid in all possible cases).
Furthermore, as already mentioned, we should talk about the lifecycle of custom properties/design tokens and how to integrate/maintain/deprecate them.
And last but not least, documentation will be key here, the more documented for beginners/themers/module builder, the better.

mherchel’s picture

There are several things coming in CSS that are extremely exciting but make me nervous to do a CSS modernization right now

All of that is super exciting. But keep in mind that the limiting factor will likely be Firefox ESR which puts those technologies in reach of Drupal 11 if we're lucky.

What I'm mostly concerned about is a standard way (with standard naming, examples, etc) to use CSS custom properties.

We can wait and deliberate all year long, but there's always going to be new exciting things coming down the pipeline.

In time, more CSS features will be available, allowing us to make our code more efficient. Once we get our modernized-CSS foot in the door, we'll be able to improve the code in smaller steps.

CSS Variables are happening in Drupal 10. They're already happening in Olivero and Claro. The primary question is will they have a coherent and predictable pattern. That is a primary objective of this proposed initiative.

When thinking about modernising the overall CSS in core I highly recommend to build a design operation (DesignOps) strategy beforehand and secondly talk about possible new CSS features to incorporate.

My thought process is that we standardize our CSS custom property naming and implementations within the core subsystems. We figure out how and when to abstract them, and then we stick to the plan.

The new CSS features incorporated won't necessarily change any designs. It's more about creating a more maintainable and extendable codebase.

I wouldn't be opposed to designops (there's been work by @ckrina on a Drupal design system), however that would be outside of the scope of this proposed initiative.

mherchel’s picture

Below are some proposed goals. I'd love to get thoughts on this!

Goals

  1. Come up with naming scheme for using CSS variables
    1. Example: --views--grid-item--max-width
    2. Example: olivero--spacing--5
    3. The deliverable could be documentation page
    4. Another deliverable could be a patch to sync up Olivero and Claro’s naming
  2. Do we want to abstract CSS variables?
    1. Should we create some default variables that are used in core?
      1. Spacing
      2. Font-size
    2. If we decide this is a good idea, deliverable could be a patch
  3. Figure out how we can deprecate core CSS variables? Figure out which ones are internal
    1. For example, will --olivero- prefixed properties be internal, since we’re not supporting subtheming olivero?
  4. Incrementally introduce CSS Grid
    1. #3151553: Create new “Views Responsive Grid” format for Views Core
    2. Media library grid
    3. Layout builder default layouts
  5. Make CSS resets more robust through the use of all: revert;
    1. Settings tray
    2. Contextual links
    3. Admin toolbar
mherchel’s picture

nod_’s picture

A few points that come to mind, maybe to add some to the issue summary:

  • how to document custom variables (comment in the CSS file? which format?), do we have a separate css file with only the variables?
  • where should custom variables names and purpose be exposed (api.drupal.org?),
  • how do we document their scope since we can't assume all variables will be global all the time.
  • do IDE offer autocompletion/documentation hints of some kind?
mherchel’s picture

Issue summary: View changes
shaal’s picture

I'd like to propose utilizing https://open-props.style for Drupal 10 usage of CSS Variables.
1 hour video that covers the Open-Props project and its benefits - https://youtu.be/Z-kD-EhwKlA

Ashwani_Verma made their first commit to this issue’s fork.

KingStar2001’s picture

Hello Sir, My name is Shivank Mishra currently Pursuing Computer Science Engineering (India). I would like to work in the Modification of Drupal Front-end page by using CSS, JavaScript, React and UI/UX to build a attractive and a reponsive website. I would like to have this opportunity to Contribute to this Organisation as its one of the growing Organization as Open Source. Sir I would love to make the Proposal Regarding it would you Please assign this task to me will try to work approx (4 to 6hr) to complete this task under great Mentorship.
Thank You

Email: mishrashivank2001@gmail.com
Contact no: 8382836059
Linkdin: https://www.linkedin.com/in/shivank-mishra-2bb4b1224/

larowlan’s picture

Should we have an issue to replace use of float with modern layout techniques?

andypost’s picture

Sound huge change and most of usage in LTR/RTL

$ git grep 'float: left'|wc -l
200
$ git grep 'float: right'|wc -l
182
mherchel’s picture

Unfortunately browsers don't yet support float in logical properties (like float: inline-start)

Chromium issue is at https://bugs.chromium.org/p/chromium/issues/detail?id=850004

I just saw from #3313146: PostCSS Logical not transpiling flow relative properties (e.g. float: inline-start) which are not supported by Chrome and Safari that there's a new plugin that will be included in PostCSS ENV (which is what core uses). So we may be able to do this! See https://github.com/csstools/postcss-plugins/issues/632#issuecomment-1401...

Mohammed khan’s picture

Hello everyone
my name is mohammad khan pursuing computer science engineering, i am a frontend developer and have nice grip on languages like HTML,CSS,CSS(Bootstrap),Javascript and React , I would like to contribute this project through GSOC'23 it will be great experience to contribute to this community and work along with mentors
I am want guide from the mentor and requesting mentor to assign this project to me
thankyou

Mohammed khan’s picture

Hello everyone
I have created the first draft of proposal for this project it will great help if mentors will review my proposal and advice me to make it more better

https://drive.google.com/file/d/1r2BZNbQMndiNVeP88TtI1fzgcfRQmPT0/view?u...

Thank you

samsoapbox’s picture

Issue summary: View changes

Fixed broken link

Version: 10.0.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Firefox ESR supports CSS nesting as of 128 which was released in July: https://www.brycewray.com/posts/2024/07/firefox-esr-native-css-nesting/ so that should be fully unblocked now afaik. Next release it could go out in is 11.1 in December.

catch’s picture

Also I don't know if/how much we rely on it, but SASS is changing how it handles declarations and nested rules https://sass-lang.com/documentation/breaking-changes/mixed-decls/

Should we open a spin-off issue to switch to native nesting everywhere? Seems like something that can be done in its own right separate from any other changes.

johnpitcairn’s picture

Might we consider also introducing the use of @layer to help tame some of ridiculous specificity that both Sass and native nesting cause?

mortona2k’s picture

Using @layer would change a lot of how things work, but probably something we need to adopt eventually.

Declaring CSS in a @layer makes it process before any unlayered CSS. Since Drupal libraries don't use @layer, any existing library could override your theme.

Maybe I don't have the right strategy in mind for using @layer. But it seems like having everything in one layer using SMACSS is incompatible with using @layer in a theme.

The cascade works differently with @layer. A generic selector in a higher priority layer will override a more specific selector in a lower layer. I can see putting base styles in a low layer, and utilities in a high one.

Maybe if core converts the SMACSS layers, and provides a way to customize the names and order, that would work.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.