Making custom CSS changes on your site

:bookmark: 本指南介绍了如何在 Discourse 网站上进行 CSS 更改,包括 CSS 简介、在 Discourse 中添加 CSS 的位置以及如何使用浏览器检查工具查找正确的选择器。

:person_raising_hand: 所需用户级别:管理员

摘要

本指南涵盖:

  1. CSS 简介和关键概念
  2. 如何使用主题组件将 CSS 添加到 Discourse 网站
  3. 使用浏览器检查工具查找正确的 CSS 选择器

理解 CSS 基础知识

CSS 代表层叠样式表。以下是三个需要理解的关键概念:

  1. 结构:CSS 规则由选择器、属性和值组成。
p {
  color: red;
}
  • 选择器:p(定位所有 HTML <p> 标签)
  • 属性:color
  • 值:red

  1. 层叠:最后应用的规则优先。例如:
p {
  color: red;
}
p {
  color: green;
}

段落将显示为绿色,因为这是最后应用的规则。

  1. 特异性:更具体的规则会覆盖不那么具体的规则。例如:
div p {
  color: green;
}
p {
  color: red;
}

div 中的段落将保持绿色,因为 div pp 更具体。

将 CSS 添加到 Discourse 网站

要将 CSS 添加到 Discourse 网站:

  1. 从侧边栏转到 Appearance > Themes & components,或访问您网站的以下网址:https://yoursite.com/admin/customize/components

  2. 分别点击“Install”和 :heavy_plus_sign: “Create New”。

  3. 为您的主题组件命名,然后点击“Create”。

  4. 选择要应用组件的主题。

  5. 点击“Edit Code”。

  6. 将您的 CSS 添加到 CSS 选项卡。

  7. 点击“Save”以应用您的更改。

:warning: 如果您有多个用户可选择的主题,请确保将您的主题组件添加到所有相关主题。

查找正确的 CSS 选择器

使用浏览器检查工具查找正确的 CSS 选择器:

  1. 右键点击您要修改的元素。

  2. 从上下文菜单中选择“Inspect”。

  3. 在开发者工具面板中,找到元素选择器。

  4. 点击相应的选择器,然后添加您的 CSS 规则。

  5. 复制规则,将其粘贴到您的主题组件的 CSS 部分,然后删除其他规则。

要获得更具体选择器,您可能需要复制 Discourse 现有样式中使用的选择器,并在您的主题组件中进行修改。

以下视频演示了上述步骤:

:information_source: 如果您的更改未生效,请检查:

  • 主题组件是否已在您的主题中启用。
  • 您的 CSS 规则是否足够具体,可以覆盖现有样式。

附加资源

39 个赞
Policy notice: no basic HTML / CSS questions please
How can hide the 'Latest', 'Top', 'Badges' and 'Categories' in the hamburger menu?
Anyway to style discourse topic title? ( IE, red, bold, highlight, anything. )
Remove background color change after page loading
How to make pinned items have less text underneath them
Notification color: light blue
Customising Login Page
How to disable all alert banners in discourse
Disable bio, location and profile picture
Prevent users from changing their Full Names
Is it possible to change vis CSS the Category Moderator shield?
Line under avatar?
Custom CSS/HTML in non-default theme (without component)
Advanced Header Search
Edit or Disable "community" in sidebar menu
How do I change text color in my Header?
Customize posts' contents with your own styles
Toolbar under site header per category
How to hide 0 votes?
Change Default Badge Colors (Bronze, Silver, Gold)
🌐 www theme
How to change search banner headline font size?
Cloudfront cdn with discourse?
How to reduce the size of category logo image
Can I make a custom button with CSS and HTML?
Applying Scroll Bar (Horizontal & Vertical) to Code Snippets
Hide suspension status
How can I hide the add button from sidebar
Forum homepage, how to make full screen?
Customizing FAQs and Guidelines
Switching the discourse color scheme: Changing Red Hues in CSS
Selected Quote Background Color
How can i disable categories on home page?
How to remove bottom bar from all topics
How to change layout of login screen
Can you change the color of customizable text?
Policy notice: no basic HTML / CSS questions please
How to resize 'Modern Category + Group Boxes' thumbnail via CSS
Category List with Banners
Customize Login pop-up box?
Search Banner image cuts off on smaller screens
"Reader Mode" theme component feedback
How to change the buttons' colors on hover?
How to remove Tos and privacy from ui?
How to add separator lines to the Welcome Banner sections?
Understanding link click counters
Watching category mandatory
Show Only Categories on Mobile View
Change URL or remove Powered by Discourse
Wishlist: visual cue for "user got a badge for this post"
Remove "pinned" button at bottom of topic
How to style category boxes?
vBulletin Emojis
Can We Represent Our Categories Like This on a Discourse Hosted Community
How to Edit the Menu Design and User Profile Design
Rounded login buttons
How can I make the home page similar to this one?
How do I add CSS?
Show sidebar to anonymous user + Welcome area in central theme
Remove search from the header
Replies and Views Orange Color, help!
Differentiate Admin and Moderator Shield Icons
Configuring the default sidebar for your users
How to use CSS for versatile-banner
Turn off the “views” on topic
CSS edits not being applied on mobile
How to make login and registration pages look like this?
Theme Identification
How to customize the composer toolbar?
Which path or file handles the custom template CSS?
Top contributors sidebar Backgound
Homepage Feature
Which is Better? Discourse or Flarum?
Is there a way to link replies after they are posted?
Customize User Cards
Change hover background color for nav class buttons
Allow “Name" field to be disabled in registration dialog
How can discourse administrators turn off personal messages
How to change/remove icon on login screen?
Easy Responsive Footer
Versatile Banner
Versatile Banner
Howto add a GAP between Categories
How can I add a menu item
Can't remove topic footer buttons on iOS
Discourse Docs
Support for Umami analytic
Hiding empty votes via plugin options
New default nav style and simplified color scheme
How can we either hide or grey out the upload icon in the Edit window? We have uploading disabled
Can the user avatars on the website and the content page of the post be hidden?
Removing category descriptions in main page, but retain them in individual category pages?
How to disable title changes for all users on Discourse?
Beginner's guide to using Discourse Themes
Trying to re-align an element of a theme and just wondering what it's called
Selected Quote Background Color
Differentiate contents from the topic list rows using different colors
Adjusting default composer height
Removing options from user activity action list
Understanding and managing post notices
Understanding and managing post notices
Is there a way to remove the pop-ups when making a new topic?
Looking for the perfect theme
Default category heading not hidden when category banner is used
Adding a table to your post using markdown
Search section like on Reddit?
Search section like on Reddit?
How to hide a button (SVG icon) if user is logged in?
Change the headings of preferences settings

能否将其设为 wiki?“我在哪里添加 CSS?”标题下的信息需要更新,因为如果您安装了现有主题,将不再有“编辑 CSS/HTML”按钮——如果有人通过新的设置向导选择了主题,现在很可能就是这种情况。现在我们只使用添加到现有主题的主题组件。

一旦它被设为 wiki,我就可以更新它。

10 个赞

好的,现在完成了 :+1:

5 个赞