HTML notes 2
HTML notes 2
An HTML element is defined by a start tag, some content, and an end tag. The HTML
element is everything from the start tag to the end tag:
HTML elements can be nested (this means that elements can contain other elements). All
HTML documents consist of nested HTML elements.
Some HTML elements will display correctly, even if you forget the end tag:
<p>This is a paragraph
<p>This is a paragraph
However, errors may occur if you forget the end tag!
HTML elements with no content are called empty elements. The <br> tag defines a line
break, and is an empty element without a closing tag:
<p>This is a <br> paragraph with a line break.</p>
Output:
This is a
paragraph with a line break.
5
The lang attribute of the <html> tag declares the language of the Web page
The title attribute defines some extra information about an element
There are two ways to specify the URL in the src attribute:
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="BUETlogo.jpg". If the URL begins with a slash, it will be relative to the
domain. Example: src="/images/ BUETlogo.jpg".
Tip: It is almost always best to use relative URLs. They will not break if you change domain.
Relative references work well when you have a bunch of interconnected web pages. If you
create a lot of pages about the same topic and put them in the same directory, you can use
relative references between the pages. If you decide to move your pages to another server,
all the links still work correctly.
If you're referring to a page on somebody else's site, you have to use an absolute reference.
If you're linking to another page on your site, you typically use a relative reference.
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): you can write ‘px’ after the value.
<img src="BUETlogo.jpg" width="500" height="600">
6
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">
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">
Bad:
<a href=https://www.w3schools.com/html/>Visit our HTML tutorial</a>
7
HTML Headings
HTML headings are titles or subtitles that you want to display on a webpage.
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
HTML headings are defined with the <h1> to <h6> tags. <h1> defines the most important
heading. <h6> defines the least important heading. Browsers automatically add some white
space (a margin) before and after a heading.
Bigger Headings
Each HTML heading has a default size. However, you can specify the size for any heading
with the style attribute, using the CSS font-size property:
<h1 style="font-size:60px;">Heading 1</h1>
HTML Paragraphs
A paragraph always starts on a new line, and is usually a block of text. 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.
<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 di erent 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.
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:
The <hr> tag is an empty tag, which means that it has no end tag.
8
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:
<p>This is<br>a paragraph<br>with line breaks.</p>
The <br> tag is an empty tag, which means that it has no end tag.
HTML Styles
The HTML style attribute is used to add styles to an element, such as color, font, size, and
more.
Use the style attribute for styling HTML elements
Use background-color for background color
Use color for text colors
Use font-family for text fonts
Use font-size for text sizes
Use text-align for text alignment
Background Color
The CSS background-color property defines the background color for an HTML element.
<body style="background-color:powderblue;">
Background color for two di erent elements can be set:
9
<h1 style="background-color:powderblue;">This is a heading</h1>
<p style="background-color:tomato;">This is a paragraph.</p>
Text Color
The CSS color property defines the text color for an HTML element:
<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
Fonts
The CSS font-family property defines the font to be used for an HTML element:
<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>
Text Size
The CSS font-size property defines the text size for an HTML element:
<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>
Text Alignment
The CSS text-align property defines the horizontal text alignment for an HTML element:
<h1 style="text-align:center;">Centered Heading</h1>
<p style="text-align:center;">Centered paragraph.</p>
10