Theme Developer Tutorial: 3. CSS in Themes

Technically, Discourse uses SCSS to author its stylesheets. However, we’re increasingly moving towards native CSS features as they mature, so the vast majority of themes won’t need to use SCSS features or syntaxes. We follow a variant of BEM for our CSS, you should be familiar with this when writing theme CSS.

This chapter will focus on Discourse-specific subjects, so if you don’t already have a passing familiarity with CSS, take some time to learn about it from CSS styling basics - Learn web development | MDN.

Authoring theme CSS

As we touched on in the last chapter, the main entrypoint for theme CSS is the common/common.scss file. For many themes, that’s all you’ll need.

You can also use desktop/desktop.scss and mobile/mobile.scss, although we’re increasingly moving away from these separate files and towards breakpoint-based styling in common.scss.

But for more complex situations, you can put additional scss in files like /stylesheets/my-styles.scss, and import from common.scss like @import "my-styles";

Using variables

Discourse makes extensive use of CSS variables for colors, font sizes, and other things which need to be shared throughout the stylesheets. You can find a full list of the color variables here, font variables here. Or alternatively, open your browser dev tools, select the <html> element, and scroll through all the available variables.

Let’s make use of this knowledge by updating our theme to use the theme colors for the banner! Open up the common.scss file, and update the color properties to use variables:

.custom-welcome-banner {
  background: var(--quaternary);
  color: var(--secondary);
  text-align: center;
  padding: 10px;
}

discourse_theme will sync this change up to your site instantly, and the change should appear in your browser.

Great! Now your banner’s colors will match the site color scheme, and automatically adjust based on light/dark modes.

For more information about the variables available, check out this document

Finding CSS selectors to style

The number of elements and classes in Discourse can feel quite overwhelming from a re-styling standpoint. The key to having a maintainable theme is to keep your changes as small as possible, and match the selectors used in Discourse core’s stylesheets.

For example, let’s assume you want to style all the buttons in Discourse. One approach would be to use DevTools and try to find every variation of every button and style it. But a better approach would be to see how core is styling buttons, and base your approach on that.

To explore re-styling Discourse in more detail, check out the Designer’s guide to Discourse themes

Or if you’re ready to explore more ways to add/change content in Discourse, let’s go to the next chapter


This document is version controlled - suggest changes on github.

2개의 좋아요

테마의 경우 scss를 사용해야 하는 것 아닌가요, 아니면 stylesheets도 실제로 사용할 수 있나요?

1개의 좋아요

둘 다 작동합니다. 코어 및 플러그인과 일관성을 위해 /stylesheets를 선호하지만, 이제 테마 스켈레톤이 scss/를 사용한다는 걸 깨달았네요. 그래서 99%의 테마가 이 방식을 쓰고 있겠네요 :sweat_smile:

3개의 좋아요

이거 그냥 여기에 슬쩍 끼워 넣으면 6년 전에 스켈레톤에 이 변경 사항을 적용하는 걸 깜빡했다는 걸 모른 척할 수 있을 것 같아 :shushing_face:

5개의 좋아요

그렇다면 기존 테마에서 scss를 stylesheets로 이름 변경하는 것이 합리적인가요? (그리고 플러그인도요? 지금 폰을 쓰고 있어서 플러그인 상태를 확인하지 못했습니다)

1개의 좋아요

전적으로 귀하에게 달려 있습니다. 저희 테마 컴파일러에서는 이 두 가지가 동의어로 취급되며, 현재 scss를 비활성화할 계획은 없습니다.

(비활성화를 결정하게 된다면, 충분한 사전 경고가 제공됩니다.)

이 사항은 테마에만 적용됩니다. 플러그인은 항상 /stylesheets를 관례로 사용해 왔습니다.

3개의 좋아요