0% found this document useful (0 votes)
22 views11 pages

The Href Attribute: Example

Uploaded by

Muhammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

The Href Attribute: Example

Uploaded by

Muhammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

HTML Attributes

HTML attributes provide additional information about HTML elements.

 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"

 The href Attribute


 The <a> tag defines a hyperlink. The href attribute specifies the URL of
the page the link goes to:
 Example
 <a href="https://www.w3schools.com">Visit W3Schools</a>

 You will learn more about links in our HTML Links chapter.

 The src Attribute


 The <img> tag is used to embed an image in an HTML page.
The src attribute specifies the path to the image to be displayed:
 Example
 <img src="img_girl.jpg">

 There are two ways to specify the URL in the src attribute:
 1. Absolute URL - Links to an external image that is hosted on another
website. Example: src="https://www.w3schools.com/images/img_girl.jpg".
 Notes: External images might be under copyright. If you do not get
permission to use it, you may be in violation of copyright laws. In addition,
you cannot control external images; it can suddenly be removed or
changed.
 2. Relative URL - Links to an image that is hosted within the website.
Here, the URL does not include the domain name. If the URL begins
without a slash, it will be relative to the current page. Example:
src="img_girl.jpg". If the URL begins with a slash, it will be relative to the
domain. Example: src="/images/img_girl.jpg".
 Tip: It is almost always best to use relative URLs. They will not break if
you change domain.

 The width and height Attributes
 The <img> tag should also contain the width and height attributes,
which specify the width and height of the image (in pixels):
 Example
 <img src="img_girl.jpg" width="500" height="600">

 The alt Attribute


 The required alt attribute for the <img> tag specifies an alternate text for
an image, if the image for some reason cannot be displayed. This can be
due to a slow connection, or an error in the src attribute, or if the user
uses a screen reader.
 Example
 <img src="img_girl.jpg" alt="Girl with a jacket">

 Example
 See what happens if we try to display an image that does not exist:
 <img src="img_typo.jpg" alt="Girl with a jacket">

 You will learn more about images in our HTML Images chapter.

 ADVERTISEMENT

 The style Attribute


 The style attribute is used to add styles to an element, such as color,
font, size, and more.
 Example
 <p style="color:red;">This is a red paragraph.</p>

 You will learn more about styles in our HTML Styles chapter.

 The lang Attribute


 You should always include the lang attribute inside the <html> tag, to
declare the language of the Web page. This is meant to assist search
engines and browsers.
 The following example specifies English as the language:
 <!DOCTYPE html>
<html lang="en">
<body>
...
</body>
</html>
 Country codes can also be added to the language code in
the lang attribute. So, the first two characters define the language of the
HTML page, and the last two characters define the country.
 The following example specifies English as the language and United
States as the country:
 <!DOCTYPE html>
<html lang="en-US">
<body>
...
</body>
</html>
 You can see all the language codes in our HTML Language Code
Reference.

 The title Attribute


 The title attribute defines some extra information about an element.
 The value of the title attribute will be displayed as a tooltip when you
mouse over the element:
 Example
 <p title="I'm a tooltip">This is a paragraph.</p>

 All HTML elements can have attributes

 The href attribute of <a> specifies the URL of the page the link
goes to

 The src attribute of <img> specifies the path to the image to


be displayed

 The width and height attributes of <img> provide size


information for images

 The alt attribute of <img> provides an alternate text for an


image
 The style attribute is used to add styles to an element, such as
color, font, size, and more

 The lang attribute of the <html> tag declares the language of


the Web page

 The title attribute defines some extra information about an


element

 HTML Table
 HTML tables allow web developers to arrange data into rows and
columns.

 Define an HTML Table


 A table in HTML consists of table cells inside rows and columns.
 Example
 A simple HTML table:
 <table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
</table>
 Try it Yourself »

 Table Cells
 Each table cell is defined by a <td> and a </td> tag.
 td stands for table data.
 Everything between <td> and </td> are the content of the table cell.
 Example
 <table>
<tr>
<td>Emil</td>
<td>Tobias</td>
<td>Linus</td>
</tr>
</table>
 Try it Yourself »
 Note: A table cell can contain all sorts of HTML elements: text, images, lists,
