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

时间:2014-05-04 22:53:21

标签: python python-sphinx

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

# conf.py
html_static_path = ['_static']
html_theme = 'default'
html_theme_options = {
  'cssfiles': ['_static/style.css']
}

结果:

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

4 个答案:

答案 0 :(得分:41)

更简单的方法是将其添加到conf.py

def setup(app):
    app.add_stylesheet('css/custom.css')  # may also be an URL

然后将文件放入_static/css/文件夹。

答案 1 :(得分:23)

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

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

然后在_templates中,您将创建一个名为' layout.html'的默认主题的扩展名。这将包括你的cssfiles,如。

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

{% set css_files = css_files + ['_static/style.css'] %}

有关详细信息,请参阅有关templating的sphinx文档。

答案 2 :(得分:9)

您可以通过html_theme_options配置的选项取决于主题。查看主题[options]的{​​{1}}部分,了解可用的内容。

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

script_files

(如需参考,请查看Sphinx的html_context = { 'css_files': ['_static/custom.css'], } 并查看how self.globalcontext gets populated there。)

答案 3 :(得分:1)

我正在使用Sphinx 3.2。

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

  • conf.py下的html_static_path = ['_static']中添加以下行:
html_css_files = ['css/custom.css']
  • 转到docs/_static/并添加css/custom.css

  • 将自定义CSS添加到文件中,然后$ make html

Source