Home 〉 HTML Tutorials 〉 HTML Space: Add & Control Space in HTML using Tags & CSS - Complete Guide
HTML Space: Add & Control Space in HTML using Tags & CSS - Complete Guide !
Do you want to learn how to add space between elements in HTML?
This blog post explained different techniques like non-breaking spaces (' '), the '<pre>' tag, and CSS spacing with 'margin', 'padding', and 'white-space'. We also covered styling techniques and HTML elements that help manage layout and spacing. Examples and explanations made everything easier to understand. The complete blog post on HTML space is as follows.
In web development, spacing plays a crucial role in making content readable and visually appealing. HTML offers different ways to add space between text, elements, and components. This guide covers HTML space tags, non-breaking spaces, and CSS spacing properties like margin and padding, with clear examples and tips for SEO.
HTML doesn't have a direct “space” tag, but it offers several methods to manage spacing:
Element | Description | Type |
---|---|---|
'<br>' | Line break | Empty |
'<p>' | Paragraph with automatic margin | Block |
'<div>' | General block container, customizable | Block |
'<span>' | Inline container for text | Inline |
Syntax: ' '
Example:
<!DOCTYPE html>
<html>
<body>
<p>This is a sentence.</p>
</body>
</html>
output 📌
This is a sentence.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
This adds multiple fixed spaces between the words. Each ' ' creates one non-breaking space.
Syntax: '<pre> ... </pre>'
Example:
<!DOCTYPE html>
<html>
<body>
<pre style="font-size:14px; background-color:#f9f9f9; padding:10px;">
This text has spacing exactly as typed.</pre>
output 📌
This text has spacing exactly as typed.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The '<pre>' tag is great for displaying code or structured text.
Creates space 'outside' the element.
Example:
<!DOCTYPE html>
<html>
<body>
<div style="margin: 20px; background-color: #e9ecef; padding:10px;">Box with margin</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Adds 20px space around the box.
Creates space 'inside' the element.
Example:
<!DOCTYPE html>
<html>
<body>
<div style="padding: 20px; background-color: #dee2e6;">Box with padding</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Adds 20px space inside the box before content starts.
Values: 'normal', 'nowrap', 'pre', 'pre-line', 'pre-wrap'
Example:
<!DOCTYPE html>
<html>
<body>
<p style="white-space: pre-wrap;">Line 1
Line 2 with multiple spaces.</p>
</body>
</html>
output 📌
Line 1 Line 2 with multiple spaces.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Preserves spaces and wraps the content normally.
Element | Description | Type |
---|---|---|
'<br>' | Line break | Empty |
'<p>' | Paragraph with automatic margin | Block |
'<div>' | General block container, customizable | Block |
'<span>' | Inline container for text | Inline |
Use these CSS properties:
Example:
<!DOCTYPE html>
<html>
<body>
<div style="margin:20px; padding:15px; background-color:#f1f1f1; border:1px solid #ccc; border-radius:5px;">
Example box with spacing
</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
HTML doesn't have a dedicated "space" tag, but using ' ', '<pre>', and CSS properties like 'margin', 'padding', and 'white-space', you can control spacing precisely. These techniques enhance readability, accessibility, and layout structure on your web pages.
Use multiple ' ' like ' ' for three spaces.
Yes, CSS 'margin' and 'padding' are preferred for layout and design.
Margin is outside the element; padding is inside around content.
Yes, HTML collapses multiple spaces into one. Use ' ' or CSS to preserve space.
Yes, it preserves all whitespace and is useful for code or formatted text.