Change the appearance
Goal
Your app works, and now you want it to look the way you imagine.
Before you start
You should already know how to build pages. If not, start with Build your first app.
What is CSS?
CSS, short for Cascading Style Sheets, is the language that describes how a web page looks. The same Python built both of these pages:
Your Python decides what is on the page: a header, two lines of text, and a button. CSS decides how each of those looks. Every Drafter page you have made was already styled with CSS that Drafter wrote for you, and now you get to change it.
What CSS is used for
CSS can change almost anything about how a page looks, but four jobs cover most of what you will want:
color, background-colorborderpadding, marginfont-family, font-sizeEach job comes down to one or two CSS properties, named under each sample. Design basics takes them one at a time.
Same page, different CSS
A theme is a complete set of CSS that somebody else wrote. In this
program, the highlighted line is the only one about appearance.
Change "sakura" to "retro", "98", or "terminal" and run it
again:
from drafter import *
set_website_style("sakura")
@route
def index() -> Page:
return Page([
Header("Corgi Fan Club"),
"Members so far: 12\n",
"Ada approves of this club.\n",
Button("Reload", "index")
])
start_server()
Three ways to style a Drafter site
Drafter gives you three ways to change the CSS, listed here from least effort to most control:
| Way | What it changes | Where to learn it |
|---|---|---|
| A theme | The whole site, in one line | Use a theme |
| Styling functions and keywords | One component at a time | Styling functions and keywords |
| Custom CSS | Everything a rule matches | Custom CSS |
Start at the top and move down only when the row above cannot do what you want. A good rule of thumb is a theme for the whole site, then functions and keywords for moments of emphasis. If you find yourself styling every single component by hand, step back and pick a different theme instead.
Common problems
- Nothing changed:
set_website_style(...)must run beforestart_server(...). Put it right after the imports. - A misspelled theme name: Drafter lists the valid names in the error, with a did-you-mean suggestion.
- The theme fights your custom look: themes style everything, so
heavy customization on top of a strong theme gets messy. Use
set_website_style("none")for a blank slate, then custom CSS.
Understand it
Styling changes how things look, never what the app remembers or does. How Drafter works explains that separation.
See another example
The theme catalog shows every theme, and Finish and test an app does a full styling pass on a real project.
Look it up
Site configuration for
set_website_style, and
Styling functions for every
helper.
Fix a problem
If a page will not display after styling changes, see Troubleshooting; styling mistakes usually show as a friendly error naming the bad value.