Design basics
In one sentence
Every component on the page is a box, and a handful of CSS properties decide how each box looks.
The idea: every component is a box
Imagine every component on the page as a box, even the ones that do not look like one. A line of text is a box. So is a button, a picture, and a header. Every box is built from the same four layers:
Some layers are zero
Every box has all four layers, but any of them can be zero thick. Here are four boxes with typical numbers:
border 0
padding 0
border 2px
padding 6px
border 2px
padding 12px
border 6px
padding 0
Plain text has no padding, border, or margin at all. A button has a little of each, which is most of why it looks like a button.
See it: one box, layer by layer
This program builds one box, then gives it a fill, padding, a border, and a margin, one highlighted line each. The next page explains these functions. For now, change a number and run it again to watch that layer grow:
from drafter import *
@route
def index() -> Page:
box = Div("Ada is a corgi.")
box = change_background_color(box, "lavender")
box = change_padding(box, "20px")
box = change_border(box, "4px solid slateblue")
box = change_margin(box, "30px")
return Page([
"The line above the box.",
box,
"The line below the box."
])
start_server()
Color: the text and the fill
Two properties do most of the work. color is the foreground, the
text itself. background-color is the fill behind the content and
its padding:
color: crimsonbackground-color: lavendercolor: whitebackground-color: slateblueOne main color and one accent go a long way, and the text always has to stand out clearly from what is behind it. The names come from the color table.
Borders
border draws the line between the padding and the margin. Its value
has three parts: how thick, what kind of line, and what color:
border: 1px solid blackborder: 4px dashed crimsonborder: 6px double slateblueThe kinds of line you will use most are solid, dashed, dotted,
and double.
Spacing: padding and margin
padding is the space inside the border, and margin is the space
outside it. Padding takes the box's fill, while margin is always
see-through, which is how to tell them apart on screen:
padding: 0margin: 0padding: 10pxmargin: 0padding: 10pxmargin: 10pxWhen a page feels wrong and you cannot say why, the answer is usually "not enough space."
Text: typeface and size
font-family picks the typeface, and font-size sets how big the
letters are:
font-family: Georgiafont-family: monospacefont-size: 12pxfont-size: 28pxSizes should mean something: one page title, section headings below it, and body text below that. Typefaces that are safe to use are in the font list.
What this means for your code
- Apply a theme first. Themes already make most of these choices well, so your main job becomes not fighting them.
- Reach for space before decoration: more padding or margin fixes more pages than a new color does.
- Keep a tiny palette: pick two colors from the color table and use only those.
You have now met seven properties: color, background-color,
border, padding, margin, font-family, and font-size. Each
one has a Drafter function and a keyword, and
Styling functions and keywords is where you set them.
Where people get confused
- "Making it pretty means adding things." Usually it means removing: fewer colors, fewer font sizes, more space.
- "Margin and padding are the same." Margin pushes neighbors away; padding pushes your own border away from your content. A background color fills padding but not margin, which is how to tell them apart on screen.
- "It looks fine on my screen." Your screen is only one of many sizes. Narrow the window and watch what happens: capped line widths and flexible layouts (see Row) hold up when the window shrinks.
In more depth
The box and its properties are the tools. Good-looking pages come from a few teachable habits in how you use them (generous space, restrained color, and clear text hierarchy) rather than from talent. Visual design has rules the way code does, and the beginner-sized set fits here.
Space is a tool, not waste
Crowded pages feel broken even when they work. Give elements room with margin and padding, group related things close together, and let unrelated things drift apart.
Color needs restraint
One main color plus one accent goes a long way, while a page with six colors has no clear emphasis at all. Two hard rules come with color. First, text must contrast strongly with its background; squint at the page, and if the text fades for you, it will fade for your users. Second, color can never be the only carrier of meaning, because not everyone perceives color; pair it with words or symbols, such as writing "Error: ..." in red rather than relying on red alone.
Check contrast and colorblind-safety once per project. Gray text on a white background and red-versus-green distinctions are the classic failures, and meaning carried only by color locks some users out entirely.
Typography is hierarchy
Use Header levels in
order for structure, and styling for emphasis. Never use a Header
only to make text bigger. Long lines tire the eyes, so if text spans
the whole window, constrain its width (change_width(..., "40em") is
a reasonable ceiling).
The habits at work
This demo shows the same content twice: first crowded, then spaced and ranked. The only differences are space, one accent color, and heading levels:
from drafter import *
def crowded() -> list:
return [
Header("Bake Sale", 3),
Header("Cookies", 3),
"Chocolate chunk, oatmeal, mystery. All good. Probably.\n",
Header("When", 3),
"Saturday.\n"
]
def designed() -> list:
return [
Header("Bake Sale"),
change_padding(Div(
Header("Cookies", 2),
change_width(
"Chocolate chunk, oatmeal, mystery. All good. Probably.\n",
"30em")
), "12px"),
change_padding(Div(
Header("When", 2),
change_color(bold("Saturday.\n"), "darkslateblue")
), "12px")
]
@route
def index() -> Page:
return Page(
[Header("Crowded", 4), HorizontalRule()] + crowded() +
[HorizontalRule(), Header("Designed", 4), HorizontalRule()] +
designed()
)
start_server()
Compare cause and effect: the designed version ranks its headings (one at level 1, sections at level 2), boxes each section with padding, caps the line length, and spends its single accent color on the one fact that matters most.
Go deeper
- Styling functions and keywords sets these properties from Python, one component at a time.
- Custom CSS sets them with real CSS rules.
- MDN's box model introduction and web.dev's design basics continue where this page stops.
- Improve the design walks through a usability pass for your project.