CSS spacing: margin, gap, and space between elements
Answer: There are many ways to adjust spacing within a UI.
HTML and CSS tools
The margin property can add space, but it may also add spacing you do not want.
The gap property can be more appropriate when you only want space between items.
The br and hr elements allow you to space elements in the block direction.
The nbsp entity becomes a non breaking space that sits in the line.
Margin details
Margin is like adding a cushion around your element.
If you use a percentage, the value is calculated from the width of the containing block.
Instead of adding space between adjacent sibling elements, it will reduce space between them.
An auto margin on a sized block-level box can take up leftover space and center a column.
See also
Claims
- id: C1 claim: There are many ways to adjust spacing within a UI. source: S1 evidence: "many ways to adjust spacing within a UI"
- id: C2 claim: gap can be more appropriate when you only want space between items. source: S1 evidence: "gap might be more appropriate in this case"
- id: C3 claim: Margin is like adding a cushion around your element. source: S2 evidence: "Margin is like adding a cushion around your element"
- id: C4 claim: A percentage margin is calculated from the width of the containing block. source: S2 evidence: "calculated based on the width of your element's containing block"
Sources
- [S1] Learn CSS: Spacing. web.dev. https://web.dev/learn/css/spacing . License: CC-BY-4.0. Accessed 2026-11-24.
- [S2] Spacing chapter (source). GoogleChrome/web.dev. https://raw.githubusercontent.com/GoogleChrome/web.dev/main/src/site/content/en/learn/css/spacing/index.md . License: CC-BY-3.0. Accessed 2026-11-24.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: web.dev, Learn CSS: Spacing; GoogleChrome/web.dev, Spacing chapter (source). CC-BY-4.0 (https://web.dev/learn/css/flexbox). Copyright Google LLC. web.dev text is CC BY 4.0. CC-BY-3.0 (https://raw.githubusercontent.com/GoogleChrome/web.dev/main/LICENSE). Copyright Google LLC. Except as otherwise noted, the content of this site is licensed under the Creative Commons Attribution 3.0 License. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: web.dev, Learn CSS: Spacing; GoogleChrome/web.dev, Spacing chapter (source). CC-BY-4.0 (https://web.dev/learn/css/flexbox). Copyright Google LLC. web.dev text is CC BY 4.0. CC-BY-3.0 (https://raw.githubusercontent.com/GoogleChrome/web.dev/main/LICENSE). Copyright Google LLC. Except as otherwise noted, the content of this site is licensed under the Creative Commons Attribution 3.0 License.
Cite this
iinventedlight. 2026. "CSS spacing: margin, gap, and space between elements." iinventedlight Library, iil:note:xb79nn5r, version of 2026-11-24. https://iinventedlight.com/library/notes/css-spacing/. License: CC-BY-4.0.
@misc{iil_note_xb79nn5r,
author = {{iinventedlight}},
title = {{CSS spacing: margin, gap, and space between elements}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-24},
url = {https://iinventedlight.com/library/notes/css-spacing/},
note = {Permanent ID iil:note:xb79nn5r. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- css-spacing.schema.json (schema; CC-BY-4.0)
- css-spacing.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/css-spacing.md.