HTML Cont.
HTML Cont.
Continuation TO HTML
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
Inline CSS
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 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, we will demonstrate some commonly used CSS properties. You will learn more about them
later.
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
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!
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, 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
To use an image as a link, just put the <img> tag inside the <a> tag:
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.
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 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).
If a browser cannot find an image, it will display the value of the alt attribute.
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.
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
To point to an image on another server, you must specify an absolute (full) URL in the src attribute:
Animated Images
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
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:
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