Before you begin
Measure the source and choose a thickness. To preserve the complete photograph, add space outside it instead of covering edges with an internal stroke.
Text explanation of the diagram
Photo 800 × 600 → Border 20 px → File 840 × 640. An 800 × 600 photo with an external 20 px frame becomes 840 × 640. Drawing the same frame inside instead covers 20 px of the photograph on every edge.
The procedure
- For an exported file, expand the canvas or add a separate layer beneath the photo. Preserve the subject’s proportions rather than stretching it together with the frame.
- For a page, use border and box-sizing: border-box. Check that the image, border and spacing fit the container together.
- Compare on light and dark backgrounds. If the image is a link, preserve keyboard focus; a decorative border should not be the only indication of interaction.
.photo { box-sizing: border-box; border: 1px solid #27241f; max-width: 100%; }A worked example
An 800 × 600 photo with an external 20 px frame becomes 840 × 640. Drawing the same frame inside instead covers 20 px of the photograph on every edge.
Check the result
Dimensions fit the layout, no image edge is accidentally lost, and mobile pages have no horizontal scrolling.
A common mistake
Applying the same border in both the file and CSS doubles its thickness. Inspect the exported file on a neutral background before changing styles.
Sources and next steps
- MDN — box-sizing
- Как сделать рамку вокруг рисунка в Фотошопе — 2009-10-07 (RU)
Diagram and examples: Web Article. The diagram explains a principle; it is not an interface capture or a software test result.
Check a transparent edge against both light and dark
An editor’s checkerboard does not prove the exported edge is clean. Compare the same PNG on two backgrounds at the same scale. These two original teaching files are RGBA 64 × 64: A has a dark partly transparent edge; B deliberately stores white in those edge pixels. This is a synthetic demonstration, not a Photoshop screenshot.
- Keep the layered original separately. Export a PNG with alpha and open the actual output, not only the editor document.
- Place it on light and dark backgrounds. Inspect the outer contour and internal openings at 100% scale, then at the intended page size.
- If a light halo appears on dark, inspect the partly transparent pixels and former matte. Do not hide the problem with another CSS border.
- Check that the checkerboard was not baked into the file and that dimensions and alpha survived. One white page is insufficient.
Checkable pixel: black RGB 0 with alpha 128 over white 255 gives approximately 127; over black 0 it gives 0. White edge RGB 255 with the same alpha gives approximately 128 over black. The simplified teaching calculation is α × foreground + (1 − α) × background, with α = 128/255. Equal alpha does not mean an equal edge.
PNG A · PNG B with a deliberate defect. Record file/dimensions __; alpha __; light background __; dark background __; intended scale __; edge observation __. Do not attribute the same fault to a real photograph before inspecting its file.
Source: developer.mozilla.org.