如何将自定义css文件添加到Sphinx?

ole*_*ole 34 python python-sphinx

我如何添加自定义css文件?以下配置对我不起作用:

# conf.py
html_static_path = ['_static']
html_theme = 'default'
html_theme_options = {
  'cssfiles': ['_static/style.css']
}
Run Code Online (Sandbox Code Playgroud)

结果:

C:\temp\test-docs\docs>make html
Running Sphinx v1.2.2
loading pickled environment... not yet created
building [html]: targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
reading sources... [ 50%] help
reading sources... [100%] index

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents...
Theme error:
unsupported theme option 'cssfiles' given
Run Code Online (Sandbox Code Playgroud)

Gri*_*ave 45

一种更简单的方法是将其添加到您的conf.py:

def setup(app):
    app.add_stylesheet('css/custom.css')  # may also be an URL
Run Code Online (Sandbox Code Playgroud)

然后将文件放入_static/css/文件夹中.

  • 嗯这似乎对我不起作用 (4认同)
  • 也许值得注意的是[alabaster主题](https://alabaster.readthedocs.io/en/latest/)在源文件夹中使用`_static/custom.css`文件开箱即用,所以`add_styleshee(' custom.css')`这个主题不需要`. (4认同)
  • 通过`app.add_javascript('file.js')`添加javascript文件. (3认同)
  • 谢谢 - 这应该是公认的答案! (2认同)

Col*_*ole 23

您应该能够通过扩展默认的sphinx主题来包含自定义css.在conf.py中,您可以指定主题扩展的位置,例如.

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Run Code Online (Sandbox Code Playgroud)

然后在_templates中,您将创建一个名为"layout.html"的默认主题的扩展名,其中包含您的cssfiles,例如.

{# layout.html #}
{# Import the layout of the theme. #}
{% extends "!layout.html" %}

{% set css_files = css_files + ['_static/style.css'] %}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅sphinx有关模板的文档.

  • "通过在带有感叹号的被覆盖模板的名称前面加上前缀,Sphinx将从底层HTML主题加载布局模板." 我已经添加了一个关于模板的sphinx文档的链接.主要是调用该行来扩展默认的sphinx layout.html,或者您可能安装的任何主题. (2认同)
  • 对于链接来说,`add_stylesheet`解决了我的问题. (2认同)

小智 13

我正在使用狮身人面像 3.2。

我可以通过执行以下操作添加一些简单的自定义 CSS:

  • conf.py在下面添加这一行html_static_path = ['_static']
html_css_files = ['css/custom.css']
Run Code Online (Sandbox Code Playgroud)
  • 转到docs/_static/并添加css/custom.css

  • 然后将自定义 css 添加到您的文件中$ make html

来源


igo*_*gor 10

您可以通过html_theme_options主题配置选项.查看[options]主题部分,theme.conf了解可用的内容.

但是,在全球范围内,您可以html_context在您的内容中定义conf.py以覆盖css_files(并且,就此script_files而言)的设置:

html_context = {
    'css_files': ['_static/custom.css'],
}
Run Code Online (Sandbox Code Playgroud)

(作为参考,请看看Sphinx的builders.html.StandaloneHTMLBuilder.prepare_writing(),看看它是如何self.globalcontext填充的.)

  • 谢谢!在提供的答案中,这是帮助我覆盖 basic.css 中烦人的自动断字的原因。 (2认同)