伪元素
那么,你想在你的 Discourse 站点中插入一些文本或图片。
关于文本,在大多数情况下,从 /admin/customize/site_texts 修改即可。
不过有时,我们希望在两个不同的区块之间添加一句话,而非修改现有的元素。
让我们看看如何使用 CSS3 中的属性 :before 和 :after 来实现。
基本步骤
-
为了进行操作,我们需要使用主流浏览器中自带的开发者控制台。要打开它,只需按 F12。

-
选择你想要添加文本的元素,
before或after。
可以看到,每当鼠标经过一个元素时,该元素会被高亮,对应的类会自动在右侧的 HTML 和 CSS 面板中被选中。在编辑 Discourse 样式表之前,先做一次实时测试,因此在选好要操作的元素后,只需点击 :heavy_plus_sign: 图标。这会在 CSS 中添加一条新规则,我们可以根据需要修改它。
-
开始编辑。为类添加后缀
:before或:after,并使用content属性添加文本。如果你不了解 CSS,添加图片会稍微复杂一些,但遵循类似的模式是一个好的做法:
background-image: url("url-goes-here"); background-repeat: no-repeat; background-size: your-value; content: ""; width: your-value; height: your-value; display: inline-block;早于:

而晚于:

请记住,文本会出现在你所选的特定类被使用的所有位置。有时你需要指定新文本要出现在哪个元素上,这时需要在 CSS 中加上父元素。
-
对其进行自定义。只要懂一点 CSS,就可以轻松地按自己的需要自定义文本的样式。

.fancy-title::after { content: "ANOTHER TEXT " "\f072"; color: violet; font-family: Fontawesome; background: linear-gradient(to right, #7ce5df 27%, #f1da36 100%); font-size: 18px; padding: 2px 4px; border: 1px solid; } -
满意之后,通过创建主题组件把你的更改添加到站点的 CSS 中。
下面继续给出一些实际示例:
-
话题标题
对某些站点而言,在标题或每个帖子早于或晚于的位置添加图片、横幅或个性化广告可能会有用。

#topic-title::before { background-image: url(your-URL-here); background-repeat: no-repeat; background-size: 750px 335px; width: 750px; height: 335px; display: inline-block; content: ""; } #topic-title::after { background-image: url(your-URL-here); background-repeat: no-repeat; background-size: 800px 295px; width: 800px; height: 295px; display: inline-block; content: ""; } -
话题正文

.topic-body.clearfix::before { background-image: url(your-URL-here); max-height: 2.8571em; width: 690px; height: 184px; background-size: auto 2.8571em; background-repeat: no-repeat; margin-left: 11px; margin-bottom: 0.25em; }或晚于:

只需把
.topic-body.clearfix::before改为.topic-body.clearfix::after。 同样,也可以添加纯文本before或after:.topic-body.clearfix::before { content: "DISCOURSE ROCKS!"; color: red; font-weight: bold; padding-left: 11px; }

-
帖子按钮


.nav.post-controls .actions::before { color: red; content: "Hello from Discourse"; } -
时间轴

.topic-timeline::before { color: red; content: "Hello World"; } .topic-timeline::after { color: red; content: "Hello again"; } .timeline-scroller-content::before { color: violet; content: "Hey,"; } .timeline-scroller-content::after { color: violet; content: "It's me again!"; } .timeline-container .topic-timeline .start-date::before { color: goldenrod; content: "Start Date "; } timeline-container .topic-timeline .start-date::after { color: goldenrod; content: " \f060"; font-family: Fontawesome; } .widget-link.now-date::before { content: "\f061 "; color: burlywood; font-family: Fontawesome; } .widget-link.now-date::after { color: burlywood; content: " Now Date"; } -
页脚按钮

#topic-footer-buttons::before { content: "THESE ARE FOOTER BUTTONS"; color: indianred; border: 2px solid; padding: 3px; } #topic-footer-buttons::after { color: indianred; content: "CONTENT AFTER GO HERE"; border: 2px solid; }在后一种情况下,需要注意
:after的内容是插入在一段文本之后的。如果不需要特殊自定义,建议通过/admin/customize/site_texts更改原文,而不是编辑 CSS。 -
推荐话题

#suggested-topics::before { content: ""; background-image: url(https://d11a6trkgmumsb.cloudfront.net/original/3X/1/0/101f03af29f12ea30e1226eb96a02c3ed2f6d2ef.png); width: 690px; height: 184px; background-size: 690px 184px; background-repeat: no-repeat; display: inline-block; } #suggested-topics::after { content: ""; background-image: url(https://d11a6trkgmumsb.cloudfront.net/original/3X/1/0/101f03af29f12ea30e1226eb96a02c3ed2f6d2ef.png); width: 690px; height: 184px; background-size: 690px 184px; background-repeat: no-repeat; display: inline-block; } -
话题操作

.widget-button.btn-flat.share.no-text.btn-icon::after { content: " Share"; } .widget-button.btn-flat.toggle-like.like.no-text.btn-icon::after { content: " Like"; }
参考:https://meta.discourse.org/t/insert-text-disclaimer-anywhere-in-discourse/99009
注意: 由
::before和::after生成的伪元素被包含在元素的格式化框中,因此不适用于诸如<img>之类的_替换元素_,也不适用于<br>元素。来源:https://developer.mozilla.org/en-US/docs/Web/CSS/::after
换句话说,这对没有子元素的“自闭合”元素不起作用。直觉上会认为伪元素是这样的:
{::before is here}<tag>text content</tag>{::after is here}
但实际上它是
<tag>{::before is here}text content{::after is here}</tag>
谢谢你的反馈!