How to Insert Spaces/Tabs in Text using HTML and CSS?
Last Updated :
19 Apr, 2025
When building web pages, controlling the spacing between elements and text is essential for creating visually appealing and well-structured content. HTML and CSS offer several ways to insert spaces and tabs in text. While HTML provides basic methods for adding spaces, CSS gives more control over layout, spacing, and alignment.
1. Using the HTML Entities
HTML entities are special codes used to add different types of spaces in your text. They are helpful when you want to control the spacing between words or characters more precisely.
- Non-Breaking Space ( ): A space that's about twice as wide as a normal space and prevents line breaks. It's used to keep words together on the same line.
- En Space ( ): A smaller space, about half the width of the current font, used for small gaps between words.
- Em Space ( ): A larger space, equal to the width of the current font, used for bigger gaps or simulating tabs.
Syntax
<!-- Regular space -->
  <!-- Two spaces gap -->
  <!-- Four spaces gap -->
html
<html>
<head>
<title>
How to insert spaces/tabs in text using HTML/CSS?
</title>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<b>How to insert spaces/tabs in text using HTML/CSS?</b>
<p>This is a regular space.</p>
<p>This is a   two spaces gap.</p>
<p>This is a   four spaces gap.</p>
</body>
</html>
Output

2. Using tab-size Property in CSS
The tab-size property in CSS allows you to set the number of spaces that each tab character will display. This property is particularly useful for formatting preformatted text (using the <pre> tag) or controlling the tab size in code blocks.
Syntax
.tab {
tab-size: value; /* for e.g: value = 2*/
}
html
<html>
<head>
<title>
How to insert spaces/tabs in text using HTML/CSS?
</title>
<style>
.tab1 {
tab-size: 2;
}
.tab2 {
tab-size: 4;
}
.tab4 {
tab-size: 8;
}
</style>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<b>How to insert spaces/tabs in text using HTML/CSS?</b>
<pre class="tab1">This is a tab with 2 spaces.</pre>
<pre class="tab2">This is a tab with 4 spaces.</pre>
<pre class="tab4">This is a tab with 8 spaces.</pre>
</body>
</html>
Output

3. Using Custom CSS
We can create a custom class in CSS to control spacing by using the margin-left property, which adds a specific amount of space. Setting display: inline-block ensures that the space stays next to the text or other elements without causing a line break.
Syntax
.tab {
display: inline-block;
margin-left: value; /* for e.g: value = 40px*/
}
html
<html>
<head>
<title>
How to insert spaces/tabs in text using HTML/CSS?
</title>
<style>
.tab {
display: inline-block;
margin-left: 40px;
}
</style>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<b>How to insert spaces/tabs in text using HTML/CSS?</b>
<p>This is a<span class="tab"></span>tab space in the document.</p>
</body>
</html>
Output
Similar Reads
How to indent text in HTML by using CSS ? Text indentation in an HTML document sets the length of empty white space before lines of text in a block, typically marking the beginning of a paragraph. Proper indentation can enhance the readability and layout of your web content. Here, we will explore several common approaches for text indentati
2 min read
How to wrap a text in a <pre> tag using CSS ? In this article, we will learn how to wrap a text in a <pre> tag. The <pre> tag is used to display the block of preformatted text which preserves the text spaces, line breaks, tabs, and other formatting characters which are ignored by web browsers. By default, the <pre> tag does no
2 min read
5 Easy Ways to Insert Spaces in HTML In HTML, we can add spaces using a special character called the non-breaking space ( ). This ensures the space is visible and doesn't collapse when the page loads. We can use multiple to create larger spaces. However, it's better to use CSS properties like margin and padding to c
3 min read
How to Add a Non-breaking Space using   in HTML? In HTML, the non-breaking space character, represented by , is used to control the spacing and formatting of text content. Unlike regular spaces, which browsers typically collapse into a single space, ensures that multiple spaces are preserved. This feature is essential for main
2 min read
Space between two rows in a table using CSS The space between two rows in a table can be done using CSS border-spacing and border-collapse properties. The border-spacing property is used to set the spaces between cells of a table and border-collapse property is used to specify whether the border of the table is collapsed or not. The border-sp
1 min read