Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does indenting or white space matter in html?

Tags:

html

css

Ofcourse indenting is need on our markup , but that really matter the way the browser render.?

I have an simple structure with label , span and input. The middle one without indent got a alignment change , why this is happening ?

enter image description here

<label class="field-row">                
       <span class="label-text" >Email</span>
       <input type="email" />
 </label>
 <label class="field-row">
       <span class="label-text" >Email</span><input type="email" />                
 </label>

Demo issue in firefox and chrome

like image 795
Sarath Avatar asked Mar 31 '14 09:03

Sarath


People also ask

What is indentation and spacing in HTML?

When you are writing HTML (or any other programming language), well-written code follows consistent indentation and spacing patterns. In some languages, indentation and spacing are required for the code to function properly.

Do I need to indent text on a web page?

This document is discussing indenting HTML code and not how to indent text on a web page. No. HTML code does not need to be indented, and all browsers and search engines ignore indentation and extra spacing. However, for any human reader it is a good idea to indent your text because it makes...

Should we indent our code with spaces or tabs?

The first debate is on whether we should use spaces or tabs to indent our code. The second is on whether our indents should equal 2 spaces or 4 spaces. At Epicodus, we indent our code 2 spaces and use tabs instead of spaces for indentation.

How many spaces should be used for indentation?

Spacing and indentation should be consistent throughout your code. Many developers choose to use 4-space or 2-space indentation. In HTML, each nested tag should be indented exactly once inside of its parent tag.


2 Answers

Whitespace does make a difference in page rendering, howevera string of whitespaces longer than one will just be rendered as one whitespace, so this (line break)

<label class="field-row">                
   <span class="label-text" >Email</span>
   <input type="email" />
</label>

and this (no line break, but a whitespace between the span and input)

<label class="field-row">                
   <span class="label-text" >Email</span> <input type="email" />
</label>

will be rendered the same, while this (no whitespace)

<label class="field-row">                
   <span class="label-text" >Email</span><input type="email" />
</label>

will be rendered without that extra space in between the elements.

like image 178
BrotherBallan Avatar answered Oct 19 '22 02:10

BrotherBallan


Whitespace makes a difference in the rendering of inline elements, but everything beyond a single one is always reduced to one whitespace.

This means, unless you use the tag <pre>, five white spaces get rendered just the same as a tab or fifty line breaks.

like image 31
René Roth Avatar answered Oct 19 '22 02:10

René Roth