Making CSS changes on your Discourse site
This guide explains how to make CSS changes on your Discourse site, including an introduction to CSS, where to add CSS in Discourse, and how to find the right selectors using browser inspection tools.
Required user level: Administrator
Summary
This guide covers:
- A brief introduction to CSS and key concepts
- How to add CSS to your Discourse site using theme components
- Using browser inspection tools to find the right CSS selectors
Understanding CSS basics
CSS stands for Cascading Style Sheets. Here are three key concepts to understand:
- Structure: A CSS rule consists of a selector, property, and value.
p {
color: red;
}
- Selector:
p
(targets all HTML<p>
tags) - Property:
color
- Value:
red
- Cascade: The last rule applied takes precedence. For example:
p {
color: red;
}
p {
color: green;
}
The paragraphs will be green because it’s the last rule applied.
- Specificity: More specific rules override less specific ones. For example:
div p {
color: green;
}
p {
color: red;
}
Paragraphs inside divs will remain green because div p
is more specific than p
.
Adding CSS to your Discourse site
To add CSS to your Discourse site:
-
Go to
Appearance > Components
from sidebar or follow this url for your site:https://yoursite.com/admin/customize/components
-
Click Install and Create New respectively.
-
Name your theme component and click Create new
-
Choose which themes to apply the component
-
Click Edit CSS/HTML
-
Add your CSS to one of these display options (Common, Desktop, or Mobile).
-
Click “Save” to apply your changes.
If you have multiple user-selectable themes, make sure to add your theme components to all relevant themes.
Finding the right CSS selectors
Use browser inspection tools to find the correct CSS selectors:
-
Right-click on the element you want to modify.
-
Select “Inspect” from the context menu.
-
In the developer tools panel, locate the element selectors.
-
Click to respective selectors and add your CSS rule.
-
Copy the rule, paste it into your theme component’s CSS section and remove other rules.
It’s possible to apply styles only for Desktop
or Mobile
view by selecting respective tabs. Leave Common
to apply both.
For more specific selectors, you may need to copy the selector used in Discourse’s existing styles and modify it in your theme component.
Here’s a video demonstrating of above steps:
If your changes aren’t applied, check that:
- The theme component is enabled on your themes.
- Your CSS rule is specific enough to override existing styles.
Additional resources
- MDN CSS Documentation
- Chrome DevTools Documentation
- Freecodecamp
- Beginner’s Guide to Using Discourse Themes
Last edited by @yigit 2024-08-08T08:21:50Z
Check document
Perform check on document: