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

kfgdxczn  于 5个月前  发布在  其他
关注(0)|答案(4)|浏览(86)

如何添加自定义CSS文件?以下conf.py不起作用:

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

字符串
测试结果:

$ 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

ct3nt3jp

ct3nt3jp1#

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

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

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

p4tfgftt

p4tfgftt2#

你应该能够通过扩展默认的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 layout of the theme. #}
{% extends "!layout.html" %}

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


请参阅sphinx在templating上的文档以获取更多信息。

fhg3lkii

fhg3lkii3#

您可以通过html_theme_options配置的选项取决于主题。查看主题theme.conf[options]部分以了解可用的选项。
但是,在全局基础上,您可以在conf.py中定义html_context以覆盖css_files的设置(以及script_files):

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

字符串
(For参考,看看Sphinx的builders.html.StandaloneHTMLBuilder.prepare_writing(),看看self.globalcontext是如何填充的。

vvppvyoh

vvppvyoh4#

我用的是Sphinx 3.2。
我可以通过以下方式添加一些简单的自定义CSS:

  • html_static_path = ['_static']下面的conf.py中添加这一行:
html_css_files = ['css/custom.css']

字符串

  • 转到docs/_static/并添加css/custom.css
  • 添加自定义CSS到您的文件,然后$ make html

Source

相关问题