×

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 Images

HTML has <img> tag to display image on the web pages or web application. This tag is an empty tag i.e. we don't have to close this tag while using. SYNTAX: <img src="source address" attributes="alt, height, width"> Example: <html>   <body>   <center>Image tag Example</center>   <img src="https://www.tutorialandexample.com/resources/images/favicon.png"    alt="tutorialandexample"/>   </body>   </html> Output: Image...

1 minute read.

HTML Button Disabled

The HTML <button> element has many attributes, one of them is the disabled attribute. The button on which you apply the disabled attribute will not work, or we can say...

4 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 Forms

Forms are required when we want to collect data from the user. Html forms consists of different fields to take user inputs such as: input, text area, checkbox, password, radio...

4 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 <select> tag

This tag helps in making the drop-down list on the web page. This is highly used in HTML forms for taking the input from user. You can use the id attribute...

3 minutes read.

CSS Clearfix

CSS Clearfix: A clearfix (or clear float) in CSS is for a component to clear or fix the child component, so we don’t need to insert additional markup. The clear...

4 minutes read.

HTML <body> tag

HTML means Hyper Text Markup Language, which is most preferred for making Web pages. It explains how a Web page is put together. There are many different elements in HTML,...

3 minutes read.

What is a Container tag?

In HTML, there are different types of tags or say instructions which make the developer specify various elements in the document and make the browsers understand what to display. Tags in...

5 minutes read.

HTML Code

Before jumping into the topic, let’s understand a common scenario where we have a code snippet in our HTML file. By code snippet, we mean computer code, which is written...

5 minutes read.

How to insert image in HTML

An image (technically digital image) in HTML or even in general technical aspects is a binary representation of visual information. By visual information we mean, in the form of drawings/pictures,...

5 minutes read.

HTML SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute 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 Attributes

HTML Attributes provide the additional information about HTML elements. Attributes are always specified in the start tag. Attributes usually comes in pairs. lang Attribute This attribute helps in declaring the language...

2 minutes read.

HTML 5 Tutorial

What is HTML5? HTML5 is a programming language which stands for the acronym Hyper Text Markup Language 5. It is program that enables the appearance of web pages to be changed, as well as changes...

5 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.

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 Lists

HTML List HTML lists are used to specify data sets. All lists can include one or more elements from the list. HTML lists are available in three different types: Ordered List or...

4 minutes read.

How to change font in HTML

To give style to the content of your website, you may change the font of the text and make your website look attractive to the user. There are several methods which...

4 minutes read.

HTML <table> tag

The Table element If we want to show the data and information in a two-dimensional table with rows and columns, then we can achieve this by using the HTML element <table>. Syntax: <body> <table> <thead> <tr> <thcolspan="#">………….</th> </tr> </thead> <tbody> <tr> <td>……….</td> <td>……….</td> </tr> </tbody> </table> </body> Example: <!DOCTYPE...

9 minutes read.