Skip to main content
Documentation
On this page

Install Cirth for Rails

A new Rails app serves stylesheets with Propshaft, which copies each file unchanged under a digest. The application layout supplies the document head for every view, so one stylesheet_link_tag there loads the vendored Cirth file globally, before the app's own stylesheets.

Verified with Rails 8.1.4 on

Four steps: add the file, link it, write views and check it.

1 Add the file #

mkdir -p vendor/assets/stylesheets
curl -o vendor/assets/stylesheets/cirth.min.css \
  https://cdn.jsdelivr.net/npm/@cirthcss/cirth@0.17.0/dist/cirth.min.css

vendor/assets is on Propshaft's load path, and keeping Cirth out of app/assets/stylesheets keeps it out of stylesheet_link_tag :app, which links that folder's files in alphabetical order.

In app/views/layouts/application.html.erb, before the app's stylesheets:

<%= stylesheet_link_tag "cirth.min", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>

bin/rails assets:precompile writes cirth.min-<digest>.css, byte for byte, and the tag links it.

3 Write views #

Form helpers render labels and inputs, which Cirth styles as they are:

<%= form_with model: @subscriber do |form| %>
  <%= form.label :email do %>
    Email <%= form.email_field :email, required: true %>
  <% end %>
  <%= form.submit "Subscribe" %>
<% end %>

With Turbo, navigations keep the head, so the stylesheet loads once. For Stimulus controllers that set Cirth's states, see the Stimulus guide.

4 Check it #

Put this forced-dark region in a page or component that uses the global stylesheet:

<section data-theme="dark">
  <article>
    <p>Dark, whatever the system scheme is.</p>
  </article>
</section>

Run the app, then open the page while the system uses a light theme. A dark article proves that Cirth loaded and that data-theme="dark" is working. It remains dark when the system switches to a dark theme.

Customization covers the --cirth-* properties; set yours in app/assets/stylesheets. Package and builds lists the classless, scoped and print entry points. Return to Framework guides for another stack.

Search documentation

Type at least two characters to search.