Open In App

How to tab space instead of multiple non-breaking spaces (nbsp)?

Last Updated : 24 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Tab space instead of multiple non-breaking spaces refers to using the tab key for indentation or spacing within text, rather than inserting multiple non-breaking space entities (nbsp) to achieve the same visual effect, which can be cumbersome and less efficient.

We can use the <pre> tag or CSS properties like inline-block, padding, and margin to display text with consistent spacing instead of relying on multiple non-breaking spaces (nbsp) for indentation.

Adding tab space using a pre-tag

The <pre> tag, HTML preserves whitespace, including tabs. This tag is ideal for displaying code or text where spacing is critical, as it maintains the original formatting without collapsing multiple spaces into one.

Syntax:

<pre> Contents... </pre>

Example: The example below shows the <pre> tag preserves the original formatting, spaces, and tabs of the enclosed text, commonly used for displaying code snippets with their indentation and whitespace intact.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
      Using the pre Tag
     </title>
</head>

<body>
    <center>
        <h2 style="color: green;">GeeksforGeeks</h2>
        <pre>
            This is some    text with        multiple    spaces and    tabs.
            Tabs are preserved,           as well as the                 spaces.
        </pre>
    </center>

</body>

</html>

Output:

nbspimg

Output

Adding tab space using inline-block

By setting the display property to inline-block for elements, they align horizontally while allowing for padding and margin adjustments. This method is useful for creating tab-like spacing between elements within a container.

Syntax:

display: inline-block;

Example: In the example, CSS is used for tab spacing with a .tab class defined as display: inline-block and margin-left: 20px to create consistent spacing between text elements, resembling tabs for enhanced readability.

HTML
<!DOCTYPE html>
<html>

<head>
	<title>Using CSS for Tab Spacing</title>
	<style>
		.tab {
			display: inline-block;
			margin-left: 20px;
		}
	</style>
</head>

<body>
	<center>
		<h2 style="color: green;">
          Adding tab space using inline-block
      	</h2>
		<p>
			This is some
          <span class="tab"></span>
          text with
          <span class="tab"></span>
          multiple
          <span class="tab"></span>
          spaces and
          <span class="tab"></span>
          tabs. Tabs are preserved,
          <span class="tab"></span>
          as well as the
          <span class="tab"></span>
          spaces.
		</p>
	</center>
</body>

</html>

Output:

nbspimg1

Output

Adding tab space using padding property

By using padding property we can give any amount of spaces in any direction of an element (i.e, top, right, bottom, left) from the border of the element. Here we only deal with the spaces so we need only two padding property i.e, padding-left and padding-right.

Syntax:

padding-left : value;
or
padding-right : value;

Example: In this example, jQuery modifies the paragraph’s left padding upon button click, dynamically changing its appearance within the red-colored div.

html
<!DOCTYPE html> 
<html> 

<head> 
<script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script> 
    <style> 
        div { 
            width: 300px; 
            height: 100px; 
            background-color: red; 
        } 
        
        P { 
            width: 100px; 
            height: 100px; 
            background-color: green; 
            text-align: center; 
            font-weight: bold; 
        } 
    </style> 
    <script> 
        $(document).ready(function() { 
            $("button").click(function() { 
                $("#demo").css("padding-left", "30px"); 
            }); 
        }); 
    </script> 
</head> 

<body> 

    <div id="demo"> 

        <p> Block 1 </p> 

        <button>Submit</button> 
    </div> 

</body> 

</html> 

Output :

tabSpacingPadding

Using Padding

Adding tab space Using margin property

Using margin property we can give any amount of spaces in any direction of an element (i.e, top, right, bottom, left) from the border of the element. Here we only deal with the spaces so we need only two margin property i.e, margin-left and margin-right.

Syntax:

 margin-left : value;
or
margin-right : value;

Example: In this example, jQuery shifts a green paragraph 60 pixels to the right within a div on button click, using CSS for initial styling and jQuery for dynamic adjustment.

html
<!DOCTYPE html> 
<html> 

<head> 
<script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script> 
    <style> 
        div { 
            width: 300px; 
            height: 100px; 
            background-color: red; 
        } 
        
        P { 
            width: 100px; 
            height: 100px; 
            background-color: green; 
            text-align: center; 
            font-weight: bold; 
        } 
    </style> 
    <script> 
        $(document).ready(function() { 
            $("button").click(function() { 
                $("#demo").css("margin-left", "60px"); 
            }); 
        }); 
    </script> 
</head> 

<body> 

    <div> 

        <p id="demo"> Block 1 </p> 

        <button>Submit</button> 
    </div> 

</body> 

</html> 

Output:

tabspacingBymargin

using margin



Next Article

Similar Reads