Introduction to HTML Basics
Introduction to HTML Basics
• An HTML comment begins with <!--, and ends with -->, as shown in
the example below:
Example: HTML comment
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>Creating Horizontal Lines in HTML</title>
• </head>
• <body>
• <!-- This is an HTML comment -->
• <!-- This is a multi-line HTML comment
• that spans across more than one line -->
• <p>This is a normal piece of text.</p>
• <p><strong>Note:</strong> Comments are not displayed by the browsers.</p>
• </body>
• </html>
HTML Attributes
• All HTML elements can have attributes
• Attributes provide additional information about elements
• Attributes are always specified in the start tag
• Attributes usually come in name/value pairs like: name="value"
• Example1
• <a href="[Link] Example</a>
• Example2
• <img src="img_girl.jpg">
Example: html attribute
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>Using HTML Attributes</title>
• </head>
• <body>
• <p><img src="/examples/images/[Link]" width="30" height="30" alt="Smiley"></p>
• <p><a href="[Link] title="Search Engine" target="_blank">Google</a></p>
• <p><abbr title="Hyper Text Markup Language">HTML</abbr></p>
• <p><input type="text" value="John Doe"></p>
• </body>
• </html>
HTML5 attribute
• There are several attributes in HTML5 that do not consist of
name/value pairs but consists of just name. Such attributes are called
Boolean attributes. Examples of some commonly used Boolean
attributes are checked, disabled, readonly, required, etc.
Example: html5 attribute
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>Using HTML Boolean Attributes</title>
• </head>
• <body>
• <p><input type="email" required></p>
• <p><input type="submit" value="Submit" disabled></p>
• <p><input type="checkbox" checked></p>
• <p><input type="text" value="Read only text" readonly></p>
• </body>
• </html>
General Purpose Attributes
• There are some attributes, such as id, title, class, style, etc. that you
can use on the majority of HTML elements. The following section
describes their usages.
• The id Attribute
• The id attribute is used to give a unique name or identifier to an
element within a document. This makes it easier to select the
element using CSS or JavaScript.
Example: id attribute
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML id Attribute</title>
• <style>
• #firstName{
• }
• #container{
• background: #ccc;
• }
• #infoText{
• color: blue;
• }
• </style>
• </head>
• <body>
• </body>
• </html>
Html class attribute
• The class Attribute
• Like id attribute, the class attribute is also used to identify elements.
But unlike id, the class attribute does not have to be unique in the
document. This means you can apply the same class to multiple
elements in a document, as shown in the following example:
Example: class attribute
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <style>
• .box{
• padding: 20px;
• }
• .highlight{
• background: yellow;
• }
• </style>
• </head>
• <body>
• </body>
• </html>
The title Attribute
• The title attribute to is used to provide advisory text about an
element or its content. Try out the following example to understand
how this actually works.
Example: title attribute
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML title Attribute</title>
• </head>
• <body>
• <p><abbr title="World Wide Web Consortium">W3C</abbr><p>
• <a href="/examples/images/[Link]" title="Click to view a larger image" target="_blank">
• <img src="/examples/images/[Link]" alt="kites">
• </a>
• <p><strong>Note:</strong> Place mouse pointer over the text and image to see how it works.</p>
• </body>
• </html>
The style Attribute
• The style attribute allows you to specify CSS styling rules such as
color, font, border, etc. directly within the element. Let's check out an
example to see how it works:
Example: style attribute
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML style Attribute</title>
• </head>
• <body>
• <p style="color: blue;">This is a paragraph.</p>
• <p><img src="/examples/images/[Link]" style="width: 300px;" alt="Cloudy Sky"></p>
• <div style="border: 1px solid red;">Some content</div>
• </body>
• </html>
HTML headings
• Headings help in defining the hierarchy and the structure of the web page
content.
• HTML offers six levels of heading tags, <h1> through <h6>; the lower the heading
level number, the greater its importance — therefore <h1> tag defines the most
important heading, whereas the <h6> tag defines the least important heading in
the document.
• By default, browsers display headings in larger and bolder font than normal text.
Also, <h1> headings are displayed in largest font, whereas <h6> headings are
displayed in smallest font.
HTML Headings
• <!DOCTYPE html>
• <html>
• <body>
• </body>
• </html>
HTML Paragraphs
• Paragraph element is used to publish text on the web pages.
• Paragraphs are defined with the <p> tag. Paragraph tag is a very basic
and typically the first tag you will need to publish your text on the
web pages. Here's an example:
HTML Paragraphs
• <!DOCTYPE html>
• <html>
• <body>
• <p>This is a paragraph.</p>
• <p>This is another paragraph.</p>
• </body>
• </html>
Managing White Spaces
• Normally the browser will display the multiple spaces created inside
the HTML code by pressing the space-bar key or tab key on the
keyboard as a single space. Multiple line breaks created inside the
HTML code through pressing the enter key is also displayed as a single
space.
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>Preserving White Space in HTML</title>
• </head>
• <body>
• <p>This paragraph has multiple spaces.</p>
• <p>This paragraph has multiple<br><br>line<br><br><br>breaks.</p>
• </body>
• </html>
HTML Text Formatting
• HTML provides several tags that you can use to make some text on
your web pages to appear differently than normal text, for example,
you can use the tag <b> to make the text bold, tag <i> to make the
text italic, tag <mark> to highlight the text, tag <code> to display a
fragment of computer code, tags <ins> and <del> for marking
editorial insertions and deletions, and more.
• The following example demonstrates the most commonly used
formatting tags in action. Now, let's try this out to understand how
these tags basically work:
Example: html text formatting
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>Formatting Text in HTML</title>
• </head>
• <body>
• <p>This is <b>bold text</b>.</p>
• <p>This is <strong>strongly important text</strong>.</p>
• <p>This is <i>italic text</i>.</p>
• <p>This is <em>emphasized text</em>.</p>
• <p>This is <mark>highlighted text</mark>.</p>
• <p>This is <code>computer code</code>.</p>
• <p>This is <small>smaller text</small>.</p>
• <p>This is <sub>subscript</sub> and <sup>superscript</sup> text.</p>
• <p>This is <del>deleted text</del>.</p>
• <p>This is <ins>inserted text</ins>.</p><p>This is <b>bold text</b>.</p>
• </body>
• </html>
HTML Links
• A link or hyperlink is a connection from one web resource to another.
Links allow users to move seamlessly from one page to another, on
any server anywhere in the world.
• A link has two ends, called anchors. The link starts at the source
anchor and points to the destination anchor, which may be any web
resource, for example, an image, an audio or video clip, a PDF file, an
HTML document or an element within the document itself, and so on.
HTML links
Example: HTML Links
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>Creating Links in HTML</title>
• </head>
• <body>
• <p><a href="[Link] target="_blank">Google Search</a></p>
• <p>
• <a href="/examples/images/[Link]">
• <img src="/examples/images/[Link]" alt="kites">
• </a>
• </p>
• <p><a href="[Link]
• </body>
• </html>
Creating Bookmark Anchors
• You can also create bookmark anchors to allow users to jump to a
specific section of a web page. Bookmarks are especially helpful if you
have a very long web page.
• <html lang="en">
• <head>
• <style>
• h2 + p{
• }
• </style>
• </head>
• <body>
• <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu sem tempor, varius quam at, luctus dui. Mauris magna metus, dapibus nec turpis vel, semper malesuada ante. Vestibulum id metus ac nisl bibendum scelerisque non non purus. Suspendisse varius nibh non aliquet sagittis. In tincidunt orci sit amet elementum vestibulum. Vivamus fermentum in arcu in aliquam. Quisque aliquam porta odio in fringilla. Vivamus nisl leo, blandit at bibendum eu, tristique eget risus. Integer aliquet quam ut elit suscipit, id interdum neque porttitor. Integer faucibus ligula. Quis quam ut magna consequat faucibus. Pellentesque eget nisi a mi suscipit
tincidunt. Ut tempus dictum risus. Pellentesque viverra sagittis quam at mattis. Suspendisse potenti. Aliquam sit amet gravida nibh, facilisis gravida odio. Phasellus auctor velit at lacus blandit, commodo iaculis justo viverra. Etiam vitae est arcu. Mauris vel congue dolor. Aliquam eget mi mi. Fusce quam tortor, commodo ac dui quis, bibendum viverra erat. Maecenas mattis lectus enim, quis tincidunt dui molestie euismod. Curabitur et diam tristique, accumsan nunc eu, hendrerit tellus.</p>
• <p>Pulvinar leo id risus pellentesque vestibulum. Sed diam libero, sodales eget sapien vel, porttitor bibendum enim. Donec sed nibh vitae lorem porttitor blandit in nec ante. Pellentesque vitae metus ipsum. Phasellus sed nunc ac sem malesuada condimentum. Etiam in aliquam lectus. Nam vel sapien diam. Donec pharetra id arcu eget blandit. Proin imperdiet mattis augue in porttitor. Quisque tempus enim id lobortis feugiat. Suspendisse tincidunt risus quis dolor fringilla blandit. Ut sed sapien at purus lacinia porttitor. Nullam iaculis, felis a pretium ornare, dolor nisl semper tortor, vel sagittis lacus est consequat eros. Sed id pretium nisl. Curabitur
dolor nisl, laoreet vitae aliquam id. Fusce enim arcu, interdum vel metus dignissim, venenatis feugiat purus. Nulla posuere orci ut leo sodales, sed cursus dolor ornare. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam sit amet quam orci. Nulla sollicitudin lectus eget posuere venenatis. Sed vestibulum elementum sagittis. Quisque tristique tortor quis feugiat sollicitudin. Ut pellentesque luctus vulputate. Ut at odio ac erat blandit vehicula ut eget urna. In hac habitasse platea dictumst. Nullam ut iaculis nibh, eget eleifend elit.</p>
• <p>Nullam hendrerit justo non leo aliquet imperdiet. Etiam in sagittis lectus. Suspendisse ultrices placerat accumsan. Mauris quis dapibus orci. In dapibus velit blandit pharetra tincidunt. Quisque non sapien nec lacus condimentum facilisis ut iaculis enim. Sed viverra interdum bibendum. Donec ac sollicitudin dolor. Sed fringilla vitae lacus at rutrum. Phasellus congue vestibulum ligula sed [Link] arcu, interdum vel metus dignissim, venenatis feugiat purus. Nulla posuere orci ut leo sodales, sed cursus dolor ornare. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam sit amet quam orci. Nulla sollicitudin
lectus eget posuere venenatis. Sed vestibulum elementum sagittis. Quisque tristique tortor quis feugiat sollicitudin. Ut pellentesque luctus vulputate. Ut at odio ac erat blandit vehicula ut eget urna. In hac habitasse platea dictumst. Nullam ut iaculis nibh, eget eleifend elit. Curabitur ligula justo, dapibus non ligula tristique, dapibus tristique nulla. Aliquam pulvinar dapibus eros, rutrum pretium urna iaculis ut. Nam est est, tempus id egestas et, viverra in dui. Aliquam gravida orci tortor, sed congue justo ornare vel. Cras in quam consectetur eros varius scelerisque. Ut vel fermentum purus. Nullam interdum blandit turpis, id pellentesque massa feugiat at. Ut
sed lectus lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla rutrum, ante quis convallis ultricies, magna quam rhoncus erat, in lacinia libero magna a ipsum.</p>
• </body>
• </html>
Creating Download Links
• You can also create the file download link in exactly the same fashion
as placing text links. Just point the destination URL to the file you
want to be available for download.
• In the following example we've created the download links for ZIP,
PDF and JPG files.
• Note: When you click a link that points to a PDF or image file, the file
is not downloaded to your hard drive directly. It will only open the file
in your web browser. Further you can save or download it to your
hard drive on a permanent basis.
Example: download links
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML Download Links</title>
• </head>
• <body>
• <p><a href="/examples/downloads/[Link]">Download Zip file</a></p>
• <p><a href="/examples/downloads/[Link]">Download PDF file</a></p>
• <p><a href="/examples/downloads/[Link]">Download Image file</a></p>
• </body>
• </html>
HTML Images
• Inserting Images into Web Pages
• Images enhance visual appearance of the web pages by making them
more interesting and colorful.
• <html lang="en">
• <head>
• </head>
• <body>
• <table>
• <tr>
• <th>No.</th>
• <th>Name</th>
• <th>Age</th>
• </tr>
• <tr>
• <td>1</td>
• <td>Peter Parker</td>
• <td>16</td>
• </tr>
• <tr>
• <td>2</td>
• <td>Clark Kent</td>
• <td>34</td>
• </tr>
• <tr>
• <td>3</td>
• <td>Harry Potter</td>
• <td>11</td>
• </tr>
• </table>
• </body>
• </html
Table: example2
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <style>
• table, th, td {
• }
• th, td {
• padding: 10px;
• }
• </style>
• </head>
• <body>
• <table>
• <tr>
• <th>No.</th>
• <th>Name</th>
• <th>Age</th>
• </tr>
• <tr>
• <td>1</td>
• <td>Peter Parker</td>
• <td>16</td>
• </tr>
• <tr>
• <td>2</td>
• <td>Clark Kent</td>
• <td>34</td>
• </tr>
• <tr>
• <td>3</td>
• <td>Harry Potter</td>
Table: example3
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <style>
• table {
• border-collapse: collapse;
• }
• table, th, td {
• }
• th, td {
• padding: 10px;
• }
• th {
• text-align: left;
• }
• </style>
• </head>
• <body>
• <table>
• <tr>
• <th>No.</th>
• <th>Name</th>
• <th>Age</th>
• </tr>
• <tr>
• <td>1</td>
• <td>Peter Parker</td>
• <td>16</td>
• </tr>
• <tr>
• <td>2</td>
Spanning Multiple Rows and Columns
• Spanning allow you to extend table rows and columns across multiple
other rows and columns.
• Normally, a table cell cannot pass over into the space below or above
another table cell. But, you can use the rowspan or colspan attributes
to span multiple rows or columns in a table.
• <html lang="en">
• <head>
• <style>
• table {
• width: 300px;
• border-collapse: collapse;
• }
• table, th, td {
• }
• th, td {
• padding: 10px;
• }
• </style>
• </head>
• <body>
• <h2>Spanning Columns</h2>
• <table>
• <tr>
• <th>Name</th>
• <th colspan="2">Phone</th>
• </tr>
• <tr>
• <td>John Carter</td>
• <td>5550192</td>
• <td>5550152</td>
• </tr>
• </table>
• </body>
• </html>
Example: rowspan
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <style>
• table {
• width: 300px;
• border-collapse: collapse;
• }
• table, th, td {
• }
• th, td {
• padding: 10px;
• }
• </style>
• </head>
• <body>
• <h2>Spanning Rows</h2>
• <table>
• <tr>
• <th>Name:</th>
• <td>John Carter</td>
• </tr>
• <tr>
• <th rowspan="2">Phone:</th>
• <td>55577854</td>
• </tr>
• <tr>
• <td>55577855</td>
• </tr>
• </table>
• </body>
• </html>
HTML Lists
• HTML lists are used to present list of information in well formed and
semantic way. There are three different types of list in HTML and each
one has a specific purpose and meaning.
• Unordered list — Used to create a list of related items, in no
particular order.
• Ordered list — Used to create a list of related items, in a specific
order.
• Description list — Used to create a list of terms and their descriptions.
List
• Note: Inside a list item you can put text, images, links, line breaks, etc.
You can also place an entire list inside a list item to create the nested
list.
HTML Unordered Lists
• An unordered list created using the <ul> element, and each list item
starts with the <li> element.
• The list items in unordered lists are marked with bullets. Here's an
example:
Example: HTML Unordered List
• <!DOCTYPE html>
• <html lang="en">
• <head>
• </head>
• <body>
• <ul>
• <li>Chocolate Cake</li>
• <li>Pineapple Cake</li>
• </ul>
• <hr>
• <ul>
• <li>Chocolate Cake
• <ul>
• </ul>
• </li>
• <li>Pineapple Cake</li>
• </ul>
• </body>
• </html>
HTML Ordered Lists
• An ordered list created using the <ol> element, and each list item
starts with the <li> element. Ordered lists are used when the order of
the list's items is important.
• The list items in an ordered list are marked with numbers. Here's an
example:
Example: HTML Ordered List
• <!DOCTYPE html>
• <html lang="en">
• <head>
• </head>
• <body>
• <ol>
• </ol>
• <hr>
• <ol>
• <ol>
• </ol>
• </li>
• </ol>
• </body>
• </html>
HTML Description Lists
• A description list is a list of items with a description or definition of each item.
• The description list is created using <dl> element. The <dl> element is used in
conjunction with the <dt> element which specify a term, and the <dd> element
which specify the term's definition.
• Browsers usually render the definition lists by placing the terms and definitions in
separate lines, where the term's definitions are slightly indented. Here's an
example:
Example: Definition List
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML Description or Definition List</title>
• </head>
• <body>
• <h2>HTML Definition List</h2>
• <dl>
• <dt>Bread</dt>
• <dd>A baked food made of flour.</dd>
• <dt>Coffee</dt>
• <dd>A drink made from roasted coffee beans.</dd>
• </dl>
• </body>
• </html>
HTML Forms
• What is HTML Form
• HTML Forms are required to collect different kinds of user inputs, such as contact
details like name, email address, phone numbers, or details like credit card
information, etc.
• Forms contain special elements called controls like input box, checkboxes, radio-
buttons, submit buttons, etc. Users generally complete a form by modifying its
controls e.g. entering text, selecting items, etc. and submitting this form to a web
server for further processing.
• The <form> tag is used to create an HTML form. Here's a simple example of a
login form:
Input Element
• This is the most commonly used element within HTML forms.
• It allows you to specify various types of user input fields, depending
on the type attribute. An input element can be of type text field,
password field, checkbox, radio button, submit button, reset button,
file select box, as well as several new input types introduced in
HTML5.
• The most frequently used input types are described below.
Text Fields
• Text fields are one line areas that allow the user to input text.
• The <option> elements within the <select> element define each list
item.
Example: select box
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML Select Box</title>
• </head>
• <body>
• <form>
• <label for="city">City:</label>
• <select name="city" id="city">
• <option value="sydney">Sydney</option>
• <option value="melbourne">Melbourne</option>
• <option value="cromwell">Cromwell</option>
• </select>
• </form>
• </body>
• </html>
Submit and Reset Buttons
• A submit button is used to send the form data to a web server. When
submit button is clicked the form data is sent to the file specified in
the form's action attribute to process the submitted data.
• A reset button resets all the forms control to default values. Try out
the following example by typing your name in the text field, and click
on submit button to see it in action.
Example: submit and reset
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML Submit and Reset Buttons</title>
• </head>
• <body>
• <form action="/examples/html/[Link]" method="post">
• <label for="first-name">First Name:</label>
• <input type="text" name="first-name" id="first-name">
• <input type="submit" value="Submit">
• <input type="reset" value="Reset">
• </form>
• </body>
• </html>
HTML iFrame
• What is iframe
• An iframe or inline frame is used to display external objects including
other web pages within a web page. An iframe pretty much acts like a
mini web browser within a web browser. Also, the content inside an
iframe exists entirely independent from the surrounding elements.
• The basic syntax for adding an iframe to a web page can be given
with:
Example: iframe
• <!DOCTYPE html>
• <html lang="en">
• <head>
• <title>HTML iFrame</title>
• </head>
• <body>
• <iframe src="/examples/html/[Link]"></iframe>
• </body>
• </html>
HTML5 Audio
• Using the HTML5 audio Element
• The newly introduced HTML5 <audio> element provides a standard
way to embed audio in web pages. However, the audio element is
relatively new but it works in most of the modern web browsers.