简体   繁体   English

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

[英]How to add custom css file to Sphinx?

How can I add a custom css file?如何添加自定义 css 文件? The following config does not work:以下配置不起作用:

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

Result:结果:

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

A simpler way is to add this to your conf.py :一种更简单的方法是将其添加到您的conf.py

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

Then put the file into the _static/css/ folder.然后将文件放入_static/css/文件夹。

You should be able to include custom css by extending the default sphinx theme.您应该能够通过扩展默认的 sphinx 主题来包含自定义 css。 In your conf.py you would specify where your extension to the theme would be, such as.在您的 conf.py 中,您将指定主题扩展的位置,例如。

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

Then in _templates you would create a extension to the default theme named 'layout.html' that would include your cssfiles such as.然后在 _templates 中,您将为名为“layout.html”的默认主题创建一个扩展,其中将包含您的 cssfiles,例如。

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

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

See sphinx's documentation on templating for more information.有关更多信息,请参阅 sphinx 的模板文档。

The options that you can configure via html_theme_options are theme-dependent.您可以通过html_theme_options配置的选项取决于主题。 Check out the [options] section of your theme's theme.conf to find out what is available.查看主题的theme.conf[options]部分以了解可用的内容。

On a global basis, though, you can define html_context in your conf.py to override the settings for css_files (and, for that matter, script_files too):在全球范围内,但是,你可以定义html_contextconf.py来覆盖设置css_files (和,对于这个问题, script_files太):

html_context = {
    'css_files': ['_static/custom.css'],
}

(For reference, have a look at Sphinx's builders.html.StandaloneHTMLBuilder.prepare_writing() and see how self.globalcontext gets populated there .) (作为参考,请查看 Sphinx 的builders.html.StandaloneHTMLBuilder.prepare_writing()了解self.globalcontext如何self.globalcontext那里填充。)

I'm using Sphinx 3.2.我正在使用 Sphinx 3.2。

I was able to add some simple custom CSS by doing the following:通过执行以下操作,我能够添加一些简单的自定义 CSS:

  • add this line in conf.py right under html_static_path = ['_static'] :conf.py html_static_path = ['_static']下添加这一行:
html_css_files = ['css/custom.css']
  • go to docs/_static/ and add css/custom.css转到docs/_static/并添加css/custom.css

  • add custom css to your file then $ make html将自定义 css 添加到您的文件中,然后$ make html

Source来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM