网页
综合
更新时间 2026-11-14 16:29:48
HTML theming — Sphinx documentation
自动蜘蛛
www.sphinx-doc.org
正文约 5771 字
Sphinx provides a number of builders for HTML and HTML-based formats.
Added in version 0.6.
This section provides information about using pre-existing HTML themes. If you wish to create your own theme, refer to HTML theme development.
Sphinx supports changing the appearance of its HTML output via themes. A theme is a collection of HTML templates, stylesheet(s) and other static files. Additionally, it has a configuration file which specifies from which theme to inherit, which highlighting style to use, and what options exist for customizing the theme’s look and feel.
Themes are meant to be project-unaware, so they can be used for different projects without change.
Using a theme provided with Sphinx is easy. Since these do not need to be installed, you only need to set the html_theme config value. For example, to enable the classic theme, add the following to conf.py:
You can also set theme-specific options using the html_theme_options config value. These options are generally used to change the look and feel of the theme. For example, to place the sidebar on the right side and a black background for the relation bar (the bar with the navigation links at the page’s top and bottom), add the following conf.py:
If the theme does not come with Sphinx, it can be in two static forms or as a Python package. For the static forms, either a directory (containing theme.toml and other needed files), or a zip file with the same contents is supported. The directory or zipfile must be put where Sphinx can find it; for this there is the config value html_theme_path. This can be a list of directories, relative to the directory containing conf.py, that can contain theme directories or zip files. For example, if you have a theme in the file blue.zip, you can put it right in the directory containing conf.py and use this configuration:
The third form is a Python package. If a theme you want to use is distributed as a Python package, you can use it after installing
Once installed, this can be used in the same manner as a directory or zipfile-based theme:
For more information on the design of themes, including information about writing your own themes, refer to HTML theme development.
Sphinx comes with a selection of themes to choose from.
Note that from these themes only the Alabaster and Scrolls themes are mobile-optimated, the other themes resort to horizontal scrolling if the screen is too narrow.
This is a basically unstyled layout used as the base for the other themes, and usable as the base for custom themes as well. The HTML contains all important elements like sidebar and relation bar. There are these options (which are inherited by the other themes):
nosidebar (true or false): Don’t include the sidebar. Defaults to False.
sidebarwidth (int or str): Width of the sidebar in pixels. This can be an int, which is interpreted as pixels or a valid CSS dimension string such as ‘70em’ or ‘50%’. Defaults to 230 pixels.
body_min_width (int or str): Minimal width of the document body. This can be an int, which is interpreted as pixels or a valid CSS dimension string such as ‘70em’ or ‘50%’. Use 0 if you don’t want a width limit. Defaults may depend on the theme (often 450px).
body_max_width (int or str): Maximal width of the document body. This can be an int, which is interpreted as pixels or a valid CSS dimension string such as ‘70em’ or ‘50%’. Use ‘none’ if you don’t want a width limit. Defaults may depend on the theme (often 800px).
navigation_with_keys (true or false): Allow navigating with the following keyboard shortcuts:
Left arrow: previous page
Right arrow: next page
enable_search_shortcuts (true or false): Allow jumping to the search box with / and allow removal of search highlighting with Esc.
globaltoc_collapse (true or false): Only expand subsections of the current document in globaltoc.html (see html_sidebars). Defaults to True.
Added in version 3.1.
globaltoc_includehidden (true or false): Show even those subsections in globaltoc.html (see html_sidebars) which have been included with the :hidden: flag of the toctree directive. Defaults to False.
Added in version 3.1.
globaltoc_maxdepth (int): The maximum depth of the toctree in globaltoc.html (see html_sidebars). Set it to -1 to allow unlimited depth. Defaults to the max depth selected in the toctree directive.
Added in version 3.2.
Alabaster theme is a modified “Kr” Sphinx theme from @kennethreitz (especially as used in his Requests project), which was itself originally based on @mitsuhiko’s theme used for Flask & related projects. Refer to its installation page for information on how to configure html_sidebars for its use.
This is the classic theme, which looks like the Python 2 documentation. It can be customized via these options:
rightsidebar (true or false): Put the sidebar on the right side. Defaults to False.
stickysidebar (true or false): Make the sidebar “fixed” so that it doesn’t scroll out of view for long body content. This may not work well with all browsers. Defaults to False.
collapsiblesidebar (true or false): Add an experimental JavaScript snippet that makes the sidebar collapsible via a button on its side. Defaults to False.
externalrefs (true or false): Display external links differently from internal links. Defaults to False.
There are also various color and font options that can change the color scheme without having to write a custom stylesheet:
footerbgcolor (CSS color): Background color for the footer line.
footertextcolor (CSS color): Text color for the footer line.
sidebarbgcolor (CSS color): Background color for the sidebar.
sidebarbtncolor (CSS color): Background color for the sidebar collapse button (used when collapsiblesidebar is True).
sidebartextcolor (CSS color): Text color for the sidebar.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。