×

How to insert spaces/tabs in text using HTML/CSS

What is HTML?

HTML or HyperText Markup Language is used to design web pages. It was released in 1993. The file extension name for HTML file is “.html”. An HTML file is created with the help of CSS and JavaScript. HTML describes the webpage's structure, basically how the page is created or the skeleton body of the webpage. The codes are written in the form of an element. These elements are also called HTML tags. The tags use the markups of HTML. The golden rule of HTML is all the major tags must be opened with <tags> and closed with </tags>. When we have multiple tags, each tag must be closed in the sequences. 

What is CSS?

CSS or Cascading Style Sheet is a language used to design the webpage that HTML creates. It is the core language of web designing. 

As web designers, we must learn how to insert tabs and spaces for a better appearance of a web page. It also improves the readability of our HTML code. For adding multiple spaces, we cannot use the spacebar numerous times. We use various spaces in the HTML code to break the code into different lines. Spaces in the code also design the code in a better way, and it also improves the readability of the code. If there are multiple spaces in our code, the browser deletes extra spaces and allows only one space in the code. But it is not true that we cannot add multiple spaces and tabs in the code. 

There are some approaches for adding some spaces in HTML and CSS.

These approaches are as follows:

Approach-1

By using unique and designated characters, we have to use these tags if we want to add more than one space in the code or the webpage. These tags also represent the non-breaking space. 

There are three special and designated characters for using spaces in the HTML and CSS code.

  • The &nbsp- For non-breaking spaces, this unique character is used in the entire code. It may generate twice the common space. The spaces generated by this unique character cannot be broken by code wrapping. 
  • The &ensp- This unique character is used entirely to denote the current fonts' half-point size, also called en spaces.
  • The &emsp- This unique character is used entirely to denote the current fonts' total size, also called em spaces. It may generate four times the common space.

Syntax

for regular space - &nbsp.
For two space gaps - &ensp.
For four space gaps- &emsp.

HTML code

<!DOCTYPE html>
<html>
<head>
<title>
How to insert spaces/tabs in text using HTML/CSS?
</title>
</head>
<body>
<h1 style="color: green">javaTpoint</h1>
<b>How to insert spaces/tabs in text using HTML or CSS?</b>
<p>This is a &nbsp; for regular space.</p>
<p>This is a &ensp; for two spaces gap.</p>
<p>This is a &emsp; for four spaces gap.</p>
</body>
</html>

Output

How to insert spaces/tabs in text using HTML/CSS

Approach-2

Using TAB size property: This property is used to set tab size space in HTML code. Changing the value of the tab character is also possible in the code. This method only works in <pre> tags.

Syntax

.tab {
        tab-size: 2;
    }

HTML code

<!DOCTYPE html>
<html>
<head>
<title>
How to insert spaces/tabs in text using HTML or CSS?
</title>
<style>
.tab5 {
tab-size: 5;
}


.tab6 {
tab-size: 8;
}


.tab7 {
tab-size: 10;
}
</style>
</head>
<body>
<h1 style="color: green">javaTpoint</h1>
<b>How to insert spaces or tabs in text using HTML or CSS?</b>
<pre class="tab5">This is a tab which show 5 spaces.</pre>
<pre class="tab6">This is a tab which show 8 spaces.</pre>
<pre class="tab7">This is a tab which show 10 spaces.</pre>
</body>
</html>

Output

How to insert spaces/tabs in text using HTML/CSS

Approach-3

By creating a new class for spacing through CSS: By using the margin-left property, a new class can give a certain amount of spaces. We can assign the number of spaces by providing the number of pixels in the property. It also allows the spaces next allow to the line.

Syntax

.tab {
    display: inline-block;
    margin-left: 40px;
}

HTML code

<!DOCTYPE html>
<html>
<head>
<title>
How to insert spaces or tabs in text using HTML or CSS?
</title>
<style>
.tab {
display: inline-block;
margin-left: 40px;
}
</style>
</head>
<body>
<h1 style="color: green">javaTpoint</h1>
<b>How to insert spaces or tabs in text using HTML or CSS?</b>
<p>This is a<span class="tab"></span>tab space in the above document.</p>
</body>
</html>

