Paragraphs are the most basic HTML Elements. This topic explains and demonstrates the usage of the paragraph element in HTML.
A <p> tag represent the paragraph in webpage .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- simple example of paragraph-->
<p>This is a simple paragraph </p>
<!-- simple example of multiline paragraph-->
<p>TDB School is a leading web developer's website.
Here you will learn how to code amazing, modern, responsive,
and attractive (static and dynamic) websites by using the latest
web technologies</p>
</body>
</html>
Note: Browser will remove any extra spaces and extra lines when the page is displayed .If you want to add extra space or a line break ,you have use (non breakable space) and br for line break.
Do you want to lean more about HTML Character Entities.
Browser removes extra spaces, you can only add a single space that's why you cannot change the output by adding extra spaces or extra lines in your HTML code. The browser will automatically remove any extra spaces and line breaks when the page is displayed
Here is a simple code example of Extra spaces.
<p>
This example paragraph which
contains a lot of spaces
in the source code,
but the browser
ignores it.
</p>
How to add extra spaces in our paragraph ?
So the question is , can we add some extra spaces in our paragraph . In some cases we want to add some extra spaces , but as we ready above browser remove extra spaces thats why HTML Intruoduce HTML Character Entities so give us for power to represent our document.
In HTML some characters are reserved we can't represent them in our document directly such as : less than (<) or greater than (>) signs in your text, the browser might mix them with tags.
Character entities are used to display reserved characters in HTML
click here learn more about HTML Character entities.
We use (non-breaking space) to add single extra space
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!--Exapmle 1 with spaces-->
<p>
This is a paragraph, extra spaces will be removed by browsers
</p>
<!--The browser will automatically remove any extra spaces-->
<!--USING to add extra spaces-->
<p>This is another paragraph, extra
spaces will be removed by browsers</p>
<!--Exapmle 1 with spaces-->
</body>
</html>
Just like extra spaces browser remove/ignore line break also.
<p>
This example paragraph which
contains
lot of line breaks
in the source
code,
but the browser
ignores it.
</p>
Info: So we use <br> tag for line break ;
Note: You can use both syntax of br ( <br> and </br> )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body><!--Exapmle 2 with line break -->
<!--The browser will automatically remove all line break and text will be display in single line-->
<p>
Browser
ignores line break.
</p> <!--USING <br> to add break lines-->
<p>
so we use <br> for line break
</p>
<!--Exapmle 2 with line break -->
</body>
</html>
Sometimes we want to display our paragraph the same as written in source code, for example, we are writing a poem but we know that Browser will remove any extra spaces and extra lines when the page is displayed.
<pre>
The plates will still shift
and the clouds will still spew.
The sun will slowly rise
and the moon will follow too
</pre>
Info: In this situation we use <pre> tag .
<pre> defines as preformatted text.
The <pre> element is displayed text as it is written in source code with extra spaces and line breaks ,it preserves both spaces and line breaks
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- Example 3 with preformated paragraph-->
<!-- The <pre> element is displayed text as it is written in source code with extra spaces and line breaks -->
<pre>
The plates will still shift
and the clouds will still spew.
The sun will slowly rise
and the moon will follow too
</pre>
</body>
</html>