Opened 5 years ago
Last modified 10 days ago
#33689 assigned Cleanup/optimization
Django theme color variables are inconsistently named and poorly documented
| Reported by: | Murray Chapman | Owned by: | Divyansh Agarwal |
|---|---|---|---|
| Component: | contrib.admin | Version: | 4.0 |
| Severity: | Normal | Keywords: | theme dark mode color variables documentation |
| Cc: | Sarah Abderemane, Antoliny | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I have written a number of UI customizations for Django and it's difficult to be sure I've done the right thing because the color variables names and usage philosophy is inconsistent.
Consider these variables:
--body-fg: #333; --body-bg: #fff; --body-quiet-color: #666; --body-loud-color: #000;
The first two are obvious since they are paired, but it's not clear the quiet and loud colors are meant to be used as foreground/text or background colors.
Other variables such as --primary and --selected-row have no suffix and it's unclear how they're supposed to be used.
The --message-*-bg variables aren't pared with any -fg equivalents; this creates an undocumented dependency on which text colors they are compatible with or even use. Without -fg equivalents it's also impossible to precisely control.
I've also seen some variables used as both text and background colors in the Django CSS.
Also: since the scope of these variables is :root it's probably a good idea to prefix them all with --django to avoid collisions with third-party CSS which may accidentally cause name collisions.
The documentation for the theming feature just points developers at the CSS file but doesn't give any guidance on the philosophy or mechanism. It would really help if there was a consistent naming scheme and style guide for using them.
Change History (15)
follow-up: 2 comment:1 by , 5 years ago
| Easy pickings: | unset |
|---|---|
| Keywords: | documentation added |
| Triage Stage: | Unreviewed β Accepted |
comment:2 by , 5 years ago
Replying to Carlton Gibson:
(Out of interest, were you proposing to create the style guide? π)
This should probably be done by someone with a modicum of visual design talent, which I don't have π, and familiarity with the Django admin's existing visual style.... which I don't see documented or described anywhere. There are probably people more passionate and experienced about this than me, so I'll defer to any of them.
I can offer some guidance based on a long career building websites, though:
- Scope
-
(Not suggesting that these need to be done as part of this ticket, but they should be kept in mind)
- be mindful that visual theme overlaps with usability, accessibility, brand identity, and widget design
- there are things beyond color that should be made variables (e.g. margins; border width/radius)
- Consistency
-
- variables should be consistently named, and refer to concepts rather than specific use cases or colors
- it should be easy to determine the target of a color: Text? Background? Border? Shadow?
- text/background color variables should always be paired (current implementation makes assumptions about text/background pairings that are difficult to deduce)
- Completeness
-
- every color should be a variable (some are still hardcoded)
- every variable should be documented and an example given
- Accessibility
-
- accessibility issues like visual impairment and color blindness should be considered (good work already done on improving visual contrast)
- usability best practices strongly discourage using color alone to communicate meaning or state (e.g. don't use a red background and assume people will know that means "error". Should be accompanied by verbiage or an icon)
- Maintainability
-
- very helpful for developers would be a visual "crib sheet" that shows all colors/widgets on a single web view. Ideally this would be part of
contrib.admin(so developers can test theme changes) as well as in the online documentation - future commits to stylesheets should be policed for hardcoded colors, ideally via automation
- technical designs should be mindful of ease of maintenance and future expansion (e.g. introduction of other CSS variables for things like border widths)
- very helpful for developers would be a visual "crib sheet" that shows all colors/widgets on a single web view. Ideally this would be part of
Django has excellent documentation and a high-quality/consistent visual design; I'm hoping we can get this formally defined/declared somewhere so plugin/extension authors can build consistently on top of it.
comment:3 by , 4 years ago
I agree with the original poster. It takes a bit of trial and error to skin the admin. Sometimes you skin something in one situation, and then the same variable is used elsewhere, and then it clashes (doesnt work visually) down the line. I also have had clashes with Django CSS custom property names.
Just a side comment: In the web framework context, it is a common pattern to use a two letter acronym for namespacing, e.g. tw for tailwind, ng for angular. We could prefix all variables with dj. e.g. --dj-body-fg.
Another solution would be to place properties under a top level admin container instead of :root, e.g.
.admin-top-level-container {
--body-fg: black;
}
comment:4 by , 4 years ago
| Owner: | changed from to |
|---|---|
| Status: | new β assigned |
comment:5 by , 2 years ago
| Cc: | added |
|---|
comment:6 by , 16 months ago
| Cc: | added |
|---|
comment:7 by , 4 months ago
| Owner: | changed from to |
|---|
comment:8 by , 4 months ago
My Proposed Naming Conventions:
1 Defaults eg
--body-fg
--body-bg
2 Text & Foreground Elements eg
--body-quiet-color -> --body-quiet-fg
--body-medium-color -> --body-medium-fg
--body-loud-color -> --body-loud-fg
--header-color -> --header-fg
--header-link-color -> --header-link-fg
--link-hover-color -> --link-hover-fg
3 Backgrounds & Layout Elements eg
β --selected-row -> --selected-row-bg
β4 Borders & Structural Lines
β--hairline-color -> --hairline-border
To ensure custom third-party admin themes/packages using the old variables don't break, I plan to maintain fallback definitions in :root mapping the old names to the new ones (e.g., --body-quiet-color: var(--body-quiet-fg);).
βPlease let me know if this mapping looks good or if there are additional variables I should include before I open the pull request.
comment:9 by , 4 months ago
| Has patch: | set |
|---|
comment:10 by , 4 months ago
The naming scheme looks good and consistent. Presumably --hairline-color will be paired with --hairline-width and --hairline-style.
Any thoughts on introducing a namespace like dj?
comment:11 by , 4 months ago
Thank you for the feedback. Regarding --hairline-border, I didn't pair it with --hairline-width or --hairline-style because those custom properties don't currently exist in the codebase and donβt fit under the goal of this PR.
Also, regarding the dj namespace, I used a --django- prefix instead. All CSS variables have been updated and still retain backward-compatibility aliases.
I noticed a couple of GitHub Action tests failed after pushing, so I will check those errors next.
comment:12 by , 3 months ago
| Cc: | added |
|---|
comment:13 by , 12 days ago
| Cc: | removed |
|---|---|
| Has patch: | unset |
| Owner: | removed |
| Status: | assigned β new |
Closed my PR. Anyone else is welcome to pick this up.
comment:14 by , 10 days ago
| Owner: | set to |
|---|---|
| Status: | new β assigned |
I'd like to pick this up, following the documentation-first scope accepted in comment:1. Thanks for making the ticket available again.
The current admin theming section gives an override example and links to base.css and dark_mode.css, but doesn't explain the roles of the color variables. I'll start with a small style-guide section covering foreground/background pairs, the quieter/louder text colors, and how light/dark overrides interact. The first patch will document the existing variable names.
I'll check the descriptions against the actual CSS usages, build the documentation, and verify the example in both themes. Would an addition to the existing admin theming section fit the intended scope, or would you prefer a separate contributor-facing style guide?
AI assistance: OpenAI Codex (GPT-6) is helping with the investigation and drafting; I'll include the verification details with the patch.
comment:15 by , 10 days ago
| Has patch: | set |
|---|
βPR adds the documentation-first reference for existing color variables, message foreground/background relationships, and a theme override example.
The HTML build, documentation lint/formatting checks, and all 31 Sphinx extension tests pass. The example was checked in the actual admin templates in automatic, light and dark modes; all 27 sampled text/background contrast checks passed. The local spellchecker reports the same 31 warnings on an unchanged checkout, and EPUBCheck passes with the same seven navigation warnings as that checkout. The PR includes the comparison, browser evidence and Codex disclosure.
The patch keeps the current variable names and runtime styles.
OK, lets accept as a documentation ticket to begin: if we can get the skeleton of a style-guide in place, it'll be easier to see/justify renaming variables.
I've removed the Easy pickings flag as I think is likely a medium sized project to do well. (Out of interest, were you proposing to create the style guide? π)