In HTML, space management is very important for layout and readability. The "<br>" tag creates a line break in text, which brings content to start on a new line. This makes it easy when you want to structure text into separate lines, similar to hitting "Enter" on your keyboard.
On the other hand, " " (non-breaking space) inserts a space that prevents browsers from bringing multiple consecutive spaces into one. It's useful for situations where you need to maintain specific spacing between words or elements without them wrapping to a new line.
Below are the approaches to add space in HTML:
Using <br> Tag
One of the easiest ways to add space in HTML is by using the <br> tag, which stands for "line break." Simply insert this tag where you want to create a new line or add vertical space between elements.
Example: This demonstrates the use of the <br> tag to create a line break within a paragraph, separating text into two distinct lines.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Using br tag</title>
</head>
<body>
<p>This is the first line.<br>This is the second line.</p>
</body>
</html>
Output:
using br tagUsing Margin and Padding
HTML also allows you to adjust spacing around elements using CSS (Cascading Style Sheets). Margin and padding are two CSS properties commonly used for spacing:
- Margin: Adds space outside the border of an element.
- Padding: Adds space inside the border of an element.
Example: This demonstrates a box with a black border, 20px margin, and 10px padding, containing the text "Content with margin and padding".
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>Box with Margin and Padding</title>
</head>
<body>
<div style="margin: 20px; padding: 10px;
border: 1px solid black;">Content with
margin and padding</div>
</body>
</html>
Output:
Adding Whitespace
HTML collapses consecutive whitespace characters (such as spaces, tabs, and line breaks) into a single space when rendered in the browser. However, you can preserve whitespace using the <pre> tag or the CSS white-space property.
Example: This example shows the adding of White space.
HTML
<!DOCTYPE html>
<html>
<head>
<title>using Whitespace</title>
</head>
<body>
<style>
.preformatted {
white-space: pre;
}
</style>
<pre>This text will preserve whitespace.</pre>
</body>
</html>
Output:
adding whitespaceUsing   Entity
This entity represents a non-breaking space in HTML. It creates space without allowing the browser to break the line. You can use it to add horizontal space between words or elements.
Example: This demonstrates the use of the non-breaking space entity (` `) to maintain fixed spaces in the sentence "This is spaced out."
HTML
<!DOCTYPE html>
<html>
<head>
<title>Using nbsp entity</title>
</head>
<body>
<p>This is spaced out.</p>
</body>
</html>
Output:
Using nbspÂ
Similar Reads
HTML nbsp HTML nbsp is an HTML entity for non-breaking space. It prevents two words from being rendered at different lines. This entity is particularly useful for maintaining spacing in cases where normal spaces might collapse. Syntax: Below are the two main important reasons to use HTML nbsp: Table
2 min read
HTML <spacer> tag The HTML <spacer> tag is used to create some white-space. This tag is history in HTML5, there is no more support in HTML5 also this tag is not supported by any major browsers. Syntax:<spacer type="" size="">Attribute:type: This attribute holds the value for the type of space horizontal,
2 min read
What is HTML? HTML (HyperText Markup Language) is the standard markup language used to structure web pages. It is used to create various elements of a webpage/website such as nav-bar, paragraphs, images, video, Forms, and more, which are displayed in a web browser.HTML uses tags to create elements of a webpage.It
3 min read
HTML p Tag HTML <p> tag defines the paragraph and is used to give structure to text content within a webpage. It is a block-level element & used to add the space or margins after and before the element this is done by browsers by default.Note: The HTML <p> tag supports the Global Attributes and
2 min read
HTML Table Padding and Spacing HTML Table Padding & Spacing is used to control the space within and around table cells in HTML. Padding is the space inside a table cell, while spacing (or border-spacing) is the space between the outer borders of adjacent cells.Table Cell PaddingTable Cell Padding refers to the space between t
2 min read
Why is HTML used in Web Pages ? Web pages are built using the HyperText Markup Language or HTML. It is a markup language that defines a webpage's content and structure using tags. These tags provide instructions to web browsers on how to show links, photos, videos, text, and other content. Consider HTML as a house's plan, detailin
9 min read