are the languages that run the web.
Note: JavaScript can be used outside the browser/HTML environment like in Node.js.
For example, the URL, http://google.com, how it goes from client/browser to server and back to client.
Think of HTML as the abstract text and images behind a web page, CSS as the page that actually gets displayed, and JavaScript as the behaviors that can manipulate both HTML and CSS.
Ex,
paragraph
with this <p></p>
HTML.HTML defines the content of every web page on the Internet. By “marking up” your raw content with HTML tags, you’re able to tell web browsers how you want different parts of your content to be displayed.
<!DOCTYPE html>
<html>
<head>
<!-- Metadata goes here -->
</head>
<body>
<!-- Content goes here -->
</body>
</html>
<!DOCTYPE html>
tells browsers that this is an HTML5 web page.<html>
tags.<html>
text is called an opening tag and </html>
is called a closing tag.Everything inside of these tags are considered part of the <html>
element, which is what gets created when a web browser parses the HTML tags.
<html>
element, we have two more elements called <head
> and <body>
.head
contains all of its metadata such as page title, CSS, JavaScript, fonts etc which are required to render the page but we don’t necessarily want the user to see them.The <body>
element, which represents the visible content of the page. In our case, its empty since it has an empty <body>
.
<!--
and ends with -->
will be completely ignored by the browser and these are called comments.Create a page, index.html
and lets putting the html elements into it.
<title>
This is the title of webpage denoted ny <title>
element. The browser displays this in the tab for our page, and search engines such as google, duckduckgo displays it in search results.
Result in the browser,
<p>
The <p>
element marks all the text inside it as a distinct paragraph.
<!DOCTYPE html>
<html>
<head>
<title>Welcome to HTML</title>
</head>
<body>
<p>Hello HTML, you are awesome.</p>
</body>
</html>
Result,
<h>
<h1>
<h2>
<h3>
<h4>
<h5>
and <h6>
<body>
<h1>Welcome homie</h1>
<p>Hello HTML, you are awesome.</p>
</body>
What of second and other level of headings? Lets see them,
<body>
<h1>Welcome homie</h1>
<h2>Welcome homie</h2>
<h3>Welcome homie</h3>
<h4>Welcome homie</h4>
<h5>Welcome homie</h5>
<h6>Welcome homie</h6>
<p>Hello HTML, you are awesome.</p>
</body>
This should result in a web page that looks something like this:
<ul>
Wrapping content in <ul>
tags tells a browser that whatever is inside should be rendered as an unordered list. To denote individual items in that list, we wrap them in <li>
tags, such as,
<ul>
<li>Facebook</li>
<li>Instagram</li>
<li>WhatsApp</li>
</ul>
In browser,
Note: <ul>
element should only be containing <li>
as a direct child.
❌ BAD
<ul>
<p>hello</p>
</ul>
✅ GOOD
<ul>
<li><p>hello</p></li>
</ul>
<ol>
With an unordered list, sequence of list items does matter, but when we need the sequence to our list item, thats when reach out to ordered list <ol>
To create an ordered list, simply change the parent <ul>
element to <ol>
.
<ol>
<li>Facebook</li>
<li>Instagram</li>
<li>WhatsApp</li>
</ol>
Would result in,
Notice that the browser automatically incremented the count for each <li>
element.
Block-level Elements or flow Content: are HTML elements which are always drawn on a new line. for an instance,
<p>
is a block-level element.Inline elements or Phrasing Content: are HTML elements which can affect sections of text anywhere within a line. For example,
<em>
is an inline element that affects a span of text inside of aparagraph
. It stands foremphasis
, and it’s typically displayed as italicized text.
Ex,
<p>Block Level Elements</p>
<p>
<em>Sometimes</em>, you need to draw attention to a particular word or phrase.
</p>
<p>
is rendered in a new line.<em>
only part of a line has been affected, which is characteristic of inline elements.<em>
It stands for emphasis
, and it’s typically displayed as italicized text.
<p>The past can't hurt you anymore, not unless <em>you let it</em>.</p>
The part wrapped in <em>
tags should render as italics, as shown above.
<strong>
If we want to be more emphatic than an <em>
tag, we can use <strong>
. It’s an inline element just like <em>
, and looks like this:
<p>The past can't hurt you anymore, not unless <strong>you let it</strong>.</p>
It should be rendered in bold text.
Let’s combine <em>
and <strong>
and see what happens,i.e. nest an <em>
element in a <strong>
or vice versa.
<p>
The past can't hurt you anymore, not unless
<strong><em>you let it.</em></strong>
</p>
So, we get a bold text in italics.
The HTML tags we’ve seen so far either wrap text content i.e. <p>
, <h1>
or <em>
or other HTML elements such as <ol>
.But, some of them can be empty or self-closing.
are the most common empty elements.
<br />
HTML condenses consecutive spaces, tabs, or newlines (together known as whitespace) into a single space.
Ex,
<h2>React</h2>
<p>It's a library to build user interfaces</p>
<p>Post by,
Avinash</p>
The newline after Post by,
in the above snippet will be transformed into a space instead of displaying as a line break as shown in above screen.
Then, how do we put a empty line, To tell the browser that we want a hard line break, we need to use an explicit <br />
element, like this:
Ex,
<h2>React</h2>
<p>It's a library to build user interfaces</p>
<p>Post by,<br />
Avinash</p>
We can use the <br/>
element in scenarios such as in email, signature, movie or music credits etc
<hr />
The <hr />
element is a horizontal rule, which represents a thematic break.
For instance,
<h2>React</h2>
<p>It's a library to build user interfaces</p>
<p>
Post by,<br />
Avinash
</p>
<hr />
<p>
P.S. This page might look like crap, but we'll fix that with some CSS soon.
</p>
Note: Don’t abuse the
<br />
and<hr />
to add lots new line spaces or horizontal line respectively. We can use CSS for presentation and leave the HTML for marking up (content).
❌ BAD
<p>a paragraph needs some space below it...</p>
<br /><br /><br /><br /><br /><br /><br /><br />
<hr />
<p>So, I added some hard line breaks.</p>
The trailing slash (/) in all empty HTML elements is entirely optional.
Let us rewrite above example without trailing slash,
<h2>React</h2>
<p>It's a library to build user interfaces</p>
<p>
Post by,<br>
Avinash
</p>
<hr>
<p>
P.S. This page might look like crap, but we'll fix that with some CSS soon.
</p>
Notice both hr
and br
are not having trailing slash (/) and still the output is same. For sake of consistency and conventions we use the all the tags with closing trailing slash.
Official HTML elements reference can be found here, https://developer.mozilla.org/en-US/docs/Web/HTML/Element
TODO:
List down all the html element at once https://developer.mozilla.org/en-US/docs/Learn/HTML/Cheatsheet
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<h1>Welcome homie</h1>
<h2>Welcome homie</h2>
<p>Hello HTML, you are awesome.</p>
<ul>
<li>Facebook</li>
<li>Instagram</li>
<li>WhatsApp</li>
</ul>
<ol>
<li>Facebook</li>
<li>Instagram</li>
<li>WhatsApp</li>
</ol>
<p>The past can't hurt you anymore, not unless <em>you let it</em>.</p>
<p>
The past can't hurt you anymore, not unless <strong>you let it</strong>.
</p>
<p>
The past can't hurt you anymore, not unless
<strong><em>you let it.</em></strong>
</p>
<h2>React</h2>
<p>It's a library to build user interfaces</p>
<p>
Post by,<br />
Avinash
</p>
<hr />
<p>
P.S. This page might look like crap, but we'll fix that with some CSS
soon.
</p>
</body>
</html>
Till now we have seen few very important HTML elements, but we were only dealing with a single web page. Links and images are fundamentally different from those elements in that they deal with external resources.