|
1 |
| -Adding Custom CSS or JavaScript to a Sphinx Project |
2 |
| -=================================================== |
| 1 | +Adding Custom CSS or JavaScript to Sphinx Documentation |
| 2 | +======================================================= |
3 | 3 |
|
4 |
| -The easiest way to add custom stylesheets or scripts, and ensure that the files |
5 |
| -are added in a way that allows for overriding of existing styles or scripts, is |
6 |
| -to add these files using a ``conf.py`` Sphinx extension. Inside your |
7 |
| -``conf.py``, if a function ``setup(app)`` exists, Sphinx will call this function |
8 |
| -as a normal extension during application setup. |
| 4 | +.. meta:: |
| 5 | + :description lang=en: |
| 6 | + How to add additional CSS stylesheets or JavaScript files |
| 7 | + to your Sphinx documentation |
| 8 | + to override your Sphinx theme or add interactivity with JavaScript. |
9 | 9 |
|
10 |
| -For example, if a custom stylesheet exists at ``_static/css/custom.css``, a |
11 |
| -``conf.py`` extension can be written to add this file to the list of |
12 |
| -stylesheets:: |
| 10 | +Adding additional CSS or JavaScript files to your Sphinx documentation |
| 11 | +can let you customize the look and feel of your docs |
| 12 | +or add additional functionality. |
| 13 | +For example, with a small snippet of CSS, |
| 14 | +your documentation could use a custom font or have a different background color. |
13 | 15 |
|
14 |
| - def setup(app): |
15 |
| - app.add_stylesheet('css/custom.css') |
| 16 | +If your custom stylesheet is ``_static/css/custom.css``, |
| 17 | +you can add that CSS file to the documentation using the |
| 18 | +Sphinx option `html_css_files`_:: |
16 | 19 |
|
17 |
| -Using an extension to add the stylesheet allows for the file to be added to the |
18 |
| -list of stylesheets later in the Sphinx setup process, making overriding parts |
19 |
| -of the Read the Docs theme possible. |
| 20 | + ## conf.py |
20 | 21 |
|
21 |
| -The same method can be used to add additional scripts that might override |
22 |
| -previously initialized scripts:: |
| 22 | + # These folders are copied to the documentation's HTML output |
| 23 | + html_static_path = ['_static'] |
23 | 24 |
|
24 |
| - def setup(app): |
25 |
| - app.add_javascript('js/custom.js') |
| 25 | + # These paths are either relative to html_static_path |
| 26 | + # or fully qualified paths (eg. https://...) |
| 27 | + html_css_files = [ |
| 28 | + 'css/custom.css', |
| 29 | + ] |
| 30 | + |
| 31 | + |
| 32 | +A similar approach can be used to add JavaScript files:: |
| 33 | + |
| 34 | + html_js_files = [ |
| 35 | + 'js/custom.js', |
| 36 | + ] |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +.. _html_css_files: https://www.sphinx-doc.org/page/usage/configuration.html#confval-html_css_files |
| 41 | + |
| 42 | +.. note:: |
| 43 | + |
| 44 | + The Sphinx HTML options ``html_css_files`` and ``html_js_files`` |
| 45 | + where added in Sphinx 1.8. |
| 46 | + Unless you have a good reason to use an older version, |
| 47 | + you are strongly encouraged to upgrade. |
| 48 | + Sphinx is almost entirely backwards compatible. |
| 49 | + |
| 50 | + |
| 51 | +Overriding or replacing a theme's stylesheet |
| 52 | +-------------------------------------------- |
| 53 | + |
| 54 | +The above approach is preferred for adding additional stylesheets or JavaScript, |
| 55 | +but it is also possible to completely replace a Sphinx theme's stylesheet |
| 56 | +with your own stylesheet. |
| 57 | + |
| 58 | +If your replacement stylesheet exists at ``_static/css/yourtheme.css``, |
| 59 | +you can replace your theme's CSS file by setting ``html_style`` in your ``conf.py``:: |
| 60 | + |
| 61 | + ## conf.py |
| 62 | + |
| 63 | + html_style = 'css/yourtheme.css' |
| 64 | + |
| 65 | +If you only need to override a few styles on the theme, |
| 66 | +you can include the theme's normal CSS using the CSS |
| 67 | +`@import rule <https://developer.mozilla.org/en-US/docs/Web/CSS/@import>`_ . |
| 68 | + |
| 69 | +.. code-block:: css |
| 70 | +
|
| 71 | + /** css/yourtheme.css **/ |
| 72 | +
|
| 73 | + /* This line is theme specific - it includes the base theme CSS */ |
| 74 | + @import '../alabaster.css'; /* for Alabaster */ |
| 75 | + /*@import 'theme.css'; /* for the Read the Docs theme */ |
| 76 | +
|
| 77 | + body { |
| 78 | + /* ... */ |
| 79 | + } |
0 commit comments