Debian Codegraph @

docs/user_guide/styling.rst

raw
1 =======================
2 Theme variables and CSS
3 =======================
4
5 .. _pydata-css-variables: https://github.com/pydata/pydata-sphinx-theme/tree/main/src/pydata_sphinx_theme/assets/styles/variables
6 .. _css-variable-help: https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Cascading_variables/Using_custom_properties
7
8 This section covers a few ways that you can control the look and feel of your theme via your own CSS and theme variables.
9
10 .. _custom-css:
11
12 Custom CSS Stylesheets
13 ======================
14
15 You can customize the theme's CSS by creating a custom stylesheet. This stylesheet will be used by Sphinx while building your site.
16 Any rules in this style sheet will override the default theme rules.
17
18 .. seealso::
19
20 For a more in-depth guide in linking static CSS and JS assets in your site, see :doc:`static_assets`.
21
22
23 To add a custom stylesheet, follow these steps:
24
25 1. **Create a CSS stylesheet** in ``_static/css/custom.css``, and update the CSS rules as desired.
26 2. **Attach the stylesheet to your Sphinx build**. Add the following to ``conf.py``:
27
28 .. code-block:: python
29
30 html_static_path = ['_static']
31
32 html_css_files = [
33 'css/custom.css',
34 ]
35
36 When you build your documentation, this stylesheet should now be activated.
37
38 .. _css-variables:
39
40 CSS theme variables
41 ===================
42
43 This theme defines several `CSS variables <css-variable-help_>`_ that can be
44 used to quickly control behavior and display across your documentation.
45
46 These are based on top of the basic `Bootstrap CSS variables <https://getbootstrap.com/docs/4.0/getting-started/theming/#css-variables>`_
47 extended with some theme-specific variables.
48
49 Base variables
50 --------------
51
52 Follow these steps to update the base variables:
53
54 1. :ref:`Add a custom CSS stylesheet <custom-css>`. This is where we'll configure the variables.
55 2. Underneath a ``html`` section, add the variables you wish to update. For example, to change the base font size you would add the following to your ``custom.css`` file :
56
57 .. code-block:: css
58
59 html {
60 --pst-font-size-base: 17px;
61 }
62
63 .. important::
64
65 Note that the theme is defined with `CSS variables <css-variable-help_>`_
66 and **not** `SASS variables <https://sass-lang.com/documentation/variables/>`_.
67
68 Refer to :ref:`the managing themes section in this documentation <manage-themes>` if
69 you desire a different behavior between the light and dark theme.
70
71 For a complete list of the theme variables that you may override, see the
72 `theme variables defaults CSS file <pydata-css-variables_>`_:
73
74 .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_layout.scss
75 :language: scss
76
77 .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_fonts.scss
78 :language: scss
79
80 .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_icons.scss
81 :language: scss
82
83 .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_admonitions.scss
84 :language: scss
85
86 .. literalinclude:: ../../src/pydata_sphinx_theme/assets/styles/variables/_versionmodified.scss
87 :language: scss
88
89 .. _color-variables:
90
91 Color variables
92 ---------------
93
94 This theme specifies color variables for the primary and secondary colors (``--pst-color-primary`` and ``--pst-color-secondary``, respectively).
95 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.
96 There are also several other color variables that control the color for admonitions, links, menu items, etc.
97
98 Each color variable has two values, one corresponding to the "light" and one for the "dark" theme.
99 These are used throughout many of the theme elements to define text color, background color, etc.
100
101 Here is an overview of the colors available in the theme (change theme mode to switch from light to dark versions).
102
103
104 .. raw:: html
105
106 <style>
107 span.pst-primary {background-color: var(--pst-color-primary);}
108 span.pst-secondary {background-color: var(--pst-color-secondary);}
109 span.pst-accent {background-color: var(--pst-color-accent);}
110 span.pst-success {background-color: var(--pst-color-success);}
111 span.pst-info {background-color: var(--pst-color-info);}
112 span.pst-warning {background-color: var(--pst-color-warning);}
113 span.pst-danger {background-color: var(--pst-color-danger);}
114 span.pst-background {background-color: var(--pst-color-background);}
115 span.pst-on-background {background-color: var(--pst-color-on-background);}
116 span.pst-surface {background-color: var(--pst-color-surface);}
117 span.pst-on-surface {background-color: var(--pst-color-on-surface);}
118 span.pst-target {background-color: var(--pst-color-target);}
119 </style>
120
121 <p>
122 <span class="sd-badge pst-badge pst-primary sd-bg-text-primary">primary</span>
123 <span class="sd-badge pst-badge pst-secondary sd-bg-text-secondary">secondary</span>
124 <span class="sd-badge pst-badge pst-accent sd-bg-text-secondary">accent</span>
125 <span class="sd-badge pst-badge pst-success sd-bg-text-success">success</span>
126 <span class="sd-badge pst-badge pst-info sd-bg-text-info">info</span>
127 <span class="sd-badge pst-badge pst-warning sd-bg-text-warning">warning</span>
128 <span class="sd-badge pst-badge pst-danger sd-bg-text-danger">danger</span>
129 <span class="sd-badge pst-badge pst-background">background</span>
130 <span class="sd-badge pst-badge pst-on-background">on-background</span>
131 <span class="sd-badge pst-badge pst-surface">surface</span>
132 <span class="sd-badge pst-badge pst-on-surface sd-bg-text-primary">on-surface</span>
133 <span class="sd-badge pst-badge pst-target">target</span>
134 </p>
135
136
137 **To modify the colors for these variables** for light and dark themes, :ref:`add a custom CSS stylesheet <custom-css>` with a structure like so:
138
139 .. code-block:: css
140
141 html[data-theme="light"] {
142 --pst-color-primary: black;
143 }
144
145 html[data-theme="dark"] {
146 --pst-color-primary: white;
147 }
148
149 This theme uses shadows to convey depth in the light theme mode and opacity in the dark one.
150 It defines 4 color variables that help build overlays in your documentation.
151
152 - :code:`background`: color of the back-most surface of the documentation
153 - :code:`on-background` elements that are set on top of this background (e.g. the header navbar on dark mode).
154 - :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).
155 - :code:`on-surface` elements that are on top of :code:`surface` elements (e.g. sidebar directives).
156
157 The following image should help you understand these overlays:
158
159 .. raw:: html
160
161 <style>
162 /* use https://unminify.com to check the indented version of the overlay component */
163 .overlay-container {margin-top: 10%; left: 20%; --width: 80%; --height: 200px; width: var(--width); height: var(--height); position: relative;}
164 .overlay-container .pst-overlay {position: absolute; border: 2px solid var(--pst-color-border);}
165 .overlay-container .pst-background {background-color: var(--pst-color-background); width: var(--width); transform: skew(-45deg); height: var(--height);}
166 .overlay-container .pst-on-background {background-color: var(--pst-color-on-background); height: var(--height); width: calc(var(--width) / 3); transform: skew(-45deg) translate(-2rem, -2rem);}
167 .overlay-container .pst-surface {background-color: var(--pst-color-surface); height: var(--height); width: calc(var(--width) / 3); transform: skew(-45deg) translate(-2rem, -2rem); left: calc(var(--width) / 3 * 2);}
168 .overlay-container .pst-on-surface {background-color: var(--pst-color-on-surface); width: calc(var(--width) / 3); height: calc(var(--height) * 0.66); transform: skew(-45deg) translate(-2rem, -4rem); left: calc(var(--width) / 3 * 2);}
169 .overlay-container .label {position: absolute; bottom: 0.5rem; left: 50%; transform: skew(45deg) translateX(-50%); white-space: nowrap;}
170 </style>
171
172 <div class="overlay-container">
173 <div class="pst-overlay pst-background">
174 <p class="label">background</p>
175 </div>
176 <div class="pst-overlay pst-on-background">
177 <p class="label">on-background</p>
178 </div>
179 <div class="pst-overlay pst-surface">
180 <p class="label">surface</p>
181 </div>
182 <div class="pst-overlay pst-on-surface">
183 <p class="label sd-bg-text-primary">on-surface</p>
184 </div>
185 </div>
186
187 .. it would be nice to have this `.. literalinclude::` here to actually show
188 the file, but there's a pygments bug that fails to lex SCSS variables
189 (specifically the `$` symbol that prepends SCSS variables, see
190 https://github.com/pygments/pygments/issues/2130). So for now it's
191 just a raw download link.
192
193 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>`.
194
195 Configure pygments theme
196 ========================
197
198 As the Sphinx theme supports multiple modes, the code highlighting colors can be modified for each one of them by modifying the ``pygments_light_style`` and ``pygments_dark_style``.
199 You can check available Pygments colors on this `pygments demo page <https://pygments.org/styles/>`__.
200
201 .. code-block:: python
202
203 html_theme_options = {
204 ...
205 "pygments_light_style": "tango",
206 "pygments_dark_style": "monokai"
207 }
208
209 Note that the PyData Sphinx theme uses the `accessible pygments styles <https://github.com/Quansight-Labs/accessible-pygments>`__ for its default syntax highlighting themes.
210 The accessible pygments themes are designed to meet `WCAG AA or AAA standards for color contrast <https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html>`__ and some included themes are also suitable for colorblind users
211 or low-light conditions.
212 You can check all the available styles at the `accessible pygments demo page <https://quansight-labs.github.io/accessible-pygments/>`__.
213
214 .. danger::
215
216 The native Sphinx option ``pygments_style`` will be overwritten by this theme.