Skip to content
ConceptCoreApply spacing, color, and typography deliberately.

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:

Content: the thing itself, such as text or a picture. Padding: space inside the border, around the content. Border: a line drawn around the padding. Margin: space outside the border that holds neighbors away.

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:

Ada is a corgi.
Plain textmargin 0
border 0
padding 0
Feed Ada
A buttonmargin 4px
border 2px
padding 6px
Field trip on Friday!
A noticemargin 8px
border 2px
padding 12px
A framed picturemargin 8px
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:

Lives remaining: 3
color: crimson
Lives remaining: 3
background-color: lavender
Lives remaining: 3
color: white
background-color: slateblue

One 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:

Ada is a corgi.
border: 1px solid black
Ada is a corgi.
border: 4px dashed crimson
Ada is a corgi.
border: 6px double slateblue

The 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:

Ada
Babbage
padding: 0
margin: 0
Ada
Babbage
padding: 10px
margin: 0
Ada
Babbage
padding: 10px
margin: 10px

When 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:

Ada is a corgi.
font-family: Georgia
Ada is a corgi.
font-family: monospace
Ada is a corgi.
font-size: 12px
Ada is a corgi.
font-size: 28px

Sizes 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