======================= Theme variables and CSS ======================= .. _pydata-css-variables: https://github.com/pydata/pydata-sphinx-theme/blob/main/src/pydata_sphinx_theme/assets/styles/variables/ .. _css-variable-help: https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties This section covers a few ways that you can control the look and feel of your theme via your own CSS and theme variables. .. _custom-css: Custom CSS Stylesheets ====================== You may customize the theme's CSS by creating a custom stylesheet that Sphinx uses to build your site. Any rules in this style-sheet will over-ride the default theme rules. To add a custom stylesheet, follow these steps: 1. **Create a CSS stylesheet** in ``_static/css/custom.css``, and add the CSS rules you wish. 2. **Attach the stylesheet to your Sphinx build**. Add the following to ``conf.py`` .. code-block:: python html_static_path = ['_static'] html_css_files = [ 'css/custom.css', ] When you build your documentation, this stylesheet should now be activated. CSS theme variables =================== This theme defines several `CSS variables `_ that can be used to quickly control behavior and display across your documentation. These are based on top of the basic `Bootstrap CSS variables `_ extended with some theme specific variables. base variables -------------- In order to change a variable, follow these steps: 1. :ref:`Add a custom CSS stylesheet `. This is where we'll configure the variables. 2. Underneath a ``:root`` section, add the variables you wish to update. For example, to update the base font size, you might add this to ``custom.css``: .. code-block:: css :root { --pst-font-size-base: 17px; } .. important:: Note that these are `CSS variables `_ and not `SASS variables `_. The theme is defined with CSS variables, not SASS variables! Refer to the previous section if you desire a different behavior between the light and dark theme. For a complete list of the theme variables that you may override, see the `theme variables defaults CSS file `_: .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_layout.scss :language: scss .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_fonts.scss :language: scss .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_icons.scss :language: scss .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_admonitions.scss :language: scss .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_versionmodified.scss :language: scss Color variables --------------- There are two special color variables for primary and secondary theme colors (``--pst-color-primary`` and ``--pst-color-secondary``, respectively). These are meant to complement one another visually across the theme, if you modify these, choose colors that look good when paired with one another. There are also several other color variables that control color for admonitions, links, menu items, etc. Each color variable has two values, one corresponding to the "light" and one for the "dark" theme. These are used throughout many of the theme elements to define text color, background color, etc. Here is an overview of the colors available in the theme (change theme mode to switch from light to dark versions). .. raw:: html

primary secondary success info warning danger background on-background surface on-surface target

**To modify the colors for these variables** for light and dark themes, :ref:`add a custom CSS stylesheet ` with a structure like so: .. code-block:: css html[data-theme="light"] { --pst-color-primary: black; } html[data-theme="dark"] { --pst-color-primary: white; } This theme uses shadows to convey depth in the light theme mode and opacity in the dark one. It defines 4 color variables that help build overlays in your documentation. - :code:`background`: color of the back-most surface of the documentation - :code:`on-background` elements that are set on top of this background (e.g. the header navbar on dark mode). - :code:`surface` elements set on the background with a light-grey color in the light theme mode. this color has been kept in the dark theme (e.g. code-block directives). - :code:`on-surface` elements that are on top of :code:`surface` elements (e.g. sidebar directives). The following image should help you understand these overlays: .. raw:: html

background

on-background

surface

on-surface

.. it would be nice to have this `.. literalinclude::` here to actually show the file, but there's a pygments bug that fails to lex SCSS variables (specifically the `$` symbol that prepends SCSS variables, see https://github.com/pygments/pygments/issues/2130). So for now it's just a raw download link. For a complete list of the theme colors that you may override, see the :download:`PyData theme CSS colors stylesheet <../../src/pydata_sphinx_theme/assets/styles/variables/_color.scss>`. Configure pygments theme ======================== As the Sphinx theme supports multiple modes, the code highlighting colors can be modified for each one of them by modifying the ``pygment_light_style`` and ``pygment_dark_style``. You can check available Pygments colors on this `page `__. .. code-block:: python html_theme_options = { ... "pygment_light_style": "tango", "pygment_dark_style": "native" } .. danger:: The native Sphinx option ``pygments_style`` will be overwritten by this theme.