links, other tables, etc.

 ADVERTISEMENT

 Table Rows
 Each table row starts with a <tr> and ends with a </tr> tag.
 tr stands for table row.
 Example
 <table>
<tr>
<td>Emil</td>
<td>Tobias</td>
<td>Linus</td>
</tr>
<tr>
<td>16</td>
<td>14</td>
<td>10</td>
</tr>
</table>
 Try it Yourself »
 You can have as many rows as you like in a table; just make sure that the
number of cells are the same in each row.
 Note: There are times when a row can have less or more cells than another.
You will learn about that in a later chapter.

 Table Headers
 Sometimes you want your cells to be table header cells. In those cases
use the <th> tag instead of the <td> tag:
 th stands for table header.
 Example
 Let the first row be table header cells:
 <table>
<tr>
<th>Person 1</th>
<th>Person 2</th>
<th>Person 3</th>
</tr>
<tr>
<td>Emil</td>
<td>Tobias</td>
<td>Linus</td>
</tr>
<tr>
<td>16</td>
<td>14</td>
<td>10</td>
</tr>
</table>
 Try it Yourself »
 By default, the text in <th> elements are bold and centered, but you can
change that with CSS.

 HTML Table Tags


Tag Description

<table> Defines a table

<th> Defines a header cell in a table

<tr> Defines a row in a table

<td> Defines a cell in a table


<caption> Defines a table caption

<colgroup Specifies a group of one or more columns in a table for formatting


>

<col> Specifies column properties for each column within a <colgroup>


element

<thead> Groups the header content in a table

<tbody> Groups the body content in a table

<tfoot> Groups the footer content in a table

HTML Paragraphs
A paragraph always starts on a new line, and is usually a block of text.

HTML Paragraphs

The HTML <p> element defines a paragraph.

A paragraph always starts on a new line, and browsers automatically add


some white space (a margin) before and after a paragraph.

Example
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

HTML Display

You cannot be sure how HTML will be displayed.

Large or small screens, and resized windows will create different results.

With HTML, you cannot change the display by adding extra spaces or extra
lines in your HTML code.

The browser will automatically remove any extra spaces and lines when the
page is displayed:

Example

<p>
This paragraph
contains a lot of lines
in the source code,
but the browser
ignores it.
</p>

<p>
This paragraph
contains a lot of spaces
in the source code,
but the browser
ignores it.
</p>

HTML Horizontal Rules

The <hr> tag defines a thematic break in an HTML page, and is most often
displayed as a horizontal rule.

The <hr> element is used to separate content (or define a change) in an


HTML page:

Example
<h1>This is heading 1</h1>
<p>This is some text.</p>
<hr>
<h2>This is heading 2</h2>
<p>This is some other text.</p>
<hr>

Try it Yourself »

The <hr> tag is an empty tag, which means that it has no end tag.

HTML Line Breaks

The HTML <br> element defines a line break.

Use <br> if you want a line break (a new line) without starting a new
paragraph:

Example

<p>This is<br>a paragraph<br>with line breaks.</p>

Try it Yourself »

The <br> tag is an empty tag, which means that it has no end tag.

The Poem Problem

This poem will display on a single line:

Example

<p>
My Bonnie lies over the ocean.

My Bonnie lies over the sea.

My Bonnie lies over the ocean.

Oh, bring back my Bonnie to me.


</p>

Try it Yourself »
Solution - The HTML <pre> Element

The HTML <pre> element defines preformatted text.

The text inside a <pre> element is displayed in a fixed-width font (usually


Courier), and it preserves both spaces and line breaks:

Example

<pre>
My Bonnie lies over the ocean.

My Bonnie lies over the sea.

My Bonnie lies over the ocean.

Oh, bring back my Bonnie to me.


</pre>

Try it Yourself »

Exercise?

True or False. HTML paragraphs always starts on a new line.

True

False

Submit Answer »

HTML Tag Reference

W3Schools' tag reference contains additional information about HTML


elements and their attributes.

Tag Description
<p> Defines a paragraph

<hr> Defines a thematic change in the content

<br> Inserts a single line break

<pre> Defines pre-formatted text

HTML Links
The target attribute can have one of the following values:

 _self - Default. Opens the document in the same window/tab as it was


clicked

 _blank - Opens the document in a new window or tab

 _parent - Opens the document in the parent frame

 _top - Opens the document in the full body of the window

You might also like