HTML (1)
HTML (1)
What is HTML?
HTML elements label pieces of content such as "this is a heading", "this is a paragraph", "this is a link", etc.
HTML Documents
All HTML documents must start with a document type declaration: <!DOCTYPE html>. The HTML document itself begins
with <html> and ends with </html>. The visible part of the HTML document is between <body> and </body>.
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
The <!DOCTYPE> declaration represents the document type, and helps browsers to display web pages correctly.
It must only appear once, at the top of the page (before any HTML tags). The <!DOCTYPE> declaration is not case
sensitive. The <!DOCTYPE> declaration for HTML5 is. The <html> element is the root element and it defines the whole
HTML document. It has a start tag <html> and an end tag </html>.
HTML Elements
The HTML element is everything from the start tag to the end tag:
<tagname>Content goes here...</tagname>
Inside the <html> element there is a <body> element. The <body> element defines the document's body. It has a start
tag <body> and an end tag </body>. Then, inside the <body> element there are two other elements: <h1> and <p>
The <h1> element defines a heading. It has a start tag <h1> and an end tag </h1>. The <p> element defines a paragraph.
It has a start tag <p> and an end tag </p>.
HTML Headings
Example :
<!DOCTYPE html>
<html>
<body>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
</body>
</html>
Example:
<!DOCTYPE html>
<html>
<body>
<p>You can change the size of a heading with the style attribute, using the font-
size property.</p>
</body>
</html>
HTML Paragraphs
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:
<!DOCTYPE html>
<html>
<body>
<p>
This paragraph
ignores it.
</p>
<p>
This paragraph
ignores it.
</p>
<p>
</p>
</body>
</html>
The Poem Problem
Example :
<!DOCTYPE html>
<html>
<body>
<p>
</p>
<pre>
</pre>
</body>
</html>
The text inside a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line
breaks:
HTML Styles
The HTML style attribute is used to add styles to an element, such as color, font, size, and more.
Example:
<!DOCTYPE html>
<html>
<body>
<p>I am normal</p>
</body>
</html>
Setting the style of an HTML element, can be done with the style attribute.
<tagname style="property:value;">
Background Color
The CSS background-color property defines the background color for an HTML element.
Example:
<html> <html>
<h1 style="background-color:powderblue;">This is a
heading</h1>
<h1> This is a heading </h1>
<p style="background-color:tomato;">This is a
<p> This is a paragraph. </p>
paragraph.</p>
</body>
</body>
</html>
</html>
Text Color
The CSS color property defines the text color for an HTML element
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Fonts
The CSS font-family property defines the font to be used for an HTML element:
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Text Size
The CSS font-size property defines the text size for an HTML element:
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Text Alignment
The CSS text-align property defines the horizontal text alignment for an HTML element
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Summary
Example:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
HTML Links - Hyperlinks
HTML links are hyperlinks. You can click on a link and jump to another document. When you move the mouse over a link,
the mouse arrow will turn into a little hand. The HTML <a> tag defines a hyperlinks.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>HTML Links</h1>
<p><a href="https://www.w3schools.com/">Visit
W3Schools.com!</a></p>
</body>
</html>
By default, the linked page will be displayed in the current browser window. To change this, you must specify another
target for the link.
_self - Default. Opens the document in the same window/tab as it was clicked
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Both examples above are using an absolute URL (a full web address) in the href attribute.
A local link (a link to a page within the same website) is specified with a relative URL (without the "https://www" part):
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Absolute URLs</h2>
<h2>Relative URLs</h2>
</body>
</html>
HTML Links - Use an Image as a Link
To use an image as a link, just put the <img> tag inside the <a> tag
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Image as a Link</h2>
</body>
</html>
Use mailto: inside the href attribute to create a link that opens the user's email program (to let them send a new email):
Example:
<!DOCTYPE html>
<html>
<body>
<p>To create a link that opens in the user's email program (to let them send a new
email), use mailto: inside the href attribute:</p>
</body>
</html>
Button as a Link
To use an HTML button as a link, you have to add some JavaScript code.
JavaScript allows you to specify what happens at certain events, such as a click of a button:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Button as a Links</h2>
</body>
</html>
Link Titles
The title attribute specifies extra information about an element. The information is most often shown as a tooltip text
when the mouse moves over the element.
Example:
<!DOCTYPE html>
<html lang="en-US">
<body>
<h2>Link Titles</h2>
</body>
</html>
Chapter Summary
Use the target attribute to define where to open the linked document
Use the mailto: scheme inside the href attribute to create a link that opens the user's email program
HTML IMAGES
Images are not technically inserted into a web page; images are linked to web pages. The <img> tag creates a holding
space for the referenced image.
The <img> tag is empty, it contains attributes only, and does not have a closing tag.
Example Code:
<!DOCTYPE html>
<html>
<body>
<h2>HTML Image</h2>
</body>
</html>
The required src attribute specifies the path (URL) to the image.
Note: When a web page loads, it is the browser, at that moment, that gets the image from a web server and inserts it
into the page. Therefore, make sure that the image actually stays in the same spot in relation to the web page,
otherwise your visitors will get a broken link icon. The broken link icon and the alt text are shown if the browser cannot
find the image.
The required alt attribute provides an alternate text for an image, if the user for some reason cannot view it (because of
slow connection, an error in the src attribute, or if the user uses a screen reader). The value of the alt attribute should
describe the image:
You can use the style attribute to specify the width and height of an image. The width and height attributes always
define the width and height of the image in pixels.
The width, height, and style attributes are all valid in HTML. However, we suggest using the style attribute. It prevents
styles sheets from changing the size of images
Example Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
</style>
</head>
<body>
<p>The first image uses the width attribute (set to 128 pixels), but the style in
the head section overrides it, and sets the width to 100%.</p>
<p>The second image uses the style attribute to set the width to 128 pixels,
this will not be overridden by the style in the head section:</p>
</body>
</html>
Common Image Formats
JPEG Joint Photographic Expert Group image .jpg, .jpeg, .jfif, .pjpeg, .pjp
Here are the most common image file types, which are supported in all browsers (Chrome, Edge, Firefox, Safari, Opera):
Chapter Summary
Use the HTML src attribute to define the URL of the image
Use the HTML alt attribute to define an alternate text for an image, if it cannot be displayed
Use the HTML width and height attributes or the CSS width and height properties to define the size of the image