`
MODULE 05: Continuation TO HTML
INSTRUCTOR: EDAN A. BELGICA
Continuation TO HTML
HTML Styles - CSS
CSS stands for Cascading Style Sheets.
Cascading Style Sheets (CSS) is used to format the layout of a webpage.
With CSS, you can control the color, font, the size of text, the spacing between elements, how
elements are positioned and laid out, what background images or background colors are to be used,
different displays for different devices and screen sizes, and much more!
Tip: The word cascading means that a style applied to a parent element will also apply to all children
elements within the parent. So, if you set the color of the body text to "blue", all headings,
paragraphs, and other text elements within the body will also get the same color (unless you specify
something else)!
Using CSS
CSS can be added to HTML documents in 3 ways:
• Inline - by using the style attribute inside HTML elements
• Internal - by using a <style> element in the <head> section
• External - by using a <link> element to link to an external CSS file
Inline CSS
An inline CSS is used to apply a unique style to a single HTML element.
An inline CSS uses the style attribute of an HTML element.
The following example sets the text color of the <h1> element to blue, and the text color of
the <p> element to red:
`
Internal CSS
An internal CSS is used to define a style for a single HTML page.
An internal CSS is defined in the <head> section of an HTML page, within a <style> element.
`
External CSS
An external style sheet is used to define the style for many HTML pages.
To use an external style sheet, add a link to it in the <head> section of each HTML page:
The external style sheet can be written in any text editor. The file must not contain any HTML code,
and must be saved with a .css extension.
Here is what the "styles.css" file looks like:
`
CSS Colors, Fonts and Sizes
Here, we will demonstrate some commonly used CSS properties. You will learn more about them
later.
The CSS color property defines the text color to be used.
The CSS font-family property defines the font to be used.
The CSS font-size property defines the text size to be used.
`
CSS Border
The CSS border property defines a border around an HTML element.
Tip: You can define a border for nearly all HTML elements.
CSS Padding
The CSS padding property defines a padding (space) between the text and the border.
CSS Margin
The CSS margin property defines a margin (space) outside the border.
`
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.
Note: A link does not have to be text. A link can be an image or any other HTML element!
HTML Links - Syntax
The HTML <a> tag defines a hyperlink. It has the following syntax:
The most important attribute of the <a> element is the href attribute, which indicates the link's
destination.
The link text is the part that will be visible to the reader.
Clicking on the link text, will send the reader to the specified URL address.
By default, links will appear as follows in all browsers:
• An unvisited link is underlined and blue
• A visited link is underlined and purple
• An active link is underlined and red
HTML Links - The target Attribute
By default, the linked page will be displayed in the current browser window. To change this, you
must specify another target for the link.
The target attribute specifies where to open the linked document.
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
`
HTML Links - Use an Image as a Link
To use an image as a link, just put the <img> tag inside the <a> tag:
Link to an Email Address
Use mailto: inside the href attribute to create a link that opens the user's email program (to let them
send a new email):
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:
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.
`
HTML Images
Images can improve the design and the appearance of a web page.
HTML Images Syntax
The HTML <img> tag is used to embed an image in a web page.
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.
The <img> tag has two required attributes:
• src - Specifies the path to the image
• alt - Specifies an alternate text for the image
The src Attribute
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 alt Attribute
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).
If a browser cannot find an image, it will display the value of the alt attribute.
Image Size - Width and Height
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.
Note: Always specify the width and height of an image. If width and height are not specified, the
web page might flicker while the image loads.
Width and Height, or Style?
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:
`
Images on Another Server/Website
Some web sites point to an image on another server.
To point to an image on another server, you must specify an absolute (full) URL in the src attribute:
Animated Images
HTML allows animated GIFs:
Image as a Link
To use an image as a link, put the <img> tag inside the <a> tag:
Image Floating
Use the CSS float property to let the image float to the right or to the left of a text:
`
HTML Background Images
A background image can be specified for almost any HTML element.
Background Image on an HTML element
To add a background image on an HTML element, use the HTML style attribute and the
CSS background-image property:
You can also specify the background image in the <style> element, in the <head> section:
Background Image on a Page
If you want the entire page to have a background image, you must specify the background image
on the <body> element:
`
Background Repeat
If the background image is smaller than the element, the image will repeat itself, horizontally and
vertically, until it reaches the end of the element.
To avoid the background image from repeating itself, set the background-repeat property to no-
repeat.
Background Cover
If you want the background image to cover the entire element, you can set the background-
size property to cover.
Also, to make sure the entire element is always covered, set the background-attachment property
to fixed:
This way, the background image will cover the entire element, with no stretching (the image will
keep its original proportions):
`
Background Stretch
If you want the background image to stretch to fit the entire element, you can set the background-
size property to 100% 100%:
Try resizing the browser window, and you will see that the image will stretch, but always cover the
entire element.
References:
https://www.w3schools.com/html/html_basic.asp
https://www.w3schools.com/html/html_colors.asp