Code Block CSS - Syntax Highlighting Referenceο
A comprehensive syntax highlighting theme for Sphinx code blocks that provides enhanced readability and visual distinction for different code elements.
Overviewο
This CSS file (code-block.css) defines custom syntax highlighting colors for code blocks in Sphinx documentation. It uses Pygments token classes to style different elements of code syntax with carefully chosen colors for optimal readability.
Color Schemeο
The theme uses a balanced color palette designed for:
High contrast for better readability
Semantic color coding where similar elements share similar colors
Visual hierarchy with bold fonts for important elements like keywords
Accessibility with colors that work for most color vision types
Installationο
Add the CSS file to your Sphinx project (If not already done):
Click on this link code-block.css to download the custom.css file.
Copy the file
code-block.cssto thesource/_static/cssdirectory.Tip
Right click on the link, copy link address. Use a shell terminal and navigate to
source/_static/cssdirectory. Then usewgetto download. This will download the code-block.css file, without needing to copy from your Download directory.Update your
conf.pyto include the CSS (If not already done):html_css_files = [ 'css/code-block.css', # Code block Syntax highlighting CSS # ... your other CSS files ]
Build your documentation:
make clean && make html
CSS Class Referenceο
π¨ Base Elementsο
Class Name |
Color |
Weight/Style |
Description |
|---|---|---|---|
|
|
normal |
General code block text color |
|
|
normal |
Highlighted line background |
|
|
normal |
Main code block background |
π Keywords & Operatorsο
Class Name |
Color |
Weight/Style |
Description |
|---|---|---|---|
|
|
bold |
General keywords (if, for, while) |
|
|
bold |
Keyword constants (True, False, None) |
|
|
bold |
Keyword declarations (def, class, var) |
|
|
bold |
Keyword namespace (import, from) |
|
|
normal |
Pseudo keywords |
|
|
bold |
Reserved keywords |
|
|
normal |
Keyword types (int, string, bool) |
|
|
normal |
Operators (+, -, *, =) |
|
|
bold |
Operator words (and, or, not, in) |
π’ Numbers & Literalsο
Class Name |
Color |
Weight/Style |
Description |
|---|---|---|---|
|
|
normal |
General numbers |
|
|
normal |
Binary numbers (0b1010) |
|
|
normal |
Float numbers (3.14) |
|
|
normal |
Hexadecimal numbers (0xFF) |
|
|
normal |
Integer numbers (42) |
|
|
normal |
Octal numbers (0o755) |
|
|
normal |
Long integer numbers |
π Stringsο
Class Name |
Color |
Weight/Style |
Description |
|---|---|---|---|
|
|
normal |
General strings |
|
|
normal |
String affixes (fββ, rββ) |
|
|
normal |
Backtick strings |
|
|
normal |
Character strings (βaβ) |
|
|
normal |
String delimiters |
|
|
italic |
Documentation strings (docstrings) |
|
|
normal |
Double-quoted strings (βtextβ) |
|
|
bold |
Escape sequences (\n, \t) |
|
|
normal |
Heredoc strings |
|
|
italic |
String interpolation (${var}) |
|
|
normal |
Other string types |
|
|
normal |
Regular expressions |
|
|
normal |
Single-quoted strings (βtextβ) |
|
|
normal |
String symbols (:symbol) |
π·οΈ Names & Identifiersο
Class Name |
Color |
Weight/Style |
Description |
|---|---|---|---|
|
|
normal |
Attribute names (obj.attr) |
|
|
normal |
Built-in names (print, len, max) |
|
|
bold |
Class names |
|
|
normal |
Constant names (CONSTANT) |
|
|
bold |
Decorators (@property) |
|
|
bold |
Named entities (&, <) |
|
|
normal |
Exception names |
|
|
normal |
Function names |
|
|
bold |
Labels |
|
|
bold |
Namespace names |
|
|
bold |
Tag names (HTML/XML) |
|
|
normal |
Variable names |
π§ Variables (Specific Types)ο
Class Name |
Color |
Weight/Style |
Description |
|---|---|---|---|
|
|
normal |
Built-in pseudo variables |
|
|
normal |
Magic function names (init) |
|
|
normal |
Class variables |
|
|
normal |
Global variables |
|
|
normal |
Instance variables |
|
|
normal |
Magic variables (name) |
π¨ Error & Generic Elementsο
Class Name |
Color |
Weight/Style |
Description |
|---|---|---|---|
|
red border |
normal |
Syntax errors |
|
|
normal |
Generic deleted text (diff -) |
|
normal |
italic |
Generic emphasized text |
|
normal |
bold italic |
Generic emphasized strong text |
|
|
normal |
Generic error text |
|
|
bold |
Generic heading |
|
|
normal |
Generic inserted text (diff +) |
|
|
normal |
Generic output text |
|
|
bold |
Generic prompt (>>> in Python) |
|
normal |
bold |
Generic strong text |
|
|
bold |
Generic subheading |
|
|
normal |
Generic traceback |
|
|
normal |
Whitespace |
Usageο
Code Block Examplesο
The syntax highlighting will automatically apply to code blocks:
```python
def hello_world():
"""This is a docstring."""
print("Hello, World!") # This is a comment
return True
```
```bash
#!/bin/bash
# This is a shell script comment
echo "Setting up environment..."
export PATH=/usr/local/bin:$PATH
```
Customizationο
To customize colors, modify the CSS variables or specific class colors:
/* Example: Change keyword color to purple */
.highlight .k { color: #8A2BE2; font-weight: bold; }
/* Example: Change string color to orange */
.highlight .s { color: #FF8C00; }
This syntax highlighting works with any language supported by Pygments, including:
Python (.py files)
JavaScript (.js files)
Bash/Shell (.sh files)
CSS (.css files)
HTML (.html files)
JSON (.json files)
YAML (.yml files)
SQL (.sql files)
And many moreβ¦
This syntax highlighting theme was designed to complement the enhanced dark mode CSS and provide optimal code readability in Sphinx documentation.
π¬ Commentsο
Class Name
Color
Weight/Style
Description
.highlight .c#408090italic
General comments
.highlight .ch#408090italic
Hashbang comments (#!/bin/bash)
.highlight .cm#408090italic
Multi-line comments (/β¦/)
.highlight .cp#007020normal
Preprocessor comments (#include)
.highlight .cpf#408090italic
Preprocessor file comments
.highlight .c1#408090italic
Single-line comments (//, #)
.highlight .cs#408090italic
Special comments with background