Left Arrow.Back
Left Arrow.Back

Notes / CSS / Alignment + Touch Target

Alignment + Touch Target

A web page design wireframe.

Alignment + Touch Target

Last Tended

Status: seed

A web page design wireframe highlighting 16px gap between all elements.

1. Requirements

In the context of design, the user of straight lines & right angles are signs that this has been made by humans. Nature doesn't seem to make things this way. They communicate a human being has put effort into building this thing. That it has been designed. Common CSS patterns often don't support alignment in a layout. Different approaches are required.

Imagine we need to code the layout for a web page. It will have 3 root elements:

  • a back link in the top left,
  • a list below the link &
  • a section for content to the right.

Elements should be positioned so:

  • the link & list items' text should align vertically,
  • the link & main's text baselines should align horizontally,
  • 16px space between elements & between elements and page border.
A web page design wireframe.

2. Add Content

Add the contents to the page & remove any excess white-space. To achieve this:

  • set all margins & padding to 0 &
  • use Capsize to generate the CSS required to trim unwanted vertical space above & below text.

The <body/> will be acting as a layout component. I believe a trait of a good layout component is it adapts to its contents. I'm using a CSS grid to position the root elements. The left column's width is set to auto, enabling it to adapt to the link & list's contents. The right is set to 1fr, filling the remaining width of the layout.

localhost:3000

import "./styles.css";
import "./styles.css";

Normally, I would import the CSS file in index.html however I wasn't able to get that working with the bundler (maybe related to this issue).

A web page design wireframe.

3. Add Space

Add space between elements:

  • padding: 16px; to <body/>
  • gap: 16px; to <body/>'s grid.
  • gap: 16px; to <ol/>'s flex container.

Visually, we are now fulfilling all requirements.

localhost:3000

import "./styles.css";
import "./styles.css";
A web page design wireframe with 1 element highlighted in red.

4. Increase Touch-Target

Unfortunatley, there is an accessibility problem with the link. The size of the clickable area is only 38 x 10px. The recommended minimums are:

  • 44 x 44px for AAA rating.
  • 24 x 24px for AA rating. This can be reduced to 20px if:
    • the area is inline (such as a a link within a paragraph) or
    • there is a minimum 4px gap between adjacent clickable areas.

1 approach to fix this is to add padding to the link:

localhost:3000

import "./styles.css";
import "./styles.css";

Although this has fixed the accessibility problem, it has broken the layout's alignment:

  • the link & list items' text no longer align vertically,
  • the link & main's text no longer align horizontally &
  • the space between the left & right columns has increased beyond 16px. I'm calculating this from the right-most letter in the left column to the left-most letter in the right column.
A web page design wireframe with 1 square highlighted.

5. Alignment Fix

We can use a different approach that will solve both the accessibility problem while maintaining the layout's alignment. Instead of adding padding, add an absolutley positioned pseudo-element to the link. This allows setting a touch target to any size without effecting the positioning of the link or its adjacent elements.

index.js

As capsize makes use of pseudo-elements, the link's text is moved into a <span> to prevent styles interfering with each other.

localhost:3000

import "./styles.css";
import "./styles.css";

We now have a layout that adapts to its content, has alignment across all elements & passes accessibility requirements.