:title: My Blog Theme :date: 2026-05-01 :tags: blogging :identifier: 20260501T205946 :signature: 1=4 My Blog Theme ============= .. Pygments does not seem to like more modern CSS syntax like nesting. Rather than have glaring red errors all over the page, let's style them as regular text. .. raw:: html I use a custom Sphinx theme for this site. Generally, I try and implement as much as I can using just HTML+CSS however, there are a few occasions where some JavaScript gets involved. CSS Variables ------------- Below are the CSS Variables used throughout the site's theme. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css :root { --bg-main: white; --fg-main: black; --bg-sidebar: #161b22; --fg-sidebar: white; --fg-sidebar-dim: #aaa; --highlight-color: #f0b100; --border-color: #657b83; --fg-accent-dim: #064e3b; --fg-accent: #059669; --fg-accent-bright: #a7f3d0; --transition: 300ms; --sidebar-min-width: 0.5em; --sidebar-max-width: 300px; --panel-min-height: 0.5em; --panel-max-height: 400px; /* Closed by default */ --left-sidebar-width: var(--sidebar-min-width); --right-sidebar-width: var(--sidebar-min-width); --panel-height: var(--sidebar-min-width); } Which makes implementing a dark theme straightforward .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css @media(prefers-color-scheme: dark) { :root { --bg-main: #0d1117; --fg-main: white; --border-color: #839496; --fg-accent-dim: #064e3b; --fg-accent: #059669; --fg-accent-bright: #a7f3d0; } } As well as respecting the ``prefers-reduced-motion`` preference .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css @media screen and (prefers-reduced-motion: reduce) { :root { --transition: 0.001ms; } } A CSS "Reset" ------------- You might have heard of a `CSS reset `__, while I haven't yet felt the need for a full blown reset stylesheet I do include the following .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css * { margin: 0; box-sizing: border-box; } Utility Classes --------------- ``.guilabel`` ^^^^^^^^^^^^^ Used by the ``:guilabel:`` role. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css .guilabel { color: var(--fg-accent); font-style: italic; font-weight: bold; } ``.hidden`` ^^^^^^^^^^^ Easy way to hide elements. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css .hidden { display: none; } ``.highlighted`` ^^^^^^^^^^^^^^^^ Used by the search system to highlight matched terms. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css .highlighted { background: var(--highlight-color); color: var(--bg-main); padding: 0 0.5em; border: solid 1px var(--fg-main); border-radius: 3px; } ``.line-through`` ^^^^^^^^^^^^^^^^^ .. role:: strike :class: line-through On some pages I define an ad-hoc role to :strike:`strikethrough` some text. .. code-block:: rst .. role:: strike :class: line-through Which requires the CSS for the given class name to be defined. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css .line-through { text-decoration: line-through; } HTML Elements ------------- Trying to work with the CSS cascade rather than against it, it's good to try and define global rules that apply to all HTML elements Adding styles to the ``html`` element applies them to everything on the page. I also *think* that by setting the font size here, I can use ``rem`` units to set all font sizes relative to this base size. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css html { font-size: 13pt; font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; scroll-behavior: smooth; } ``a`` ^^^^^ It's nice if links follow the accent color .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css a { color: var(--fg-accent);} Section Links """"""""""""" Sphinx automatically inserts links for each section header. I style the usual character (``ΒΆ``) so that it's not visible and instead use the link icon from the `feather icon set `__. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css a.headerlink { float: right; color: var(--bg-main); &::before { content: ""; display: inline-block; background: url('data:image/svg+xml;utf8, '); width: 1em; height: 1em; } } Tags """" When linking to tags I want the link to be styled like a "pill". .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css a.tag { color: var(--fg-accent-bright); background: var(--fg-accent-dim); border: solid 1px var(--fg-accent); border-radius: 3px; padding: 0.1em 0.5em; span.count { color: var(--fg-main); } } main a.tag { color: var(--bg-main); background: var(--fg-accent); border-color: var(--fg-accent-dim); } Lists of tags should be flattened .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css ul.taglist { display: flex; gap: 1em; flex-wrap: wrap; li { list-style: none; margin: 0; } } ``blockquote`` ^^^^^^^^^^^^^^ As generated by the ``.. pull-quote::`` directive. .. pull-quote:: The technology you use **impresses no one**. The experience you create with it is **everything**. -- `Sean Gerety`_ .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css blockquote.pull-quote { padding: 0 1em; font-style: italic; border-left: solid var(--fg-accent); } ``details`` ^^^^^^^^^^^ .. details:: Expand... For additional information. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css details > :not(summary) { border-left: solid 1px var(--border-color); padding: 0 0 0 1em; margin: 0 0.2em; } ``dd`` & ``dt`` ^^^^^^^^^^^^^^^ Used by Sphinx/docutils to represent `definition lists `__ ``dt`` Represents the term to be defined. ``dd`` Contains the definition. .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css dt { margin-top: 1em; } dd { margin-left: 1em; padding-left: 1em; border-left: solid 1px var(--fg-accent); p:first-child { margin-top: 0; } } ``figure`` ^^^^^^^^^^ .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css figure { &.align-center img { display: block; margin: auto; } figcaption p { margin: auto; font-size: 0.8rem; text-align: center; } } ``h1-h6`` ^^^^^^^^^ .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css h1, h2, h3, h4, h5, h6 { font-weight: 500; } h1 { font-size: 2rem; } h2 { font-size: 1.8rem; } h3 { font-size: 1.7rem; } h4 { font-size: 1.5rem; } h5 { font-size: 1.4rem; } h6 { font-size: 1.2rem; } ``img`` ^^^^^^^ .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css img { max-width: 100%; } ``kbd`` ^^^^^^^ e.g. :kbd:`C-x C-f` .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css kbd { padding: 0 0.15em; color: var(--fg-accent); } ``table`` ^^^^^^^^^ A ``table`` is composed of many elements. ========= =========== Element Description ========= =========== ``table`` Top-level table element ``thead`` Contains the table's header rows ``tbody`` Contains the table's rows ``tr`` Defines a row ``th`` Defines a header cell ``td`` Defines a normal cell ========= =========== .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css table { width: 100%; border-collapse: collapse; thead { tr { border-bottom: solid 1px var(--border-color); } } td, th { padding: 0 0.5em; border-right: 1px solid var(--border-color); } td:last-child, th:last-child { border-right: none; } } Layout ------ The layout for this site is divided into the following sections .. container:: .. raw:: html Header Left Main Right Footer I want a somewhat dynamic layout for this site, meaning that the sidebars and the footer should be open/closable by the user. Amazingly, it turns out that the ``grid-template-columns`` and ``grid-template-rows`` CSS properties `can be animated `__! So as long as you set the ``transition`` property and use a few CSS variables .. code-block:: css :project: sphinx:dirhtml :filename: _static/css/styles.css body { overflow: hidden; } .grid { color: var(--fg-main); background: var(--bg-main); display: grid; grid-template-columns: var(--left-sidebar-width) 1fr var(--right-sidebar-width); grid-template-rows: 3em calc(100vh - 3em - var(--panel-height)) var(--panel-height); transition: var(--transition); } Then the standard `checkbox trick `__ can be used to open/close the various elements! For example, assuming that when checked the sidebar should close, the CSS for the left sidebar might look something like this. .. code-block:: css #left-sidebar-checkbox { &:checked ~ .grid { --left-sidebar-width: var(--sidebar-min-width); .sidebar { opacity: 0; } } } Which, assumes the following HTML structure. .. code-block:: html
...