10. Web Design1
10. Web Design1
Introduction
Our journey begins with a thorough review of the internet and the web,
providing you with a clear understanding of how data is transmitted and
accessed across the vast network of interconnected devices. From the
underlying protocols that govern communication to the role of web servers
and browsers, you will gain insight into the infrastructure that powers the
digital landscape we navigate every day.
Learning Objectives
1. Introduction
1.1. What is the difference between internet and world wide web?
On the other hand, the World Wide Web (WWW) is a subset of the
internet, consisting of interconnected documents and resources accessed
through web browsers. Created by Tim Berners- Lee in 1989, the web
allows users to navigate between web pages via hyperlinks, accessing a
diverse range of content such as websites, web applications, multimedia,
and online services. While the internet provides the infrastructure, the
World Wide Web represents the interconnected ecosystem of information
and resources that has become integral to our digital lives.
1.2. Applications of the Internet
The Internet offers a wide range of services and applications that enable
users to perform various tasks.
World Wide Web (WWW): The web as a collection of
The Web works as per the internet's basic client-server format as shown in
the following image. The servers storeand transfer web pages or
information to user's computers on the network when requested by the
users. A web server is a software program which serves the web pages
requested by web users using a browser. The computer of a user who
requests documents from a server is known as a client. Browser, which is
installed on the user' computer, allows users to view the retrieved
Tinsae D@2024 Internet and web design
documents.
All the websites are stored in web servers. Just as someone lives on rent in
a house, a website occupies a space in a server and remains stored in it.
The server hosts the website whenever a user requests its Webpages, and
the website owner has to pay the hosting price for the same. The moment
you open the browser and type a URL in the address bar or search
something on Google,the WWW starts working. There are three main
technologies involved in transferring information(web pages) from servers
to clients (computers of users). These technologies include Hypertext
Markup Language (HTML), Hypertext Transfer Protocol (HTTP) and
Web browsers.
1.4. Web sites
A website is a collection of web pages and related content that are hosted
on a web server and can be accessed via the World Wide Web. It serves as
a virtual space where individuals, organizations, businesses, and other
entities can share information, showcase products or services,
i. Security
HTTP: HTTP does not provide any built-in security measures. Data
transmitted over HTTP is sent in plain text, making it vulnerable to
interception and eavesdropping. It does not offer data encryption or
protection against tampering.
HTTPS: HTTPS encrypts the data being transmitted between the client
and server. The encryption process ensures that the data is converted into
a secure format that can only be decrypted by the intended recipient (the
server). This protects the confidentiality and integrity of the data.
v. URL Scheme
HTTP: URLs for HTTP resources start with "http://" in the address bar of web
browsers.
HTTPS: URLs for HTTPS resources start with "https://" in the address bar.
The presence of "https://" indicates that the connection is secure, and the
data transmitted between the client and server is encrypted.
Let’s delve deeper and see more details on how DNS works. The first thing
we need to know is that there isn’t a single DNS, but a selection of them,
that can help resolve a single IP address.
A computer has its own cache of DNS data – a local IP-address-to-URL
matching – that it uses for quick references. But, it can’t store all the
information for every website or device out there; hence,the need for root,
recursive, Top Level, Second Level, and authoritative DNS servers.
When a user asks to go to a website, the browser first checks the local DNS
cache for the correct resolution (from URL to IP address) and, if not found,
goes:
Out to the recursive DNS server – which is normally operated
by the local ISP provider
DNS hierarchy
The DNS hierarchy, also called the domain name space, is an inverted tree
structure. The DNS hierarchy tree has a single domain at the top of the
structure called the root domain – indicated bythe “.” as we have mentioned
above. Below the root domain are the top-level domains that dividethe DNS
hierarchy into segments containing second-level domains, sub-domains,
and hosts.
Within the hierarchy, the resolution process starts at the Root Level DNS,
the Top Level DNS, working its way down to the Second Level DNS, then
through any number of sub-domains until we get to the actual hostname we
want to resolve into an IP address. Let’s have a look at each component
individually to see
1.8. Web browser
1. What is the fundamental difference between the Internet and the World Wide Web?
2. How does a web browser work, and what are the key components involved in rendering a
webpage?
3. Describe the role of HTTP and HTTPS protocols in web communication. How do they differ?
4. What is DNS (Domain Name System), and how does it facilitate internet browsing?
5. What are cookies in the context of web browsing, and how are they used by websites?
6. Explain the significance of TCP/IP in internet communication. How does it ensure data
transmission reliability?
7. Describe the difference between client-side scripting and server-side scripting. Provide examples
of each.
8. How do search engines like Google index and rank web pages?
9. What are some common security threats associated with browsing the internet, and what
measures can users take to protect themselves from these threats?
10. Can you explain the concept of IPv6 and its significance in the context of internet addressing?
Lesson 2: HTML
2.1.Introduction to HTML
HyperText Markup Language, commonly referred to as HTML, is the
standard markup languageused to create web pages. Web browsers can
read HTML files and render them into visibleor audible web pages. HTML
describes the structure of a website semantically along with cues for
presentation, making it a markup language, rather than a programming
language. HTML, an acronym for Hyper Text Markup Language, specifies
how the structure of a webpage will be withthe help of various markups. It
is a structured markup language that is used to create Web pages. Markup
languages like HTML bundle together codes which are elements that are used
to representthe structure and format of a document. A user agent, usually a
Web browser which renders (delivers) the document, interprets the meaning
of these codes to decipher (making it into simplerhuman readable text) how
to structure or display a document. The HTML elements are made up of
Tinsae D@2024 Internet and web design
alphanumeric tokens surrounded by angle brackets, for example, <B>,
<HTML>, <IMG> and <HR> .
Almost all elements possess a pair of tags i.e. a start tag and an end tag. The
start tag is a mnemonic symbol for the element enclosed in ‘<’ ’>’, also
known as angle brackets, for instance, the symbol associated with bolding
text is B and the start tag for this purpose is <B>. An end tag is the same as
that for a start tag, but with an exception that there exists a forward slash
preceding the text symbol of start tag: </B>. The instruction applied by an
element modifies whatever content is present between the starting and
ending tags:
<html>
<head>
<title>Page Title</title>
</head>
<body>
</body>
</html>
Output:
HTML 1.0:
HTML 1.0, introduced in 1993 by Tim Berners-Lee, marked the first standardized
version of HTML. It provided a basic set of markup tags for structuring and
HTML 2.0:
HTML 2.0, released in 1995, expanded upon the foundation established by HTML
1.0. It introduced support for forms, allowing users to interact with web pages
through text fields, checkboxes, and buttons, paving the way for more dynamic and
interactive web experiences.
HTML 3.2:
HTML 3.2, introduced in 1997, further extended the capabilities of HTML,
introducing support for tables, frames, and multimedia elements such as images,
audio, and video. These new features enabled developers to create more complex
layouts and incorporate multimedia content into their web pages.
HTML 4.01:
HTML 4.01, released in 1999, represented a major update to the HTML
specification. It introduced stricter rules and improved support for scripting and
styling, standardizing the Document Object Model (DOM) and introducing support
for cascading style sheets (CSS), allowing for more precise control over document
layout and presentation.
XHTML (Extensible Hypertext Markup Language):
XHTML emerged as a reformulation of HTML as an application of XML, designed
to be more compatible with XML-based technologies and standards. XHTML 1.0,
released in 2000, introduced stricter syntax rules and required documents to be
well-formed XML, helping to improve consistency and interoperability across
Tinsae D@2024 Internet and web design
different platforms and devices.
HTML5:
HTML5, introduced in 2014, represents the latest evolution of the HTML
specification. It introduces new semantic elements, APIs, and multimedia
capabilities, providing native support for audio and video playback, offline storage,
geolocation, canvas drawing, and responsive design. HTML5 is well-suited for
building modern web applications and multimedia-rich websites, reflecting the
ever-changing landscape of web development and the increasing demands of users
and developers alike.
2.2. HTML Elements, Tags and Attributes
While getting started with HTML, you will likely encounter new—and often
strange— terms. Over time you will become more and more familiar with all of
them, but the three common HTML terms you should begin with are elements,
tags, and attributes.
2.2.1. Elements
Before we start adding tags to our document, let’s look at the anatomy of an
HTML element (its syntax) and firm up some important terminology. HTML
elements are the building blocks of webpages. They define the structure and
content of a webpage by encapsulating different parts of the page within specific
tags. Each HTML element consists of an opening tag, content, and a closingtag.
The opening tag denotes the start of an element, the closing tag marks its end, and
the contentlies between the tags. Elements can be nested within each other,
creating a hierarchical structure that represents the relationship between different
HTML tags can be categorized into different types based on their purpose and
the behavior they define for the elements. Not all elements have content,
however. Some are empty by definition, such as the img element used to add an
image to the page.
Tinsae D@2024 Internet and web design
In HTML, the capitalization of element names is not important. So <img>,
<Img>, and <IMG> are all the same as far as the browser is concerned. However,
in XHTML (the stricter version of HTML) all element names must be all
lowercase in order to be valid. Many web developers havecome to like the
orderliness of the stricter XHTML markup rules and stick with all lowercase.
2.2.3. Attributes
HTML elements can also have attributes that provide additional information or
modify their behavior. Attributes are properties used to provide additional
information about an element. The most common attributes include the id
attribute, which identifies an element; the class attribute, which classifies an
element; the src attribute, which specifies a source for embeddable content; and
the href attribute, which provides a hyperlink reference to a linked resource.
Attributes are defined within the opening tag, after an element’s name. Generally
attributes includea name and a value. The format for these attributes consists of
the attribute name followed by an equals sign and then a quoted attribute value.
The syntax for an attribute is
as follows:
attributename="value"
Attributes go after the element name, separated by a space. In non-empty
<element attributename="value">
<element attributename="value">Content</element>
A. Core Attributes
B. Internationalization Attributes
C. Generic Attributes
The most widely used attribute is core attributes. There are 4 types of core
attributes:
Id
Class
Title
Tinsae D@2024 Internet and web design
Style
The Id Attribute:
This is the most widely used attribute. The id attribute is used to give a unique
id to an HTMLelement. Each element in HTML with an id attribute has its
own unique identity, just as each of us has our own unique identity. Multiple
elements can’t share the same id.
For example:
In the above example, it is easy to distinguish the two paragraphs with the
name- id attributehaving two different values i.e. html and python.
The class Attribute:
The id attribute is used to give a title to an HTML element. When the HTML
element isloading, a tooltip of the cursor comes and shows the written title.
For example:
There are three types of internationalization attributes i.e. dir, lang, xml:lang
The dir attribute tells the browser in which direction should the text flow.
There are twotypes of dir attributes:
ltr: Left to rightrtl: Right to left
Generic attributes include various attributes which are mostly used. Some
common genericattributes are:
The “align” Attribute:
align attribute uses align name for HTML element and uses left, right, and
center values toindicate the text accordingly. It is used for horizontal aligns
tags.
The “valign” Attribute:
valign attribute uses valign name for HTML element and uses top, middle, and
bottom valuesto indicate the text accordingly. It is used for vertical aligns tags.
The “bgcolor” Attribute:
bgcolor attribute uses bgcolor name to HTML element and uses numeric,
hexadecimal, RGBcode values to change the element's background color
accordingly.
The “width” Attribute:
The height attribute uses the height name of the HTML element and uses
numeric values tochange the element’s height according to the numeric given.
The “src” Attribute:
The src attribute is mostly used by the img element that we’ll be going to
discuss later. Thissrc attribute specifies the URL path to that element that is to
be displayed. We can choose any of the two paths:
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 relativeto the current page. Example:
src="img_girl.jpg". If the URL begins with a slash, it will be relative to
thedomain. Example: src="/images/img_girl.jpg".
When working with HTML, it's essential to understand the distinction between
block-level elements and inline-level elements. These two types of elements play
a crucial role in structuringand formatting the content of a web page. In this
article, we will explore the characteristics, behaviors, and use cases of block and
inline-level elements in HTML.
1. Block-Level Elements:
Block-level elements are HTML elements that create a block-level box. These
elements typicallystart on a new line and occupy the full width available within
their parent container. Some common examples of block-level elements include
<div>, <p>, <h1> to <h6>, <ul>, <li>, and
<section>.
Key characteristics of block-level elements:
Start on a new line: Block-level elements create a line break before and
after themselves,visually separating them from other content.
Creating lists: <ul> and <ol> elements, along with their list items <li>,
are block-levelelements used to create unordered and ordered lists.
2. Inline-Level Elements:
Inline-level elements, as the name suggests, are HTML elements that do not
create line breaks and are contained within a line of text or other inline
elements. Examples of inline-level elementsinclude <span>,
<a>, <strong>, <em>, <img>, and <input>.
Inserting images: The <img> element is used to insert images into a web
page. Althoughit is an inline-level element, it may affect the flow of
surrounding text.
Now that you know what HTML elements, tags, and attributes are, let’s take a
look at putting together our first web page. If anything looks new here, no
worries—we’ll decipher it as we go.
1. I don’t want to confuse things, but the first line in the example isn’t an element
at all; it is a document type declaration (also called DOCTYPE declaration)
that identifies this document as an HTML5 document. I have a lot more to say
about DOCTYPE declarations in Chapter 10,but for this discussion, suffice it
to say that including it lets modern browsers know they shouldinterpret the
document as written according to the HTML5 specification.
2. The entire document is contained within an html element. The html element is
called the root element because it contains all the elements in the document,
and it may not be contained within any other element. It is used for both
HTML and XHTML documents.
3. Within the html element, the document is divided into a head and a body. The
head element contains descriptive information about the document itself, such
as its title, the style sheet(s) ituses, scripts, and other types of “meta”
information.
4. The Meta elements within the head element provide information about the
document itself. A Meta element can be used to provide all sorts of
information, but in this case, it specifies the character encoding (the
Tinsae D@2024 Internet and web design
standardized collection of letters, numbers, and symbols) used in the
document. I don’t want to go into too much detail on this right now, but know
that there are many good reasons for specifying the charset in every document,
so I have included it as part of the minimal document structure.
5. Also in the head is the mandatory title element. According to the HTML
specification, every document must contain a descriptive title.
6. Finally, the body element contains everything that we want to show up in the
browser window.
Meta tags are a fundamental component of HTML that play a crucial role in
improving a website's visibility, accessibility, and overall performance. These
small snippets of code provide valuable information about a web page to search
engines, social media platforms, and browsers. In this section, we will explore
the importance of Meta tags in HTML and how they contribute to the success of
your website.
One of the primary benefits of Meta tags is their impact on search engine
optimization. Search engines use Meta tags to understand the content and
relevance of a web page, which influences itsranking in search results. Some key
meta tags for SEO include:
<title> tag: Defines the title of the web page. It appears in search engine
results and browsertabs as the clickable headline.
Meta tags also play a crucial role in how your website is shared and displayed on
social media platforms. When someone shares a web page on platforms like
Facebook or Twitter, Meta tags provide the necessary information to populate
the post with relevant content. Some key Meta tagsfor social media sharing
include:
By including these meta tags, you can control how your website appears when
shared on socialmedia, ensuring a consistent and appealing representation that
attracts users' attention.
Meta tags also contribute to improving the accessibility of your website. They
provide additional information to assistive technologies like screen readers,
enabling them to accurately interpret andpresent the content to users with
disabilities. For example:
By including these meta tags, you enhance the accessibility and usability of your
website, makingit more inclusive for all users.
HTML headings are titles or subtitles that you want to display on a webpage.
HTML also has six levels of headings, which use the elements <h1>, <h2>,
<h3>, <h4>, <h5>, and <h6>. Search engines use the headings to index the
structure and content of your web pages. Users often skim apage by its headings.
It is important to use headings to show the document structure. <h1>
headingsshould be used for main headings, followed by <h2> headings, then the
less important <h3>, andso on. While displaying any heading, browser adds one
line before and one line after that heading.
Example
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. Paragraphs are widely used for varioustypes of textual content,
such as articles, descriptions, and explanations. You can add attributes tothe <p>
element to provide additional information or control its appearance. Each
paragraph of text should go in between an opening <p> and a closing </p> tag.
Here's an example of a basic paragraph without any attributes::
Example
Style Attribute (style): Allows you to apply inline CSS styles directly to the
paragraph. Here's anexample of changing the font color and size:
In HTML, the horizontal line tag is represented by the <hr> element. It is used
to create a horizontal line or separator on a webpage. The <hr> tag is a self-
closing tag, meaning it doesn't require a closing tag. Here's an example of a basic
horizontal line:
Example
<!DOCTYPE html>
<html>
<head>
<title>Horizontal Line Example</title>
</head>
<body>
<p>This is paragraph one and should be on top</p>
<hr />
<p>This is paragraph two and should be at bottom</p>
</body>
</html>
You can also add attributes to the <hr> element to modify its appearance or
behavior. Here aresome commonly used attributes with examples:
Tinsae D@2024 Internet and web design
Width Attribute (width): Specifies the width of the horizontal line. It can be
set in pixels (px) oras a percentage of the container width.
<hr width="50%">
<hr size="3">
Align Attribute (align): Determines the alignment of the horizontal line within its
container. Thepossible values are left, center, right, or justify.
<hr align="center">
The <pre> tag stands for "preformatted text" and is used to display text exactly
as it is written, preserving all formatting, including spaces, line breaks, and
indentation. It is commonly used todisplay code snippets, poetry, or any content
that requires precise formatting. Here's an exampleof using the <pre> tag to
preserve formatting:
Example:
In HTML, there are several formatting elements that you can use to apply
specific styles or formatting to your content. These elements help structure and
present your text in a visually appealing and meaningful way. Here are some
commonly used formatting elements in HTML:
The <b> element and its semantic counterpart <strong> are used to make text
appear in a bold orstronger font weight.
The <i> element and its semantic counterpart <em> are used to make text appear
in an italicizedor emphasized style.
C. Underline (<u>):
The <s> element and its semantic counterpart <del> are used to display text with
a strikethroughor line through it.
E. Superscript (<sup>):
The <sup> element is used to display text as a superscript, typically for indicating
footnotes,mathematical exponents, or citations.
F. Subscript (<sub>):
The <sub> element is used to display text as a subscript, typically for chemical
formulas,mathematical subscripts, or annotations.
G. Code (<code>):
In HTML, there are specific elements designed for marking up quotations and
citations. These elements provide semantic meaning to the quoted or cited
content, allowing browsers, search engines, and assistive technologies to
interpret and present the information correctly. Here are thequotation and citation
elements in HTML:
A. Blockquote (<blockquote>):
Code snip
Tinsae D@2024 Internet and web design
<blockquote>
<p>This is a longer quotation that should be set apart from the main
text.</p>
<cite>John Doe</cite>
</blockquote>
Result
The <cite> element can be used within the <blockquote> element to provide
the source or authorof the quotation.
B. Q (<q>):
The <q> element is used to mark up short inline quotations within a paragraph or
sentence.
Code snip
<p>He said, <q>This is a short quotation</q>, and continued his
speech.</p>
Result
The <cite> element is used to indicate the title of a creative work, such as a
book, article, or song,or to provide a reference to the source of information.
Code snip
<p>The article titled <cite>HTML Basics</cite> provides an intr
D. Dfn (<dfn>):
The <dfn> element is used to mark the defining instance of a term within a
document. It iscommonly used in glossaries or to provide definitions of technical
terms.
Code snip
<p>The <dfn>HTML</dfn> stands for Hypertext Markup
Language</p>
Result
Code snip
<address>
<p>John Doe</p>
</address>
Result
Code snip
<bdo dir="rtl">This text is right-to-left.</bdo>
Result
By specifying the dir attribute with a value of "rtl" (right-to-left) or "ltr" (left-
to-right), you canchange the directionality of the text within the <bdo> element.
G. Abbreviation (<abbr>):
In the example above, the <abbr> element is used to mark the abbreviation
"HTML" and providethe full expansion using the title attribute. When the user
hovers over the abbreviation, the full expansion will be displayed as a tooltip.
Including the <abbr> element helps provide additional context and
understanding for abbreviations or acronyms used in your content. It enhances
accessibility, allowing assistive technologies to provide the full expansion of the
abbreviation when necessary.
These quotation and citation elements help provide structure and clarity to quoted
or cited contentin HTML documents. They aid in accessibility, search engine
optimization, and proper renderingacross different devices and browsers. It's
important to use these elements appropriately and in accordance with their
intended purpose to ensure accurate interpretation and presentation of the quoted
or cited information.
Comments start with <!-- and end with -->. Anything placed between these
delimiters will be treated as a comment and will not be rendered by the browser.
Here's an example of comments used in HTML code:
In the example above, comments are used to provide explanations and notes
within the HTML code. The comments labeled "CSS Styles" and "JavaScript
Scripts" indicate the purpose of the
linked files. The comment "Main Content" helps identify the main content
section within the body.The "TODO" comment serves as a reminder for future
development tasks.
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
<!-- CSS Styles -->
A. Color Values
In HTML, you can specify colors using various methods, including predefined
color names, hexadecimal color codes, RGB values, or HSL values. These color
representations allow you to define the color of text, backgrounds, borders, and
other elements on your web page. Here are thedifferent ways to specify colors in
HTML:
These methods can be applied to the color property using inline styles or in a
separate CSS file todefine the color of various HTML elements. Additionally,
you can use the same color representations for other CSS properties such as
Text Color:
You can use the color property to define the color of text within an HTML
element. You can specify the color using predefined color names, hexadecimal
color codes, RGB values, or HSL values.
Result
Result
The border-color property is used to specify the color of the border around an
HTML element. It can be combined with other border-related properties to
control the style, width, and placement ofthe border.
Result
There are many reasons why you might want to add an image to a web page: you
might want to include a logo, photograph, illustration, diagram, or chart. There
are several things to consider when selecting and preparing images for your site
but taking time to get them right will make it look more attractive and
professional. In this section you will learn how to:
A picture can say a thousand words, and great images help make the difference
between an averagelooking site and a really engaging one. Images can be used
to set the tone for a site in less time than it takes to read a description. If you do
not have photographs to use on your website, there arecompanies who sell stock
images; these are images you pay to use (there is a list of stock photography
websites below). Remember that all images are subject to copyright, and you can
getin trouble for simply taking photographs from another website. Images
Should...
Be relevant
Convey information
Convey the right mood
Be instantly recognizable
Fit the color palette
Image file types
When using the <img> element, you can supply JPEG (.jpg or .jpeg), GIF (.gif),
PNG (.png), or SVG (.svg) files. The following is a brief description of each file
type that should help you decidewhich file type to use for your application.
JPG Also known as JPEG, this is best for photographs because it offers high
Tinsae D@2024 Internet and web design
compressionand up to
16.8 million color combinations, butthe compression algorithm is lossy,
meaningthat you lose detail every time you save the file.
GIF GIF is great to use on small images that have a fixed number of colors.
GIF also supports transparent color. GIF uses lossless compression and is
best for logos and worst for photos. GIF also supports the ability to
encapsulate multiple images in one file, whichis commonly used to provide
animated GIFs.
PNG PNG is a great all-around file type due to its lossless high
compression. PNG files can be 48- bit true color or 16-bit grayscale. PNG
not only supports transparent color but also offers variable transparency.
Photos aren’t compressed to be as small as JPG photos but being lossless
makes it worth the extra size in many scenarios. You might use PNG asyour
storage type for photos that you want to edit, but when displaying them on
the web, you might want to save the PNG as JPG to achieve the best
compression.
SVG SVG is Scalable Vector Graphics and is great for drawings but not
for photos. SVGimages can be scaled up or down without losing detail
because the file contains the instructions to draw the image, as opposed to
the other file types that contain a raster-basedimage. Raster-based images
are composed of color dots that make up the image. If you need to scale a
raster-based image up or down, you will see that in color, dots are re-
sampled, and the image typically ends up looking blocky.
Tinsae D@2024 Internet and web design
Adding Images
The <img> tag is an HTML element used to display images on a web page. It is
a self-closing tag,meaning it does not have a closing tag. The <img> tag requires
the src attribute, which specifies the source URL or file path of the image. It also
supports several optional attributes to provide additional information and control
the behavior and appearance of the image.
By providing the file path relative to the location of the current web page
file.
We will first discuss inserting the image to the webpage & simultaneously, we
will understandboth the above approaches.
Adding images on a webpage: The <img> tag is used to add or embed the images
Tinsae D@2024 Internet and web design
to a webpage/website. The “img” tag is an empty tag, which means it can contain
only a list of attributes and it has no closing tag. The addition of the images
improves the quality along with enhancing the design structure, appearance of
the webpage. Nowadays, a website does not directlyadd images to a web page,
as the images are linked to web pages by using the <img> tag which holds space
for the image.
Syntax:
<imgsrc="url"alt="some_text"width=""height="">
Attributes:
The <img> tag has following attributes:
src: It is used to specify the path to the image.
crossorigin: It is used to import images from third-party sites that allow cross-
origin access
Example 1: This simple example illustrates the use of the <img> tag in HTML
that is used toembed the image into the webpage.
own or it willbe provided by your course instructor. Here is the output of the
above example:Output of the above code in a browser:
Every image has an src attribute which tells the browser where to find the image
you want to display. The URL of the image provided points to the location where
the image is stored. When the webpage loads for the first time, then the browser
gets the image from a web server and insertsit into the page. If the image is not
spotted by the browser then users will get a broken link icon. Itmight be possible
if the file path is wrong or the image got deleted from that location.
Tinsae D@2024 Internet and web design
Example 2: Use of src attribute
The example illustrates the use of the src attribute in the <img> tag.
<!DOCTYPE html>
<html>
<head>
<title>src Attribute Example</title>
</head>
<body>
<p>Insert an image from the same folder with the file:</p>
<img src="img_girl.jpg">
<p>Insert an image from another folder:</p>
<img src="/images/stickman.gif">
<p>Insert an image from a web site:</p>
<img src="https://www.w3schools.com/images/lamp.jpg" >
</body>
</html>
Note: on the above example you can change the value of the src
attribute by your own or it will be provided by your course instructor.
Here is the output of the above example:
alt: If the image cannot be displayed then the alt attribute acts as an alternative
description for theimage. The value of the alt attribute is a user-defined text. It
generally happens when the user, forsome reason, cannot view it due to a slow
internet connection or an error in the src attribute, or ifthe user uses a screen
reader.
Example 3: The example illustrates the use of the alt attribute in the <img> tag.
<p>inserted image using "img" tag and Adding image name in usi
ng "alt" attribute:</p>
<img src="" alt="Girl in a jacket">
</body>
</html>
Output:
Example 4: The example illustrates the use of the width & height attribute in the
<img> tag.
<!DOCTYPE html>
<html>
<body>
<h2>Image Size</h2>
<p>Here we specify the width and height of an image with the width an
d height attributes:</p>
</body>
</html>
Alternatively, You can use the style attribute to specify the width and height of
an image.
Example 5: The example illustrates the use of the style attribute to set the width
& height of animage.
<html>
<body>
<h2>Image Size</h2>
<p>Here we use the style attribute to specify the width and height of an image:</p>
</body>
</html>
Note that: Both the above examples (4 and 5) have the same output on the
browser.
Common Image Format: Here is the commonly used image file format that is
supported by allthe browsers.
The <audio> tag is an HTML element used to embed audio content within a web
page. It providesa native way to play audio files directly in the browser
without the need for external plugins. Youcan use the
<audio> tag to add various audio formats such as MP3, WAV, or OGG to
your web pages. Here's an example of the <audio> tag with some commonly
used attributes:
In this example:
src: Specifies the source URL or file path of the audio file. It is a required
attribute.
controls: Adds default audio controls (play, pause, volume, etc.) to the
audio player,allowing users to control the playback.
autoplay: Starts playing the audio automatically when the page loads.
The content placed between the opening and closing <audio> tags, in this case,
"Your browser does not support the audio element," will be displayed if the
browser does not support the <audio>tag or the specified audio format.
Tinsae D@2024 Internet and web design
Here's another example demonstrating additional attributes:
</audio>
Preload: Specifies the audio preloading behavior. The value "metadata" indicates that
onlythe audio metadata, such as duration, should be loaded. Other values include
"auto" (the entire audio file is preloaded) and "none" (no preloading).
Volume: Sets the initial volume of the audio. The value ranges from 0.0
(silent) to 1.0 (fullvolume).
These are just a few examples of the attributes that can be used with the <audio>
tag. Other attributes, such as muted, controlsList, crossorigin, and more, provide
additional control and customization options for embedding and controlling
audio content on a web page.Remember to provide fallback content within the
<Audio> tags, as shown in the examples, so that users can understand and interact
with the audio content if their browser does not support the <audio> tag or the
specified audio format.
The <video> tag is an HTML element used to embed video content within a web
page. It providesa native way to play video files directly in the browser without
the need for external plugins. Withthe <video> tag, you can add various video
formats such as MP4, WebM, or Ogg to your web pages.
<!DOCTYPE html>
<html>
<body>
<video controls autoplay loop width="640" height="360">
<source src="movie.mp4" type="video/mp4"> Your
browser does not support the video tag.
</video>
</body>
</html>
In this example:
</video>
Lists are used to group together related pieces of information, so they are clearly
associated with each other and easy to read. In modern web development, lists
are workhorse elements, frequentlyused for navigation as well as general content.
Humans are natural list makers, and HTML provides elements for marking up
three types of lists:
A. Unordered lists
Unordered lists are often referred to as bulleted lists. Instead of being numbered,
each element in the list has the same marker. The markup to create an unordered
list looks just like an ordered listexcept that the list is created by using
<ul>...</ul> tags rather than ol. The elements of the list areplaced within <li>
tags, just as with ordered lists. Browsers have standardized on using a solid bullet
to mark each item in an unordered list by default. Text browsers usually use an
asterisk forthese lists. The following input and output example shows an
unordered list. example shows the results in a browser.
Example
Tinsae D@2024 Internet and web design
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<ul>
</ul >
</body>
Output
As with ordered lists, unordered lists can be customized using the type attribute
or the list-style- type property. As mentioned in the section on ordered lists, the
type attribute is no longer valid forHTML5. The bullet styles are as follows:
Example
</ul>
</ul>
Output
If you don’t like any of the bullet styles used in unordered lists, you can substitute
an image of your own choosing in place of them. To do so, use the list-style-
image property. By setting this property, you can use an image of your choosing
for the bullets in your list. Here’s an example :
<li>Example</li>
</ul>
Nesting Lists
A nested list is a list inside another list. You can create a nested unordered list, or
a nested orderedlist, or even an ordered list nested inside an unordered one.
Remember that the only direct child ofthe <u>l tag is
<li>.
<li>DSA</li>
<ul>
<li>Array</li>
<li>Linked List</li>
<li>stack</li>
<li>Queue</li>
</ul>
<li>Web Technologies</li>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
<li>Aptitude</li>
<li>Gate</li>
<li>Placement</li>
another list.
B. Ordered lists
Ordered lists are surrounded by the <ol>...</ol> tags (ol stands for ordered list),
and each item within the list is included in the <li>...</li> (list item) tag. When
the browser displays an ordered list, it numbers and indents each of the elements
sequentially. You don’t have to perform the numbering yourself and, if you add
or delete items, the browser renumbers them the next time thepage is loaded.
Ordered lists are lists in which each item is numbered or labeled with a counter
ofsome kind (like letters or roman numerals).
Use numbered lists only when the sequence of items on the list is relevant.
Ordered lists are goodfor steps to follow or instructions to the readers, or when
Tinsae D@2024 Internet and web design
you want to rank the items in a list. If you just want to indicate that something
has a number of elements that can appear in any order, use an unordered list
instead. For example, the following is an ordered list of steps that explain how
to boil an egg.
You can see how the list is displayed in a browser in the example bellow.
Example:
Output :
Tinsae D@2024 Internet and web design
Figure 2-10 An ordered list in HTML
Customizing Ordered Lists
There are two customizations that are specific to ordered lists. The first enables
you to change thenumbering style for the list, and the second enables you to
change the numbering itself. There aretwo ways to change the numbering style:
the CSS property list-style-type, and the type attribute, which is obsolete in
HTML5. If you’re creating a new ordered list, you should always use the
CSSproperty, however, you may see existing Web pages in which the type
attribute is used instead.
Attributes:
Tinsae D@2024 Internet and web design
compact: It defines the list should be compacted (compact attribute is not
supported in HTML5. Use CSS instead.).
reversed: It defines that the order will be descending.
start: It defines from which number or alphabet the order will start.
type: It defines which type(1, A, a, I, and i) of the order you want in your
list of numeric,alphabetic, or roman numbers.
You can specify types of numbering in the <ol> tag using the style attribute, like
this:
<li>Lundi</li>
<li>Mardi</li>
<li>Mercredi</li>
<li>Jeudi</li>
<li>Vendredi</li>
<li>Samedi</li>
<li>Dimanche</li>
</ol >
Output:
You can also use the list-style-type property with the <li> tag, changing the
Tinsae D@2024 Internet and web design
numbering type in themiddle of the list, but you need to change every list item
following it if you want them all to havethe same new type. Using the start
attribute, you can specify the number or letter with which to start your list. The
default starting point is 1, of course. You can change this number by using start.
<ol start="4">, for example, would start the list at number 4, whereas <ol
style="list-style-type: lower- alpha"start="3"> would start the numbering with c
and move through the alphabet from there. The value for the start attribute should
always be a decimal number, regardless of the numbering style being used. For
example, you can list the last six months of the year and start numbering with
the Roman numeral VII as follows. The results appear in Figure 2:14
Example:
st<
Output:
Bl
Output :
C. Definition Lists
Definition lists differ slightly from other lists. Each list item in a definition list has
two parts:
A term
Each part of the glossary list has its own tag: <dt> for the term (definition term ),
and <dd> forits definition (definition description). <dt> and <dd> usually occur
in pairs, although most browsers can handle single terms or definitions. The
entire glossary list is indicated by the tags
<dl>...</dl> (definition list).
The following is a glossary list example with a set of herbs and descriptions of
how they grow(see Figure 2.14):
<dt>Basil</dt>
ur
is heavenly</dd>
stab
</dl>
Output:
Creating tables in HTML is a degree more complex. Think about how many
different types of tables there are. A table can be a three-by-three grid with labels
across the top, or two side-by-sidecells, or a complex Excel spreadsheet that
comprises many rows and columns of various sizes. Representing tables in
HTML is heavy on tags, and the tags can be hard to keep track of when youget
going.
The basic approach with table creation is that you represent tabular data in a linear
fashion, specifying what data goes in which table cells using HTML tags. In
HTML, tables are created from left to right and top to bottom. You start by
creating the upper-left cell and finish with the bottom-right cell. This will all
become clearer when you see some actual table code .
Table Parts
Before getting into the actual HTML code to create a table, here are some table-
related terms you’llsee throughout this lesson:
The caption indicates what the table is about: for example, “Voting
Statistics, 1950–1994,” or
“Toy Distribution Per Room at 1564 Elm St.” Captions are optional.
The table headings label the rows, columns, or both. Usually they’re in an
emphasized font that’s
Table cells are the individual squares in the table. A cell can contain
normal table data ora table heading.
Table data is the values in the table itself. The combination of the table
headings and table data makes up the sum of the table.
<caption>Vital Statistics</caption>
<tr>
<th>Name</th>
<th>Height</th>
<th>Weight</th>
<th>Eye Color</th>
<tr
>
</tr>
</tr>
<tr
>
</tr
>
Tinsae D@2024 Internet and web design
</tr <td>Tom</td>
>
<td>6'0"</td>
<tr
<td>165</td>
> <td>
Aliso <td>Hazel</td>
n</td
>
<td>5
'4"</t
d> <td>Susan</td>
<td> <td>5'1"</td>
140< <td>97</td>
/td>
<td>Brown</td>
<td>
Blue<
/td>
Now that you’ve been introduced to the <table> element, we’ll move on to the
rows and cells. Inside the
<table>...</table> element, you define the actual contents of the table. Tables are
specified in HTML row by row, and each row definition contains all the cells in
that row. So, to create a table, you start with the top row and then each cell in turn,
from left to right. Then you define a second row and its cells, and so on. The
number of columns is calculated based on how many cells there are in each row.
Each table row starts with the <tr> tag and ends with the closing
</tr>. Your table can have as many rows and columns as you like, but you should
make sure that each row has the same number of cells so that the columns line
up. The cells within each row arecreated using one of two elements:
<th>...</th> elements are used for heading cells. Generally,
browsers center the contents of a <th> cell and render any text in the
cell in boldface.
<td>...</td> elements are used for data cells. td stands for table data.
<th>Name</th>
<th>Height</th>
<th>Weight</th>
<th>Eye Color</th>
</tr>
the followingcode:
The top row is followed by three rows of data cells, which are coded as follows:
<td>5'4"</td>
</tr>
</tr>
<td>5'1"</td>
</tr>
As you’ve seen, you can place the headings along the top edge by defining the <th>
elements inside
the first row. Let’s make a slight modification to the table. You’ll put the
headings along the left edge of the table instead. To accomplish this, put each
<th> in the first cell in each row and followit with the data that pertains to each
heading. The new code looks like the following:
</tr>
<td>5'4"</td>
<td>6'0"</td>
<td>5'1"</td>
</tr>
</tr>
Output:
Tinsae D@2024 Internet and web design
Figure 2-17 An example of a table that includes headings in the leftmost column.
Captions
Table captions tell your visitor what the table is for. The <caption> element,
created just for this purpose, displays the text inside the tag as the table caption
(usually centered above the table). Although you could use a regular paragraph
or a heading as a caption for your table, tools that process HTML files can extract
<caption> elements into a separate file, automatically number them, or treat them
in special ways simply because they’re captions.
If you don’t want a caption, it’s optional. If your table is understandable without
a caption or you have described it in some other location, you can leave it off.
<table>
<caption>Vital Statistics</caption>
<tr >
rows, and it containsthe title of the table. It closes with the </caption> tag:
You can include details inside a caption to provide additional information about
the table that is
hidden by default. When you include details, you need to also include a summary
that acts as a title for the additional details. Chrome, Safari, and Opera all support
showing and hiding content with the <detail> and
<summary> tags. You can use the <details> and <summary> tags inside the
<caption> tag:
<caption>
<details>
<summary>Vital Statistics</summary>
<p>This table includes the name, height, and, weight of various employees.</p>
</details>
</caption>
Figure 2.18 shows a <detail> element that has been clicked on to show the
hidden content.
2.10.HTML Forms
HTML forms provide a way for users to interact with a website by submitting
data. HTML Formsare used to select different kinds of user input. HTML forms
are used to pass data to a server. Theyare essential for capturing user input, such
as text, selections, and file uploads. HTML forms consist of various form
elements, including input fields, checkboxes, radio buttons, dropdown lists,and
buttons. These elements are configured using attributes to define their behavior
and appearance. An HTML form can contain input elements like text fields,
checkboxes, radio-buttons,submit buttons and more. HTML provides a range of
form elements that allow developers to createinteractive forms for collecting user
input. Each form element is configured using variousattributes to define its
behavior and appearance. In addition to the standard HTML form
elements,HTML5 introduced new form elements and attributes that enhance the
Tinsae D@2024 Internet and web design
form-building process. Let's
explore both the standard and HTML5 form elements along with their attributes:
The <form> tagis used to create an HTML form:
Example
</form>
<form>: Defines a container for form elements. Wraps all form elements
and provides thenecessary context for form submission.
The form attributes
method: Specifies the HTTP method used for form submission (e.g.,
"GET" or"POST").
2.10.1. HTML Forms - The Input Element
The most important form element is the <input> element. The <input> element
is used to selectuser information. An <input> element can vary in many ways,
depending on the type attribute. An <input> element can be of type text field,
checkbox, password, radio button, submit button,and more. The most common
input types are described below.
<input type="text"> defines a one-line input field that a user can enter text or
alphanumericcharacters. The default width of a text field is 20 characters. :
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" placeh
older="Enter your username" required maxlength="20">
</form>
In this example, we have a form field for capturing the user's username. Let's
break down theattributes and their significance:
type="text": Specifies that the input field should accept text input.
B. Password Field
C. Radio Buttons
In this example, we have a set of radio buttons for selecting the preferred
language. Let's breakdown the attributes and their significance:
type="radio": Specifies that the input field should be rendered as a radio
button.
<form>
<label for="option1">Option 1:</label>
<input type="checkbox" id="option1" name="options[]" value="op
tion1">
If you type some characters in the text field above, and click the "Submit" button, the
browser willsend your input to a page called "demo_form_action.asp". The page will
show you the received input.
2.10.2. Text area
The <textarea> element is used to create a multi-line text input field in HTML. It
allows users to enter and edit larger amounts of text. Here's an example of
<textarea> with attributes and a description:
<form>
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="30" placeho
lder="Enter your message" required></textarea>
</form>
In this example, we have a textarea for capturing a message from the user. Let's
break down theattributes and their significance:
rows="4": Specifies the visible number of rows (lines) for the textarea.
In this case, thetextarea will display four rows.
In this example, we have a select menu for choosing a country. Let's break down the
attributesand their significance:
<option> elements: Represent the available options within the select menu.
Each <option>element contains a value attribute and the text content that
represents the option. Users canchoose one of these options.
The selected option in the select menu will be sent to the server upon form
submission. By default,the first option (<option>) in the list is displayed as the
initially selected option. In the example, "Ethiopia" will be the initially selected
<form>
<button type="button" id="submitBtn" onclick="submitForm()">Logi
n</button>
</form>
In this example, we have a button that triggers a form submission. Let's break down
the attributesand their significance:
type="button": Specifies that the button element should be rendered as a
regular button.It is used to perform custom actions or trigger JavaScript
functions.
onclick="submitForm()": Specifies the JavaScript code or function to be
executed whenthe button is clicked. In this example, the submitForm()
Tinsae D@2024 Internet and web design
function will be called.
Button content: The text "Submit" is the content of the button, which is
displayed to theuser.
When the button is clicked, it triggers the JavaScript function specified in the
onclick attribute. The function can perform various actions, such as validating
form data, manipulating the DOM, or initiating an AJAX request.
2.10.5. HTML5 Form Elements and Attributes:
HTML5 introduced several new form elements and attributes to enhance the
functionality and userexperience of web forms. Here are some of the notable
HTML5 form elements and attributes. These HTML5 form elements and
attributes enhance the functionality and user experience of webforms by
providing built-in validation, specialized input types, and more interactive
controls. Theyallow developers to create more intuitive and user-friendly forms
while reducing the need for custom JavaScript validation and user input
handling. Here is a combined example that illustratesHTML 5 form elements.
<form>
<fieldset>
<legend>Contact Information</legend>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="website">Website:</label>
<input type="url" id="website" name="website">
<label for="phone">Phone:</label>
<legend> element that provides a caption or title for the fieldset. Inside the
fieldset, there arevarious input fields with different types and attributes.
<input type="email"> represents an email input field where users can
enter their emailaddresses. The required attribute ensures that the field
must be filled out.
<input type="url"> represents a URL input field for users to enter website
addresses.
This combined example showcases the usage of different input types and
attributes to create a formwith various data input fields. The <fieldset> and
<legend> elements provide structure and a caption for the related fields,
enhancing the form's usability and organization.
The HTML <div> element is a block level element that can be used as a container
for grouping other HTML elements. The <div> element has no special meaning.
Except that, because it is a block level element, the browser will display a line
break before and after it. When used togetherwith CSS, the <div> element can
be used to set style attributes to large blocks of content. Anothercommon use of
the <div> element, is for document layout. It replaces the "old way" of defining
layout using tables. Using <table> elements for layout is not the correct use of
<table>. The purpose of the <table> element is to display tabular data.
Example
<!DOCTYPE html>
<html>
<head>
<title>Line Break Example</title>
</head>
<body>
<div id="container" class="wrapper">
<h1>Welcome to <span class="highlight">My Website</span></h1>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Vestibulum consectetur, risus vel fermentum malesuada.</p>
<p>Suspendisse potenti. <span class="highlight">Vivamus</spa
n> accumsan nibh eu leo tincidunt, sit amet ultrices lectus luct
us.</p>
</div>
</div>
</body>
</html>
In this example, we have a <div> element with the attributes id="container" and
class="wrapper".Inside the
<div>, there is an <h1> element and another <div> element with the class
"content". Within the "content"
<div>, there are two <p> elements. The <span> element with the class
"highlight" is used within the <h1> and one of the <p> elements. Let's break
down the example:
<div id="container" class="wrapper">: The outer <div> acts as a
container with the assigned ID "container" and the class "wrapper". This
can be used for applying styles or targeting the container with JavaScript.
In this combined example, the <div> element is used as a container to group and
organize the content of the webpage. The <span> element is used within the <h1>
and <p> elements to highlightspecific words or phrases. You can style or
manipulate the container <div> using the assigned IDor class, and target the
highlighted text using the specified class.
Tag Desc
Defines a header for a document
header
or a section
Defines a container for
nav
navigation links
section Defines a section in a document
Defines an independent self-
article
contained article
Defines content aside from the
aside
content (like a sidebar)
Defines a footer for a document
footer
or a section
details Defines additional details
<style>
body {
}
header {
background-color:
#333;color: #fff; text-
align: center;padding:
10px;
}
nav
{
background-color:
#444;color: #fff; text-
align: center;padding:
}
10px;
color: #fff;
nav a:hover {
background-color: #555;
.container {
main {
aside {
flex: 1;
footer {
</style>
</head>
<body>
<header>
</header>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</nav>
<div class="container">
</p>
</main>
<aside>
<h3>Side Bar</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisci
ng elit. Nulla eget nulla vel nisi commodo rutrum in at nunc.
</p>
</aside>
</div>
<footer>
<p>© 2023 Simple Website. All rights reserved.</p>
</footer>
<!DOCTYPE html>: This line specifies the document type and version,
indicating that thedocument follows the rules of HTML5.
<html>: The root element of the HTML document, encompassing all the
content.
Tinsae D@2024 Internet and web design
<head>: Contains metadata about the document, such as the page title
and links to external stylesheets.
<title>: Specifies the title of the webpage that appears in the browser's title
bar or tab.
<style>: This section includes inline CSS code that defines the styles for
various elementsused in the layout.
<body>: The main content of the webpage is placed within this element.
The output:
The div element is a block level element, designed for grouping HTML elements.
But layout canbe designed using <div> elements, because CSS can position and
style <div> elements. The following example uses five <div> elements to create
a multiple column layout:
Example
<b>Menu</b><br>
HTML<br>
CSS<br>
JavaScript</div>
</div>
</body>
The purpose of the <table> element is to display tabular data. But layout can be
designed using
<table> element, because all table elements can be styled with CSS. The
following example usesa table with 3 rows and 2 columns - the first and last
row spans 2 columns using the colspan attribute:
Example
Tinsae D@2024 Internet and web design
<!DOCTYPE html>
<html>
<body>
<tr>
</td>
</tr>
<tr>
<td style="background-color:#FFD700;width:100px;vertical-align:top;">
<b>Menu</b><br>HTML<br>
CSS<br> JavaScript
</td>
<td style="background-
color:#eeeeee;height:200px;width:400px;vertical-align:top;">
Content goes here</td>
</tr>
</tr>
</table>
</body>
</html>
143
12. Explain the purpose of the <iframe> tag in HTML and give
examples of its usage.
13. How do you embed audio and video files in an HTML document?
Provide examples using the <audio> and <video> tags.
14. Describe the <meta> tag in HTML and its role in providing
metadata about the document.
15. What is the purpose of the <script> tag in HTML, and how do
you include external JavaScript files?
16. Explain the <blockquote> tag in HTML and its significance for
displaying quoted content.
17. How do you create headings (<h1> to <h6>) in HTML, and
what is their semantic significance?
18. Describe the <form> tag attributes action and method, and their
role in form submission.
19. Explain the use of the <abbr> tag in HTML for defining
abbreviations or acronyms, and provide an example.
20. How do you create a line break in HTML? Compare and contrast the
<br> tag with the CSS
proper white-space: .
ty pre;
144
Lesson 3: CSS
CSS is the acronym for "Cascading Style Sheet". It's a style sheet
language used for describing the presentation of a document written in a
markup language like HTML. CSS helps the web developers to control
the layout and other visual aspects of the web pages. CSS plays a crucial
role in modern web development by providing the tools necessary to
create visually appealing, accessible, and responsive websites.
CSS Versions
Since the inception of CSS, several versions have come into existence.
Some of the notable versions include:
145
CSS2.1 - The version 2.1, published as a W3C Recommendation in
June 2011, clarified and refined CSS2, addressing inconsistencies
and ambiguities in the specification. CSS2.1 focused on improving
interoperability among web browsers.
CSS3 (Cascading Style Sheets Level 3) - CSS3 is a collection of
modules that extend the capabilities of CSS. It introduces numerous
new features and enhancements, including advanced selectors,
multiple column layouts, animations, transformations, gradients,
shadows, and more.
CSS4 (Cascading Style Sheets Level 4) - CSS4 is an ongoing effort
to extend CSS3 with new features and enhancements.
Each version of CSS builds upon the previous ones, adding new features
and refining existing capabilities to meet the evolving needs of web
developers and designers. CSS is referred as just CSS now, without a
version number.
Components of CSS
CSS works by associating rules with HTML elements. A CSS rule contains two
main parts:
146
semicolons.
147
Each declaration includes a property name and a value, specifying the
aspect of the element's presentation to control.
Sample Code
<html>
<head>
<title>CSS Tutorial</title>
<style>
h1 {
color: #36CFFF;
}
p{
font-size: 1.5em;
color: white;
}
div {
border: 5px inset gold;
background-color: black;
width: 300px;
text-align: center;
}
</style>
</head>
<body>
<div>
<h1>Hello World!</h1>
<p>This is a sample CSS code.</p>
</div>
</body>
</html>
Just to give you a little excitement about CSS, here is a sample CSS snippet for
your reference.
148
In the above CSS snippet:
h1, p, and div are the selectors that target the <h1>, <p>, and <div>
elements.
color, font-size, border, background-color, width, and text-align are the
properties.
#36CFFF, 1.5em, white, 5px inset gold, black, 300px, and center
are the corresponding values passed to these properties.
CSS handles the look and feel part of a web page. Using CSS, you can
control the color of the text, the style of fonts, the spacing between
paragraphs, how columns are sized and laid out, what background images
or colors are used, layout designs,variations in display for different
devices and screen sizes as well as a variety of other effects.
149
CSS is easy to learn and understand but it provides powerful control over
the presentation of an HTML document. Most commonly, CSS is
combined with the markup languages HTML or XHTML.
Advantages of CSS
CSS saves time − You can write CSS once and then reuse same
sheet in multiple HTML pages. You can define a style for each
HTML element and apply it to as many Web pages as you want.
Pages load faster − If you are using CSS, you do not need to write
HTML tag attributes every time. Just write one CSS rule of a tag
and apply it to all the occurrences of that tag. So less code means
faster download times.
Easy maintenance − To make a global change, simply change the
style, and all elements in all the web pages will be updated
automatically.
Superior styles to HTML − CSS has a much wider array of
attributes than HTML, so you can give a far better look to your
HTML page in comparison to HTML attributes.
Multiple Device Compatibility − Style sheets allow content to be
optimized for more than one type of device. By using the same
HTML document, different versions of a website can be presented
for handheld devices such as PDAs and cell phones or for printing.
Global web standards − Now HTML attributes are being
deprecated and it is being recommended to use CSS. So its a good
150
idea to start using CSS in all the HTML pages to make them
compatible to future browsers.
CSS – Syntax
A CSS comprises of style rules that are interpreted by the browser and then
applied to the corresponding elements in your document. A style rule is made of
three parts −
Selector − A selector is an HTML tag at which a style will be
applied. This could be any tag like <h1> or <table> etc.
Property − A property is a type of attribute of HTML tag. Put
simply, all the HTML attributes are converted into CSS
properties. They could be color, border etc.
Value − Values are assigned to properties. For example, color
property can have value either red or #F1F1F1 etc.
151
Example − You can define a table border
as follows − table{ border :1px solid #C00;
}
Here table is a selector and border is a property and given value 1px
solid #C00 is the value of that property.
You can define selectors in various simple ways based on your comfort.
Let me put these selectors one by one.
The Type Selectors
This is the same selector we have seen above. Again, one more example
to give a color to all level 1 headings −
h1 {
color: #36CFFF;
}
152
The Universal Selectors
* {
color: #000000;
}
This rule renders the content of every element in our document in black.
The Descendant Selectors
Suppose you want to apply a style rule to a particular element only when
it lies inside a particular element. As given in the following example, style
rule will apply to <em> element only when it lies inside <ul> tag.
ul em {
color: #000000;
}
The Class Selectors
You can define style rules based on the class attribute of the elements.
All the elements having that class will be formatted according to the
defined rule.
.black {
color: #000000;
}
This rule renders the content in black for every element with class attribute set to black
in our document. You can make it a bit more particular. For example −
153
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class
attribute set to black. You can apply more than one class selectors to
given element. Consider the following example −
<p class = "center bold">
This para will be styled by the classes center and bold.
</p>
The ID Selectors
You can define style rules based on the id attribute of the elements.
All the elements having that id will be formatted according to the
defined rule.
#black {
color: #000000;
}
This rule renders the content in black for every element with id
attribute set to black in our document. You can make it a bit more
particular. For example −
h1#black {
color: #000000;
}
154
This rule renders the content in black for only <h1> elements with id attribute set
to black.
#black h2 {
color: #000000;
}
The true power of id selectors is when they are used as the foundation
for descendant selectors, For example −
In this example all level 2 headings will be displayed in black color
when those headings will lie with in tags having id attribute set to black.
The Child Selectors
You have seen the descendant selectors. There is one more type of
selector, which is very similar to descendants but have different
functionality. Consider the following example −
body > p {
color: #000000;
This rule will render all the paragraphs in black if they are direct child of
<body> element. Other paragraphs put inside other elements like <div>
or <td> would not have any effect of this rule.
The Attribute Selectors
You can also apply styles to HTML elements with particular attributes.
The style rule below will match all the input elements having a type
attribute with a value of text −
155
input[type = "text"] {
color: #000000;
}
The advantage to this method is that the <input type = "submit" /> element is
unaffected, and the color applied only to the desired text fields.
There are following rules applied to attribute selector.
p[lang] − Selects all paragraph elements with a lang attribute.
p[lang="fr"] − Selects all paragraph elements whose lang
attribute has a value of exactly "fr".
p[lang~="fr"] − Selects all paragraph elements whose lang
attribute contains the word "fr".
p[lang|="en"] − Selects all paragraph elements whose lang
attribute contains values that are exactly "en", or begin with "en-".
Multiple Style Rules
You may need to define multiple style rules for a single element. You
can define these rules to combine multiple properties and corresponding
values into a single block as defined in the following example −
h1 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
156
Here all the property and value pairs are separated by a semicolon (;).
You can keep them in a single line or multiple lines. For better
readability, we keep them in separate lines.
For a while, don't bother about the properties mentioned in the above
block. These properties will be explained in the coming chapters and you
can find complete detail about properties in CSS References
Grouping Selectors
You can apply a style to many selectors if you like. Just separate the
selectors with a comma, as given in the following example −
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
This define style rule will be applicable to h1, h2 and h3 element as well. The order of
the list is irrelevant. All the elements in the selector will have the corresponding
declarations applied to them.
You can combine the various id selectors together as shown below −
157
CSS - Selectors
CSS selectors are patterns used to select and style HTML elements on a
web page. They allow you to target specific elements or groups of
elements to apply styles like colors, fonts, margins, and more. CSS
selectors are a fundamental part of Cascading Style Sheets (CSS), which
is a language used to control the presentation and layout of web
documents.
The element or elements that are selected by the selector are referred to as subject
of the selector.
Selector lists
If same CSS is used by more than one selector, then these selectors can
be combined together to form a selector list. Thus the CSS rule is
applied to all the individual selectors.
p {
color: crimson;
}
.sample {
color: crimson;
}
158
p, .sample {
color: crimson;
}
But, we can combine these two rules into one selector list, by adding a
comma to separate them as shown below:
Following syntax will be deemed invalid, as one of the selector is invalid
(..sample - is an incorrect way of defining a class).
p {
color: crimson;
}
..sample {
color: crimson;
}
p, ..sample {
color: crimson;
}
159
p {
color: green;
}
h1 {
text-decoration-line: underline;
}
<html>
<head>
<style>
div {
border: 5px inset gold;
width: 300px;
text-align: center;
}
p {
color: green;
}
h1 {
text-decoration-line: underline;
}
</style>
</head>
<body>
<div>
<h1>Type selector</h1>
<p>div with border and text-aligned to center</p>
<p>paragraph with green color</p>
<p>h1 with an underline</p>
</div>
</body>
</html>
A class selector targets an element with a specific value for its class attribute.
160
.style-h1 {
text-decoration-line: underline;
}
.style-p {
color: green;
font-size: 25px;
}
<html>
<head>
<style>
.style-div {
border: 5px inset gold;
width: 300px;
text-align: center;
}
.style-p {
color: green;
font-size: 25px;
}
.style-h1 {
text-decoration-line: underline;
}
</style>
</head>
<body>
<div class="style-div">
<h1 class="style-h1">class selector</h1>
<p class="style-p">class .style-p applied</p>
<p>No class applied on this p element</p>
</div>
</body>
</html>
161
CSS Selector - ID Selector
#style-p {
color: green;
font-size: 25px;
}
#style-h1 {
text-decoration-line: underline;
color: red;
}
162
<html>
<head>
<style>
#style-div {
border: 5px inset purple;
width: 300px;
text-align: center;
background-color: lightgoldenrodyellow;
}
#style-p {
color: green;
font-size: 25px;
}
#style-h1 {
text-decoration-line: underline;
color: red;
}
</style>
</head>
<body>
<div id="style-div">
<h1 id="style-h1">ID selector</h1>
<p id="style-p">id #style-p applied</p>
<p>No id applied on this p element</p>
</div>
</body>
</html>
p, #style-h1 and #style- div are the id selectors applied on the elements:
163
a[target] {
background-color: peachpuff;
}
You can also specify the element with an attribute having a specific value.
a[href="https://www.tutorialspoint.com"] {
background-color: peachpuff;
}
<html>
<head>
<style>
a[target] {
background-color: peachpuff;
color: blueviolet;
font-size: 2em;
}
</style>
</head>
<body>
<h2>Attribute selector</h2>
<p>Styling applied to anchor element with target attribute:</p>
<a href="#">Tutorialspoint</a>
<a href="#" target="_blank">google</a>
<a href="#" target="_self">wikipedia</a>
</body>
</html>
164
a :hover {
background-color: peachpuff;
color: green;
font-size: 2em;
}
<html>
<head>
<style>
a:hover {
background-color: peachpuff;
color: green;
font-size: 2em;
}
</style>
</head>
<body>
<h2>Pseudo-class selector</h2>
<p>Styling applied to anchor element with a pseudo-class:</p>
<a href="#">Tutorialspoint</a>
</body>
</html>
a::before {
content: url();
}
165
<html>
<head>
<style>
a::before {
content: url('images/smiley.png');
}
a::after {
content: " Pseudo-element ::after applied";
color: red;
background-color: chartreuse;
}
</style>
</head>
<body>
<h2>Pseudo-element selector</h2>
<p>Styling applied to anchor element with a pseudo-element:</p>
<a href="#">Tutorialspoint</a>
</body>
</html>
Combinators
Combinator shows the relationship between the selectors. Two or more
simple selectors can be combined using a combinator, to form a
selector. You can read more about combinator here.
166
<html>
<head>
<style>
/* style applied to only div */
div {
border: 2px solid black;
width: 300px;
}
/* style applied to all li elements directly under ul */
ul li {
background-color: lightpink;
color: purple;
font-size: 1.5em;
padding: 5px;
margin-right: 15px;
}
167
<ul>
<li>Item One</li>
<li>Item Two
<ol>
<li>Nested 1</li>
<li>Nested 2</li>
</ol></li>
<li>Item Three</li>
<li>Item Four</li>
<li>Item Five
<ol>
<li>Nested 3</li>
<li>Nested 4</li>
</ol>
</li>
</ul>
</div>
</body>
</html>
As per the above syntax, the universal selector is used to apply a margin
and padding of 0 to all HTML elements.
168
<html>
<head>
<style>
* {
background-color: peachpuff;
color: darkgreen;
font-size: 25px;
}
</style>
</head>
<body>
<h1>Universal selector (*)</h1>
<div>Parent element
<p>Child paragraph 1</p>
<p>Child paragraph 2</p>
</div>
<p>Paragraph 3</p>
</body>
</html>
CSS - Inclusion
There are four ways to associate styles with your HTML document.
Most commonly used methods are inline CSS and External CSS.
169
You can put your CSS rules into an HTML document using the <style> element. This tag is placed
inside the <head>...</head> tags. Rules defined using this syntax will be applied to all the
elements available in the document. Here is the generic syntax − <!DOCTYPE html>
<html>
<head>
<style type = "text/css" media = "all">
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Embedded CSS - The <style> Element
170
screen
tty
tv
projecti
on
handhel Specifies the device the document will be
media d print displayed on. Default value is all. This is an
braille optional attribute.
aural
all
You can use style attribute of any HTML element to define style rules.
These rules will be applied to that element only. Here is the generic
syntax −
Attributes
171
style style combination of style declarations separated
rules by semicolon (;).
Example
Following is the example of inline CSS based on the above syntax − <html>
<head>
</head>
<body>
<h1 style = "color:#36C;">
This is inline CSS
</h1>
</body>
</html>
The <link> element can be used to include an external stylesheet file in your
HTML document.
An external style sheet is a separate text file with .css extension. You
define all the Style rules within this text file and then you can include this
file in any HTML document using <link> element.
172
Here is the generic syntax of including external CSS file −
<head>
<link type = "text/css" href = "..." media = "..." />
</head>
Attributes
Attributes associated with <style> elements are −
scre
en Specifies the device the document will be
media tty displayed on. Default value is all. This is
tv optional attribute.
projection
173
handh
eld
print
braille
aural
all
Example
Consider a simple style sheet file with a name mystyle.css having the following
rules −
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Now you can include this file mystyle.css in any HTML document as follows −
<head>
<link type = "text/css" href = "mystyle.css" media = " all" />
</head>
174
@import is used to import an external stylesheet in a manner similar to
the <link> element. Here is the generic syntax of @import rule.
<head>
@import "URL";
</head>
Here URL is the URL of the style sheet file having style rules. You can
use another syntax as well −
<head>
@import url("URL");
</head>
Example
Following is the example showing you how to import a style sheet file into
HTML document −
<head>
@import "mystyle.css";
</head>
175
A border, in the context of design and styling, refers to a decorative or
functional element that surrounds the content of an object, such as a text
box, image, or any other HTML element on a web page.
Borders play a vital role in the overall aesthetics and design of the webpage.
Importance of borders
177
width specifies the width of a medium
border
Value Description
none No border
178
dashes A series of short dashes
179
initial Sets the border-style property to its default
value
border-top-style
181
border-right-style
border-bottom-style
border-left-style
<html>
<head>
<style>
p {border-top-style: dotted; border-right-style: solid; border-bottom-
style: dashed; border-left-style: double;
padding: 2em;}
</style>
</head>
<body>
<h2>border-style (single-side)</h2>
<p>Different border styles on all sides.</p>
</body>
<html>
Value Description
182
thick a thick border
Declare a border-style before declaring border-width, else the border effect will
not be seen.
<html>
<head>
<style>
183
p.thin {border-width: thin;}
p.medium {border-width:
medium;} p.thick {border-width:
thick;} p.length {border-width:
100px;}
p.thin1 {border-style: double; border-width: thin;}
p.medium1 {border-style: dashed; border-width: medium;}
p.thick1 {border-style: solid; border-width: thick;} p.length1
{border-style: dotted; border-width: 10px;}
</style>
</head>
<body>
<h2>border-width without <i>border-style</i> property</h2>
<p class="thin">Thin border.</p>
<p class="medium">Medium border.</p>
<p class="thick">Thick border.</p>
<p class="length">Specific length border.</p>
<h2>border-width with <i>border-style</i> property</h2>
<p class="thin1">Thin width.</p>
<p class="medium1">Medium width.</p>
<p class="thick1">Thick width.</p>
<p class="length1">Specific length width border.</p>
</body>
184
</html>
border-top-width
border-right-width
border-bottom-width
border-left-width
Let us see an example:
<html>
<head>
<style>
p {border-style: solid;
border-top-width: thin;
border-right-width: thick;
border-bottom-width: medium;
border-left-width: 10px;
padding: 2em;}
</style>
</head>
<body>
<h2>border-width (single-side)</h2>
<p>Different border widths on all sides.</p>
</body>
</html>
185
CSS Borders - color Property
The border-color is the third constituent property of border. It sets the color of
the border.
Declare a border-style before declaring border-color, else the border effect will
not be seen.
186
<html>
<head>
<style>
p.color1 {border-color: red;}
p.hexa1 {border-color: #00ff00;}
p.color2 {border-style: dashed; border-color: red;}
p.hexa2 {border-style: solid; border-color: #00ff00;}
</style>
</head>
<body>
<h2>border-color without <i>border-style</i> property</h2>
border-top-color
border-right-color
border-bottom-color
border-left-color
187
<html>
<head>
<style>
p {border-style: solid;
border-top-color: red;
border-right-color: #0000ff;
border-bottom-color: rgb(100,123,111);
border-left-color: rgba(50,123,111,0.4);
padding: 0.5in;}
</style>
</head>
<body>
<h2>Different border color on all sides</h2>
<p>Check the border colors!!!</p>
</body>
</html>
CSS - border-block
The CSS shorthand property border-block is a logical property that
defines width, style and color of both the start and end in the block
dimension at once.
188
border-block property acts on both the start and finish in the block
dimension. The writing mode, directionality, and text orientation of
the element determine which precise physical borders are impacted.
When the writing-mode is set to the default horizontal direction,
border-block is applied to the top and bottom borders of an
element.
Conversely, a vertical writing-mode, applies border-block to the right and
left borders.
Possible values
<border-width> - The width of the border.
<border-style> - The line style of the border.
<border-color> - The color of the border.
Constituent Properties
border-block-color
border-block-style
border-block-width
Syntax
189
All the HTML elements.
CSS border-block - Basic Example
The following example demonstrates the css border-block property.
<html>
<head>
<style>
.border-demo {
width: 300px;
height: 150px;
margin: 20px;
padding: 20px;
background-color: #f0f0f0;
border-block: 5px dotted #3498db;
}
.add-demo {
font-size: 18px;
color: #111;
}
</style>
</head>
<body>
<div class="border-demo">
<p class="add-demo">This is a bordered element with padding and
background color.</p>
<p>This is an example for border-block property</p>
</div>
</body>
</html>
190
<html>
<head>
<style>
.vertical-border {
writing-mode: vertical-rl;
direction: ltr;
width: 150px;
height: 250px;
margin: 50px;
padding: 20px;
background-color: #f0f0f0;
border-block: 1rem solid red;
}
.add-style {
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
<div class="vertical-border">
<p class="add-style">This is a vertical bordered element with a solid
red border.</p>
</div>
</body>
</html>
CSS - Margins Property
This chapter discusses about the margins in CSS. Margins are used to
create space around an element.
They define the amount of space between an element's border and adjacent
elements.
Possible Values
Following values can be passed to each side:
Value Description
191
length The margin is specified in px, pt, cm, etc
selector {
margin: top right bottom left;
}
Syntax
You can specify margins for all sides at once (top, right, bottom, left) or
set specific values for individual sides.
You can set the margin for each side individually, which is as follows:
Property Description
192
margin-top sets the top margin of the element
margi
n- sets the bottom margin of the element
botto
m
193
allows you to specify how much space should appear between the
content of an element and its border.
It adds extra space inside the element, effectively increasing or
decreasing the distance between the content and the border.
has a default value of 0 (zero). So if you don’t set a padding value
then no padding should appear around the element.
Negative values are not allowed.
The order of the padding properties is important, and follows this pattern:
194
All the padding properties can have the following values:
Let us see a simple example to set padding with a single length value,
which is applied equally to all four padding sides. Here we add 5 px
195
padding on h2 element :
<!DOCTYPE html>
<html>
<head>
<title>CSS - Padding</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
196
#myDIV {border: 2px solid;}
h2 {padding: 5px; background-color: skyblue}
</style>
</head>
<body>
<div id="myDIV">
<h2>The padding can be seen around the text.</h2>
</div>
</body>
</html>
197
<!DOCTYPE html>
<html>
<head>
<title>CSS - Padding</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
#myDIV {border: 2px solid;}
h2 {padding: 2em; background-color: skyblue}
h3 {background-color: silver;}
</style>
</head>
<body>
<div id="myDIV">
<h2>The padding can be seen around the text.</h2>
</div>
<div id="myDIV">
<h3>No padding added to the h3 element. Hence the border is
nearly touching the text.</h3>
</div>
</body>
</html>
CSS provides four separate properties to set padding for top, right, bottom, and
left for an element.
padding-top
198
padding-right
padding-bottom
padding-left
Following example shows how different padding properties can be set around an
h2 element:
<!DOCTYPE html>
<html>
<head>
<title>CSS - Padding</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
h2 {padding-top: 10px; padding-right: 20px; padding-bottom:
30px; padding-left: 40px; background-color: skyblue; border: 1px
solid;}
</style>
</head>
<body>
<div>
<h2>The different padding property values are set to the h2
element.</h2>
</div>
</body>
</html>
Padding can be set to all the sides of an element at once. The value for
padding properties can be in pixels, inches, ems, or centimeters, apart
from percentage.
199
Let us see an example for the shorthand property of padding:
<!DOCTYPE html>
<html>
<head>
<title>CSS - Padding</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
200
<!DOCTYPE html>
<html>
<head>
<title>CSS - Padding</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
h2 {padding: 20px 40px 10px; background-color: silver;}
</style>
</head>
<body>
<div>
<h2>The three different padding property values are set to
the h2 element.</h2>
</div>
</body>
</html>
201
content areas of an element in the box model.
7. How do you apply styles to a specific element when it has multiple
classes? Explain with an example.
8. What are pseudo-classes and pseudo-elements in CSS, and give examples
of each.
9. Describe the difference between the display: block, display: inline-
block
display: inline, and CSS properties.
10. Explain the purpose position property in CSS and describe the values static,
of the
relative, absolute,fixed
and
202
Lesson 4: Java script
4.1. Introduction
Evolution of JavaScript
204
This version of ECMAScript was widely adopted and formed
the basis of JavaScript implementations in major web browsers.
4. ECMAScript 4 (Abandoned)
Development of ECMAScript 4 began but was eventually
abandoned due to disagreements among stakeholders regarding
the proposed features and complexity.
Instead of releasing ECMAScript 4, the ECMAScript committee
decided to focus on smaller, incremental updates.
5. ECMAScript 5 (2009)
Released in December 2009, ECMAScript 5 introduced
significant enhancements and improvements to the language.
New features included strict mode, which provided better error
handling and improved security, as well as array methods like
forEach, map, filter, and reduce.
ECMAScript 5 was widely adopted and is still supported by all modern
web browsers.
6. ECMAScript 6 (ES2015)
Released in June 2015, ECMAScript 6, also known as ES6 or
ES2015, was a major milestone in the evolution of JavaScript.
ES6 introduced many new features and syntax enhancements,
including let and const for variable declaration, arrow functions,
template literals, classes, and modules.
It also introduced new data structures like Map, Set, and symbols,
as well as promises for asynchronous programming.
205
ECMAScript 6 aimed to make JavaScript code more
readable, maintainable, and expressive.
7. Subsequent ECMAScript Versions
Following the release of ECMAScript 6, the ECMAScript
specification adopted a yearly release cycle, with new versions
introduced annually.
Subsequent versions, including ECMAScript 7 (ES2016),
ECMAScript 8 (ES2017), ECMAScript 9 (ES2018),
ECMAScript 10 (ES2019), ECMAScript 11 (ES2020), and
ECMAScript 12 (ES2021), introduced additional features, syntax
enhancements, and improvements to the language.
These updates focused on enhancing developer productivity,
improving performance, and addressing pain points in JavaScript
development.
8. Latest Version: ECMAScript 13 (ES2022)
ECMAScript 13, also known as ES2022, is the latest version of
the ECMAScript specification.
Released in June 2022, ES2022 introduces new features such as
class fields, private methods and accessors, extended numeric
separators, and more.
It continues the tradition of improving JavaScript by adding new
capabilities and refining existing features to meet the needs of
modern web development.
206
The evolution of JavaScript, guided by the ECMAScript specification,
has transformed it from a simple scripting language to a powerful,
versatile, and widely-used programming language that powers the
modern web.
Comparison:
let y; // Declaration
y = 'Hello'; // Initialization
let: Introduced in ES6, let allows block-scoped variables that can be
reassigned. e.g:
const: Also introduced in ES6, const declares variables with a
constant (unchanging) value. They must be initialized when
declared and cannot be reassigned. Example:
const PI = 3.14; // Declaration and initialization
Dynamic Typing:
JavaScript variables can hold values of any data type, and the data
type of a variable can change at runtime. Example:
var dynamicVar;
dynamicVar = 10; // Number
dynamicVar = 'Hello'; // String
dynamicVar = true; // Boolean
Naming Conventions:
209
digit).
Variable names are case-sensitive.
var myVariable;
var firstName;
var numStudents;
It's recommended to use camelCase for variable names to improve
readability. Example:
Best Practices:
210
let num = 10;
let floatNum = 3.14;
3. Undefined: Represents a variable that has been declared but not assigned a
value.
let x;
console.log(x); // Output: undefined
212
Special Data Types:
JavaScript Operators
JavaScript operators are symbols that perform operations on operands
(values or variables). They can be categorized into several types based on
their functionality. Here's a detailed discussion of JavaScript operators:
Arithmetic Operators:
213
Assignment Operators:
Comparison Operators:
Comparison operators compare two values and return a Boolean result (true or
false).
Equal (==): Checks if two operands are equal in value, but not necessarily
in type.
Strict Equal (===): Checks if two operands are equal in value and type.
Not Equal (!=): Checks if two operands are not equal in value.
214
Strict Not Equal (!==): Checks if two operands are not equal in value or
type.
Greater Than (>): Checks if the left operand is greater than the right
operand.
Less Than (<): Checks if the left operand is less than the right operand.
Greater Than or Equal To (>=): Checks if the left operand is
greater than or equal to the right operand.
Less Than or Equal To (<=): Checks if the left operand is less
than or equal to the right operand.
Logical Operators:
Unary Operators:
215
Decrement (--): Decreases the value of its operand by 1.
216
Unsigned Right Shift (>>>): Shifts the bits of the left operand to
the right by the number of positions specified by the right operand,
filling the leftmost positions with zeros.
Conditional Statements:
217
if (condition1) {
// code block
} else if (condition2) {
// code block
} else {
// code block
}
if...else if...else Statement: Executes different blocks of code for different
conditions.
switch Statement: Evaluates an expression and executes a block of code
based on matching case labels.
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
218
break;
default:
// code block
}
Loop Statements:
220
Lesson 3: Functions
Objective: To teach students how to define and use functions in JavaScript.
i. Function Declaration:
221
ii. Function Expression:
222
let greet = (name) => {
return 'Hello, ' + name + '!';
};
v. Function Parameters:
function greet(name) {
return 'Hello, ' + name + '!';
}
console.log(greet('John')); // Output: Hello, John!
Return Statement:
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // Output: 8
function greet(name) {
return 'Hello, ' + name + '!';
224
}
console.log(greet('Alice')); // Output: Hello, Alice!
function test() {
let x = 10;
console.log(x); // Output: 10
}
console.log(x); // Error: x is not defined
A recursive function is a function that calls itself, often used for tasks
that can be broken down into smaller, similar subtasks. It's crucial to
have a base case to prevent infinite recursion.
225
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5)); // Output: 120
x. Callback Function:
226
function process(callback) {
callback();
}
function greet() {
console.log('Hello!');
}
process(greet); // Output: Hello!
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // Arguments: 3, 4 | Output: 7
Global Scope:
function test() {
console.log(globalVar); // Output: I am global
}
test();
Local Scope:
Variables declared inside a function have local scope, meaning they are
accessible only within that function. They are not visible outside of the
function.
228
function test() {
let localVar = 'I am local';
console.log(localVar); // Output: I am local
}
test();
console.log(localVar); // Error: localVar is not defined
Function Scope:
JavaScript uses function scope, meaning that var within a function are
variables declared with scoped to that function.
They are not visible outside of the function.
function test() {
var localVar = 'I am function-scoped';
console.log(localVar); // Output: I am function-scoped
}
test();
console.log(localVar); // Error: localVar is not defined
229
if (true) {
let blockVar = 'I am block-scoped';
console.log(blockVar); // Output: I am block-scoped
}
Lexical Scope:
function inner() {
230
console.log(outerVar); // Output: I am outer
}
inner();
}
outer();
console.log(outerVar); // Error: outerVar is not defined
Closure:
function outer() {
let outerVar = 'I am outer';
function inner() {
console.log(outerVar); // Output: I am outer
}
return inner;
}
231
Summary Questions
232
HTML:
CSS:
234
6. Discuss the purpose of CSS preprocessors like Sass
and LESS, and provide examples of their features.
7. Explain the concept of CSS specificity and how it
determines which styles apply to an element.
8. Describe the difference between absolute and relative
positioning in CSS.
9. What are CSS frameworks, and how do they simplify the
process of styling web pages?
10. Discuss the role of media queries in responsive web
design and provide examples of their usage.
JavaScript: