我可以将自定义 CSS 样式应用于特定帖子吗?

发布于 2025-01-09 21:13:58 字数 168 浏览 2 评论 0原文

如果我没记错的话,帖子的 css 属性与 postpost-content 类相关,所以如果我重写它们的一些属性,将适用于我网站上的所有帖子。

我想仅将一些 css 属性应用于特定帖子,我该怎么做?

The css properties of a post are related to post and post-content classes if I am not wrong so if I override some of their properties this will apply to all posts in my site.

I would like to apply apply some css properties only to a particular post, how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2025-01-16 21:13:58

这里有两种方法:

解决方案 1:额外的 css 文件

最好的方法是使用 页面资源。您可以浏览 页面包 中的文件并查找 CSS 文件。然后,您应该使用以下方法将 CSS 文件添加到您的 head/content 中:

{{ with .Resources.GetMatch "*.css" }}
  <style>{{ .Content | safeCSS }}</style>
{{ end }}

解决方案 2:body 类

一种更简单(但更混乱)的方法是向您的 body 添加一个类。看起来像这样:

<body class="{{ with .File }}{{ .BaseFileName }}{{ end }}">

这将使用页面的 slug 添加一个类到主体中。这样,您可以通过向主样式表添加规则来应用特定于页面的样式。

Here are two ways to do it:

Solution 1: extra css file

The nicest way to do this is with page resources. You can walk over the files in the page bundle and look for a CSS file. You should then add the CSS file to your head/content using:

{{ with .Resources.GetMatch "*.css" }}
  <style>{{ .Content | safeCSS }}</style>
{{ end }}

Solution 2: body class

A simpler (but messier) way is to add a class to your body. This looks like this:

<body class="{{ with .File }}{{ .BaseFileName }}{{ end }}">

This will add a class to the body with the slug of your page. This way you can apply page specific styling, by adding rules to your main stylesheet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文