为具有特定标题的通用狮身人面像警告定义标记
我正在使用 Sphinx 为 Python 程序生成 HTML 文档。
我想使用通用警告
指令具有特定的标题,并以我可以定义的方式对其进行标记,例如使用 note
指令生成的内容,即盒装的,但具有不同的颜色(大多数<一href="https://www.sphinx-doc.org/en/master/usage/restructedtext/basics.html#directives" rel="nofollow noreferrer">警告没有特殊样式)。
我该如何最好地解决这个问题?
I am using Sphinx to generate HTML documentation for a Python program.
I would like to use the generic admonition
directive with a specific title and have it marked up in a way I can define, for example like content generated with the note
directive, i.e., boxed, but with a different color (most Admonitions are not specially styled).
How do I best go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我正确理解你的问题,你想对警告应用自定义 CSS 样式。您可以使用 :class: 属性来做到这一点。
例如,以下
呈现为
您然后添加您自己的样式表。例如,通过 source 目录中的 _templates 目录中的自定义 layout.html:
然后您可以在您的使用 myownstyle 类的选择器的样式表
If I understood your question correctly, you'd like to apply a custon CSS style to the admonition. You can do this with a :class: attibute.
For example, the following
renders as
You then add your own style sheet. For example, by a custom layout.html in the _templates directory in your source directory:
Then you can play around with CSS styles in your style sheet using a selector for the myownstyle class
要更轻松地添加 css 文件,请将它们放在
_static
文件夹中,然后将其添加到conf.py
中:To add css files more easily, put them in the
_static
folder and then add this toconf.py
:下面是一个示例 custom.css,用于覆盖
myownstyle
警告的标题颜色和背景颜色。我在conf.py
中使用了app.add_stylesheet()
调用。Here is an example custom.css for overwriting the color of the title and background color of the
myownstyle
admonition. I used theapp.add_stylesheet()
call inconf.py
.以下是我对此的看法:
添加静态文件需要满足的要求:
在
conf.py
中:添加以下内容:
html_static_path = ['_static']
添加 css 文件的路径:
要添加到列表
html_css_files
的 CSS 文件的名称:<前><代码> html_css_files = [
'css/some_file.css',
'css/custom.css', # 创建自定义 CSS 文件并添加
]
注意:作为推论,CSS 文件的位置将为(路径):
_static/css/custom.css(或任何其他选择的文件夹名称来代替 css)
将类添加到admonition(或指令) - 详细信息此处
如@Bud 此处所述(为简洁起见而添加):
创建文件
custom.css
(如果尚未完成)并添加以下内容:告别说明:截至今天 (2024) ,我们可以完成上述所有操作,而无需创建我们自己的(自定义)
layout.html
在 Sphinx v 7.2.6、Python 3.9
中测试
对于 @Mad Physicist,人们可以随时尝试:如何将类名称添加到告诫...。 (实际上,我正在寻找在我的页面中创建浮动框的方向,并且意外(或者由于Google著名算法的仁慈)碰巧来到了这里.
Here are my take on this:
Requirements to be fulfilled for adding static files:
In
conf.py
:Add the following:
html_static_path = ['_static']
Adding path to css files:
Name of the css file to be be added to list
html_css_files
:Note: As a corollary, the location of the CSS file would be (path):
_static/css/custom.css
(or any other folder name of choice in place of css)Add a class to admonition (or a directive) - details here
As detailed above by @Bud here (being added for brevity):
Create file
custom.css
(if not done already) and add the following:A parting note: As of today (2024), we may do all the above without having to take recourse to creating our own (custom)
layout.html
Tested in Sphinx v 7.2.6, Python 3.9
To @Mad Physicist, one can always take a shot at: how to add class name to an admonition to .... (Actually I was looking for some direction at creating floating box in my page/s, and by accident (or by mercy of Google's famous algo) happened to land here.