Output

How to insert spaces/tabs in text using HTML/CSS

Approach-4

By adding a line break in HTML: We can add a line break by using the tag <br> in the HTML code. With the help of this tag, we can create a new line.

HTML code

<!DOCTYPE html>
<html>
<body>
  <p>this is my first line.
  <br>this is second line.
  </p>
</body>
</html>

Output

How to insert spaces/tabs in text using HTML/CSS

Related Topics

Html &lt;datalist >Tag

The autocomplete feature in HTML files is provided by the <datalist> tag. It can be used in conjunction with an input tag to enable users to quickly fill out form...

4 minutes read.

HTML <strong> tag

The strong element in HTML is used to display the real significant information in bold so that the user or reader will understand it easily. Syntax: <p> <strong>………… </strong> </p> Example Here, we will demonstrate how to...

2 minutes read.

HTML <span> tag

The span tag in HTML is used as an inline container element to mark up a specific or special chunk of a text or a section of a page. If you...

2 minutes read.

HTML Ordered List

We use <ol> tag to make an ordered list in html. An ordered list is made to make a list of items in ordered form, ordered list can be either...

3 minutes read.

HTML <section> tag

The section element in HTML is used to define sections in a document. Syntax of Section Tag: <body> <h1>……….</h1> <section> <h2>……….</h2> <p> …………………………</p> </section> <section> <h2>………..</h2> <p>…………………………. <p> </section> </body> Examples of Section Tag Example 1: In this example, we use the HTML "section" element...

3 minutes read.

HTML Youtube

<object> element: It embed the object within the HTML documents and embed plug-ins such as Java applets, PDF readers, Flash Players etc in web pages. Example: <object width="100" height="100" data="bookmark.swf"></object> <embed> element: It defines an embedded...

1 minute read.

Html &lt;center> Tag

The term HTML refers to Hyper Text Markup Language. The most widely used markup language for making web pages is HTML. A structure of web page is described in HTML....

3 minutes read.

Html &lt;dd> Tag

In an HTML description list, an item's definition or description is indicated using the definition description ("dd" tag). A <dd> tag can contain paragraphs, line breaks, images, links, and lists. It...

4 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

HTML Elements

An HTML element is nothing but a starting tag and then some content and then the closing tag. So we can say HTML elements is everything from the starting tag...

3 minutes read.

HTML <sub> tag

The sub element in HTML is used to describe one or more subscript texts. If The sub tag shows the text which is presented in a smaller font and below...

2 minutes read.

HTML Data Tag

HTML Data Tag: The HTML < data > tag is used to transmit its content to a machine-readable version. The data are presented in a particular format. It is useful...

3 minutes read.

Html Wbr Tag

The Line Break Opportunity element A word break opportunity is a place in text from where the browser breaks a line. This opportunity is represented by the element wbr in the...

2 minutes read.

HTML Code Tag

The code tag in HTML is a phrase tag used to insert a piece of computer programming code. In HTML there is a separate tag named as <code> tag. Sometimes,...

3 minutes read.

HTML Id Attribute

The HTML id attribute represents the unique identifier for an element of the HTML document. Each element has its unique identifier. The id attribute is a global attribute that can be...

12 minutes read.

HTML Tutorial

Introduction This HTML tutorial helps beginners and professionals to learn HTML easily. HTML is a widely used web technology for website development. It stands for Hyper Text Markup Language. It is mainly used to...

7 minutes read.

HTML Font Size

The HTML <font> tag uses the size attribute to specify the size of the text. We can set the size attribute’s value from 1 to 7. The default size of...

4 minutes read.

HTML Search Box

The HTML search box is also known as the search bar. It is an input text field that is used to search the content. If the users want to explore...

7 minutes read.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

Caption Tag in HTML

Caption Tag in HTML: The < caption > tag in HTML describes the heading of a table in the HTML text. Browsers typically make the text found above the table...

4 minutes read.