define mark up for generic sphinx admonitions with a specific title
Asked Answered
A

3

22

I am using Sphinx to generate HTML documentation for a Python program.

I would like to use the generic admonition directive with a specific title and have it marked up in a way I can define, for example like content generated with the note directive, i.e., boxed, but with a different color (most Admonitions are not specially styled).

How do I best go about this?

Airframe answered 6/2, 2012 at 15:52 Comment(2)
How about using the topic directive?Penhall
@crayzeewulf: no, I semantically want it to be admonition; Bud's answer is perfectly fine.Airframe
W
22

If I understood your question correctly, you'd like to apply a custon CSS style to the admonition. You can do this with a :class: attibute.

For example, the following

.. admonition:: my title goes here
   :class: myOwnStyle

   this is the admonition text

renders as

<div class="myownstyle admonition">
  <p class="first admonition-title">my title goes here</p>
  <p class="last">this is the admonition text</p>
</div>

You then add your own style sheet. For example, by a custom layout.html in the _templates directory in your source directory:

{% extends "!layout.html" %}
{% set css_files = css_files + ["_static/customstyle.css"] %}

Then you can play around with CSS styles in your style sheet using a selector for the myownstyle class

Wireman answered 16/3, 2012 at 14:20 Comment(2)
Took me hours to figure this out because I was not phrasing the question correctly to Google. Your answer is thorough and includes more useful information than I thought I could get.Galvez
Good answer. Only thing missing for me is what the css file should look like.Connatural
T
6

To add css files more easily, put them in the _static folder and then add this to conf.py:

def setup(app):
    app.add_stylesheet('custom.css')
Tungstic answered 15/6, 2016 at 21:48 Comment(0)
S
3

Here is an example custom.css for overwriting the color of the title and background color of the myownstyle admonition. I used the app.add_stylesheet() call in conf.py.

.rst-content .myownstyle .admonition-title {
   background: #b99976
}

.rst-content .myownstyle {
   background: #e5d3b3
}
Supernumerary answered 24/4, 2020 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.