What Is an HTML Margin?

Mary McMahon
Mary McMahon

A hypertext markup language (HTML) margin is a property that determines the spacing outside elements. It can be used in context with padding, the space inside elements, to organize objects visually. Margins may be necessary for ease of reading as well as aesthetic purposes. When elements are crammed closely together, it can give a website a busy look that may be off-putting to users. Correct spacing with an HTML margin creates a more balanced, even appearance that remains visually interesting, but not overwhelming.

The HTML margin determines the spacing around elements, and it can be used in conjunction with padding related to border elements.
The HTML margin determines the spacing around elements, and it can be used in conjunction with padding related to border elements.

When a developer sets the HTML margin, it can take up to four values. If a single value is given, the browser will assume the margins around the object should be the same on all sides. For two values, it will use the first for the top and bottom, and the second for the left and right. Three values outline the top margin, the measurements at the sides, and the bottom. In cases where all four are defined, the browser will set the top, left, right, and bottom as specified.

Designers can set an HTML margin as a percentage, or in a value like em or pixels.
Designers can set an HTML margin as a percentage, or in a value like em or pixels.

Each HTML margin provides some visual space around the outside of an element. In a classic example, a web designer might have a navigation bar at the top of a page. Without margins, the next element, like a box with content, would butt immediately up against the navigation bar. This would not look very pleasant, and also might make the site difficult for users to use. An HTML margin could provide space around the content, creating clearance between the menu and the content to separate them visually.

Designers can set an HTML margin as a percentage, or in a value like em or pixels. The best option can depend on the designer’s preferences and the overall look and feel of the site. It is important to use consistent measurements in the design. If margins are given in percentages, for example, so should padding, to avoid conflicts.

Another consideration can be changes to screen size or display, which may occur on mobile devices or computers where users magnify the content for readability. If the design is not flexible, content may spill out of containers, run off the page, or fail to magnify properly. This can make the site hard to read, and in some cases may break the content so users cannot access it at all. In testing, designers may explore several different browsers and could experiment with magnification and shrinkage to see what happens to the display.

Mary McMahon
Mary McMahon

Ever since she began contributing to the site several years ago, Mary has embraced the exciting challenge of being a wiseGEEK researcher and writer. Mary has a liberal arts degree from Goddard College and spends her free time reading, cooking, and exploring the great outdoors.

You might also Like

Readers Also Love

Discuss this Article

Post your comments
Login:
Forgot password?
Register: