Web Design
Web Design
Web Design
HTML stands for Hyper Text Markup Language, which is the most
widely used language on Web to develop web pages.
HTML is the standard markup language for creating Web pages
Hypertext refers to the way in which Web pages (HTML documents)
are linked together. Thus, the link available on a webpage is called
Hypertext.
HTML was created by Berners-Lee in late 1991 but "HTML 2.0" was
the first standard HTML specification which was published in 1995.
HTML 4.01 was a major version of HTML and it was published in late
1999. Though HTML 4.01 version is widely used but currently we are
having HTML-5 version which is an extension to HTML 4.01, and this
version was published in 2012.
Why need of HTML?
As mentioned before, HTML is one of the most widely used language over the web.
I'm going to list few of them here:
Web pages development - HTML is used to create pages which are rendered over the web.
Almost every page of web is having html tags in it to render its details in browser.
Internet Navigation - HTML provides tags which are used to navigate from one page to
another and is heavily used in internet navigation.
Responsive UI - HTML pages now-a-days works well on all platform, mobile, tabs,
desktop or laptops owing to responsive design strategy.
Offline support HTML pages once loaded can be made available offline on the machine
without any need of internet.
Game development- HTML5 has native support for rich experience and is now useful in
gaming development arena as well.
Prerequisites
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>
Output:
This is a heading
1 <!DOCTYPE...>
This tag defines the document type and HTML version.
2 <htm l>
This tag encloses the complete HTML document and mainly comprises of document header which is represented by <head>...</head> and
document body w hich is represented by <body>...</body> tags.
3 <head>
This tag represents the document's header w hich can keep other HTML tags like <title>, <link> etc.
4 <title>
The <title> tag is used inside the <head> tag to mention the document title.
The <title> element specifies a title for the HTML page (w hich is shown in the brow ser's title bar or in the page's tab)
5 <body>
This tag represents the document's body w hich keeps other HTML tags like <h1>, <div>, <p> etc.
6 <h1>
This tag represents the heading.
7 <p>
This tag represents a paragraph.
HTML Page Structure
Below is a visualization of an HTML page structure:
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>
The content inside the <body> section (the white area above) will be displayed in
a browser. The content inside the <title> element will be shown in the browser's
title bar or in the page's tab.
The <!DOCTYPE> Declaration
<!DOCTYPE html>
HTML – BASIC TAGS
Heading Tags
Any document starts with a heading. You can use different sizes for your headings. HTML also
has six levels of headings, which use the elements <h1>, <h2>, <h3>, <h4>, <h5>, and
<h6>. While displaying any heading, browser adds one line before and one line after that
heading.
Example: OUTPUT:
<!DOCTYPE html>
<html>
<head>
<title>Heading Example</title>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<h5>This is heading 5</h5>
<h6>This is heading 6</h6>
</body>
Paragraph Tag
The <p> tag offers a way to structure your text into different paragraphs. Each paragraph of text should go
in between an opening <p> and a closing </p> tag as shown below in the example:
Example
<!DOCTYPE html>
<html>
<head>
<title>Paragraph Example</title>
</head>
<body>
<p>Here is a first paragraph of text.</p>
<p>Here is a second paragraph of text.</p>
<p>Here is a third paragraph of text.</p>
</body>
</html>
Whenever you use the <br /> element, anything following it starts from the next line.
This tag is an example of an empty element
EX:
<!DOCTYPE html>
<html>
<head>
<title>Line Break Example</title>
</head>
<body>
<p>Hello</br>You delivered your assignment on
time.</br>Thanks</br> Mahnaz</p>
</body>
</html>
You can use <center> tag to put any content in the center of the page or
any table cell.
➔ Horizontal Lines
<!DOCTYPE html>
<html>
<head>
<title>Preserve Formatting Example</title>
</head>
<body> This will produce the following
<pre> result:
function testFunction( strText ){ function testFunction( strText ){
alert (strText) alert (strText)
} }
</pre>
</body>
</html>
Non breaking Spaces
Suppose you want to use the phrase "12 Angry Men." Here, you would not want a browser
to split the "12, Angry" and "Men" across two lines:
Example
<!DOCTYPE html>
<html>
<head>
<title>Nonbreaking Spaces Example</title>
</head>
<body>
<p>An example of this technique appears in the movie
"12 Angry Men."</p>
</body>
</html>
The HTML Attributes
valign top, middle, bottom Vertically aligns tags within an HTML element.
bgcolor numeric, hexidecimal, RGB values. Places a background color behind an element
id User Defined Names an element for use with Cascading Style Sheets.
class User Defined Classifies an element for use with Cascading Style Sheets.
width Numeric Value Specifies the width of tables, images, or table cells.
height Numeric Value Specifies the height of tables, images, or table cells.
<!DOCTYPE html>
<html>
<head>
<title>Align Attribute Example</title>
</head>
<body>
<p align="left">This is left aligned</p>
<p align="center">This is center aligned</p>
<p align="right">This is right aligned</p>
</body>
</html>
Bold Text
Anything that appears within <b>...</b> element, is displayed in bold as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Bold Text Example</title>
</head>
<body>
<p>The following word uses a <b>bold</b> typeface.</p>
</body>
</html>
This will produce the following result:
The content of a <tt>...</tt> element is written in monospaced font. Most of the fonts
are known as variable-width fonts because different letters are of different widths (for
example, the letter 'm' is wider than the letter 'i'). In a monospaced font, however, each
letter has the same width. TYPE WRITING EFFECT
Example
<!DOCTYPE html>
<html>
<head>
<title>Monospaced Font Example</title>
</head>
<body>
<p>The following word uses a <tt>monospaced</tt> typeface.</p>
</body>
</html>
This will produce the following result:
The following word uses a typeface.
Superscript
Text
The content of a <sup>...</sup> element is written in superscript; the font size used
is the same size as the characters surrounding it but is displayed half a character's
height above the other characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Superscript Text Example</title>
</head>
<body>
<p>The following word uses a <sup>superscript</sup> typeface.</p>
</body>
</html>
This will produce the following result:
Subscript
Text
The content of a <sub>...</sub> element is written in subscript; the font size used is
the same as the characters surrounding it, but is displayed half a character's height
beneath the other characters.
Example
<!DOCTYPE html>
<html>
<head>
<title>Subscript Text Example</title>
</head>
<body>
<p>The following word uses a <sub>subscript</sub> typeface.</p>
</body>
</html>
This will produce the following result:
Larger
Text
The content of the <big>...</big> element is displayed one font size larger
than the rest
of the text surrounding it as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Larger Text Example</title>
</head>
<body>
<p>The following word uses a <big>big</big> typeface.</p>
</body>
</html>
The content of the <small>...</small> element is displayed one font size smaller
than the rest of the text surrounding it as shown below:
Example
<!DOCTYPE html>
<html>
<head>
<title>Smaller Text Example</title>
</head>
<body>
<p>The following word uses a <small>small</small> typeface.</p>
</body>
</html>
The HTML <mark> element defines text that should be marked or highlighted:
Example
<p>Do not forget to buy <mark>milk</mark> today.</p>
HTML Quotation
The HTML <bdo> tag is used to override the current text direction:
Example
<bdo dir="rtl">This text will be written from right to left</bdo>
HTML Colors
HTML colors are specified with predefined color names, or with RGB, HEX,
HSL(hue saturation lightness), RGBA, or HSLA(hsl alpha) values.
Color Names
Tomato, Orange, DodgerBlue, MediumSeaGreen, Gray, SlateBlue, Violet,LightGray
HTML supports 140 standard color names.
RGBA Value
rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0
(fully transparent) and 1.0 (not transparent at all):
rgb(255, 99, 71)
#ff6347
Hsl(9, 100%, 64%)
rgba(255, 99, 71, 0.5)
hsla(9, 100%, 64%, 0.5)
HTML Definition Lists
⚫ The <style> tag is used to define style information (CSS) for a document.
⚫ Inside the <style> element you specify how HTML elements should render in a
browser.
⚫ Example:
⚫ <html>
<head>
<style>
h1 {color:red;}
p {color:blue;}
</style>
<style>
h1 {color:green;}
p {color:pink;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body></html>
SCROLLING TAG
Marquee speed can be changed using the "scrollmount" attribute. For example,
if you are using scrollmount = "1" then it sets the marque to scroll very slowly,
and as you increase the "scrollmount," the scrolling speed will also increase.
Images are not technically inserted into a web page; images are linked to web
pages. The <img> tag creates a holding space for the referenced image.
Note: Also, always specify the width and height of an image. If width and
height are not specified, the page might flicker while the image loads.
Tip: To link an image to another document, simply nest the <img> tag inside
an <a> tag (see example below).
Example:
<!DOCTYPE html>
<html>
<head>
<title>Example of inserting img</title>
</head>
<body>
<p>This is some text. <img
src="https://wc.wallpaperuse.com/wallp/1- 12770_s.jpg" alt="Smiley
face" style="float:left"></p>
</body>
</html>
Linking Documents
By default, the linked page will be displayed in the current browser window. To
change this, you must specify another target for the link.
</a></p>
</body>
</html>
Add link to image
Example:Add link to image
<!DOCTYPE html>
<html>
<body>
<p><a href="http://www.ppdcagra.dcmsme.gov.in/">
<img src="https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcQpzBAuvKtOgLtx5fL0Ml3lKLT
lioHTClKI3H40bIMzfBvA0IAmcHY17LE2xUN_PENyYac&usqp=CAU"
alt="/www.ppdcagra.dcmsme.gov.in" width="100" height="132">
</a></p>
</body>
</html>
Grouping Content
The <div> and <span> elements allow you to group together several
elements to create sections or subsections of a page.
For example, you might want to put all of the footnotes on a page
within a <div> element to indicate that all of the elements within that
<div> element relate to the footnotes.
You might then attach a style to this <div> element so that they appear
using a special set of style rules.
Example
<!DOCTYPE html>
<html>
<head>
<title>Div Tag Example</title>
</head>
<body>
<div id="menu" align="middle" >
<a href="/index.htm">HOME</a> |
<a href="/about/contact_us.htm">CONTACT</a> |
<a href="/about/index.htm">ABOUT</a>
</div>
<div id="content" align="left" bgcolor="white">
<h5>Content Articles</h5>
<p>Actual content goes here.....</p>
</div>
</body>
</html>
SPAN
The <span> element, on the other hand, can be used to group inline
elements only. So, if you have a part of a sentence or paragraph
which you want to group together, you could use the <span> element
as follows
<!DOCTYPE html>
<html>
<head>
<title>Span Tag Example</title>
</head>
<body>
<p>This is the example of <span style="color:green">span tag</span> and
the <span style="color:red">div tag</span> alongwith CSS</p>
</body>
</html>
HTML Tables
HTML tables allow web developers to arrange data into rows and
columns.
Table Heading is Defined by <th>
Table Cells
Everything between <td> and </td> are the content of the table
cell.
<!DOCTYPE html>
<html>
<body>
HTML tables can have cells that span over multiple rows and/or
columns.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Cell that spans two columns</h2>
<p>To make a cell span more than one column, use the colspan attribute.</p>
<table border=1 cellpadding= “2” cellwspacing=”2”>
<tr>
<th colspan="2">Name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>43</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>57</td>
</tr>
</table>
</body>
</html>
HTML – FRAMES
The HTML frameset and frame elements were used to create page layouts
in which certain content remained visible while other content was
scrollable.
An HTML form is used to collect user input. The user input is most often
sent to a server for processing.
The <form> Element
HTML
The HTML <form> element is used to create an HTML form for user input:
Syntax:
Forms
<form>
.
form elements
.
</form>
The <form> element is a container for different types of input elements, such
as: text fields, checkboxes, radio buttons, submit buttons, etc.
The HTML <form> Elements
The <label> element is useful for screen-reader users, because the screen-
reader will read out loud the label when the user focus on the input element.
The <label> element also help users who have difficulty clicking on very small
regions (such as radio buttons or checkboxes) - because when the user clicks
the text within the <label> element, it toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of the
<input> element to bind them together.
Radio Buttons
</body>
</html>
The Submit Button
The <input type="submit"> defines a button for submitting the form data to a form-handler.
The form-handler is typically a file on the server with a script for processing input data.
Calendar
Email validation
URL validation
If you want to display a range control you can use type as range.
Time input.
<!DOCTYPE html>
<html>
<body> Note:
<h2>The select Element</h2> The <option>
<p>The select element defines a drop-down list:</p> elements defines
<form action="/action_page.php"> an option that can
<label for="cars">Choose a car:</label> be selected.
<select id="cars" name="cars">
<option value="volvo">Volvo</option> By default, the
<option value="saab">Saab</option> first item in the
<option value="fiat">Fiat</option> drop-down list is
<option value="audi">Audi</option> selected.
</select>
<input type="submit">
</form>
<select id="cars" name="cars" size="3">
</body>
</html>
The <textarea> Element
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Textarea</h2>
<p>The textarea element defines a multi-line input field.</p>
<form>
<textarea name="message" rows="10" cols="30">The cat was playing in the garden.</textarea>
<br><br>
<input type="submit"> Note: style="width:200px;
</form>
</body> height:600px;
</html>
The rows attribute specifies the visible number of lines in a text area.
</body>
</html>
HTML <fieldset> element:
Example:
<form>
<fieldset>
<legend>User Information:</legend>
<label for="name">Enter name</label><br>
<input type="text" id="name" name="name"><br>
<label for="pass">Enter Password</label><br>
<input type="Password" id="pass" name="pass"><br>
<input type="submit" value="submit">
</fieldset>
</form>
Form Attribute
Example of Action & required attribute:
<!DOCTYPE html>
<html>
<body>
<h2>Demo of action attribute of form element</h2>
<form action="action.html" method="post">
<label>User Name:</label><br>
<input type="text" name="name"><br><br>
<label>User Password</label><br>
<input type="password" name="pass"><br><br>
<label>Account holder name</label><br>
<input type="text" name="uname" size="40" required><br><br>
<input type="submit">
</form>
<p><b>It will redirect to a new page "action.html" when you click on submit
button</b></p>
</body>
</html>
⚫ Input Type Color
⚫ <input type="color" id="favcolor" name="favcolor">
⚫ Input Type Image
⚫ <input type="image" src="img_submit.gif" alt="Submit" width="48"
height="48">
⚫ The input multiple attribute specifies that the user is allowed to enter more
than one value in an input field.The multiple attribute works with the
following input types: email, and file.
⚫ <label for="files">Select files:</label>
⚫ <input type="file" id="files" name="files" multiple>
⚫ The placeholder Attribute
The short hint is displayed in the input field before the user enters a value.
The placeholder attribute works with the following input types: text, search,
url, tel, email, and password.
<input type="tel" id="phone" name="phone"
placeholder="9980780990"
The list Attribute
The input list attribute refers to a <datalist> element that contains
pre- defined options for an <input> element.
<form action="/action_page.php">
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" value="Submit">
</form>
HTML iframes
An HTML iframe embeds another document within the current HTML document
in the rectangular region.
The webpage content and iframe contents can interact with each other using
JavaScript.
Iframe Syntax
<iframe src="URL"></iframe>
BASICS OF CSS
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.
p{
color: red;
text-align: Center;
}
Example Explained
p is a selector in CSS (it points to the HTML element you want to style: <p>).
color is a property, and red is the property value
text-align is a property, and Center is the property value
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>
</body>
</html>
The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class
name.
Example
In this example all HTML elements with class="center" will be red and center-aligned:
.center {
text-align: center; (All are inside the <style>.......</style>)
color: red;
}
You can also specify that only specific HTML elements should be affected by a class.
Example
In this example only <p> elements with class="center" will be red and center-aligned:
p.center {
text-align: center;
color: red;
}
HTML elements can also refer to more than one class.
The <p> element will be styled according to class="center" and to class="large"
Note: it won’t affect other element eg <h1>
The CSS id Selector
To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
Example
The CSS rule below will be applied to the HTML element with id="para1":
#para1 {
text-align: center;
color: red;
}
Three Ways to Insert CSS
⚫ External CSS
⚫ Internal CSS / Embedded CSS
⚫ Inline CSS
External CSS
The <link> element can be used to include an external stylesheet file in your
HTML document.
Each HTML page must include a reference to the external style sheet file inside
the <link> element, inside the head section.
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.
</body>
</html>
Embedded CSS/ Internal CSS
The internal style is defined inside the <style> element, inside the head section.
The <style> Element -
<head>
<st yle t ype = "text/css" >
body {background-color: linen; }
</style>
</head>
Example:
<!DOCTYPE html>
<html>
<head>
<style>
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>
Inline CSS
Example
Inline styles are defined within the "style" attribute of the relevant element:
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
Imported CSS - @import Rule
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">
Lorem ipsum dolor sit amet
</p>
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:#867890;">Lorem ipsum...</p>
<h1 style="background-color:DodgerBlue;color:Tomato;">Hello World</h1>
<h1 style="border:2px solid Tomato;">Hello World</h1>
</body>
</html>
CSS background-image
Property
Example
Use the shorthand property to set the background properties in one declaration:
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
CSS background-image Property
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcRnIX_WtVC8SLDNymWVIVzhV1CwH6-NNvXJ7y- zNpUK1A5s7y-
DCyf2jZUOs1uB3- y1k44&usqp=CAU");
}
#example1 {
border: 2px solid black;
padding: 25px;
background: url("https://img.freepik.com/free-vector/hand-painted-watercolor-pastel-sky-background_23-
2148902771.jpg?w=2000");
background-size: auto;
}
</style>
</head>
<body>
<h1 style="background-color:white">The background-image Property</h1>
<div id="example1">
<h2>Hello World</h2>
</div>
</body>
</html>
Set the Font Family
The font-weight property is used to increase or decrease how bold or light a font appears.
The font property is used as shorthand to specify a number of other font properties.
<!DOCTYPE html>
<html> <h2>The border-style Property</h2>
<head><style> <p>This property specifies what kind of
p.dotted {border-style: dotted;} border to display:</p>
p.dashed {border-style: <p class="dotted">A dotted border.</p>
dashed;} <p class="dashed">A dashed border.</p>
p.solid {border-style: solid;} <p class="solid">A solid border.</p>
p.double {border-style: double;} <p class="double">A double border.</p>
p.groove {border-style: groove;} <p class="groove">A groove border.</p>
p.ridge {border-style: ridge;} <p class="ridge">A ridge border.</p>
p.inset {border-style: inset;} <p class="inset">An inset border.</p>
p.outset {border-style: outset;} <p class="outset">An outset border.</p>
p.none {border-style: none;} <p class="none">No border.</p>
p.hidden {border-style: hidden;} <p class="hidden">A hidden border.</p>
p.mix {border-style: dotted <p class="mix">A mixed border.</p>
dashed solid double;} </body>
</style> </html>
CSS Rounded Corners
The letter-spacing property is used to add or subtract space between the letters that make up a word.
The word-spacing property is used to add or subtract space between the words of a sentence.
The text-transformproperty is used to capitalize text or convert text to uppercase or lowercase letters.
The white-space property is used to control the flow and formatting of text.
The text-shadow property is used to set the text shadow around a text.
CSS Text Alignment
The text-align property is used to set the horizontal alignment of a
text.
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
P
{
text-align: justify;
}
Text Transformation
The text-transform property is used to specify uppercase and lowercase
letters in a text.
<html> <body>
<head>
<style> <h1>Using the text-transform
p.uppercase { property</h1>
text-transform: uppercase;
} <p class="uppercase">This text is
p.lowercase { transformed to uppercase.</p>
text-transform: lowercase; <p class="lowercase">This text is
} transformed to lowercase.</p>
p.capitalize { <p class="capitalize">This text is
text-transform: capitalize; capitalized.</p>
}
</style> </body>
</head> </html
CSS Text Spacing
CSS Text Indentation, Letter Spacing, Line Height, Word Spacing, and
White Space
⚫ text-indent Eg:
⚫ letter-spacing text-indent: 50px;
⚫ line-height letter-spacing: 5px;
⚫ word-spacing line-height: 1.8;
⚫ white-space word-spacing: 10px;
CSS Text Shadow
<!DOCTYPE html>
<html>
<head>
<style> *specify the horizontal shadow (2px)
h1 { *The vertical shadow (2px)
text-shadow: 0 0 3px #FF0000, 0 0 5px #0000FF; *color (red) to the shadow
}
</style> *blur effect (5px) to the shadow
</head>
<body>
<h1>Text-shadow with red and blue neon
glow!</h1>
</body>
</html>
CSS Links
The four links states are:
⚫ a:link - a normal, unvisited link
<span style="cursor:auto">auto(or)text</span><br>
<span style="cursor:crosshair">crosshair</span><br>
<span style="cursor:default">default</span><br>
<span style="cursor:help">help</span><br>
<span style="cursor:move">move</span><br>
<span style="cursor:pointer">pointer</span><br>
<span style="cursor:progress">progress</span><br>
CSS Lists
The list-style-type Property
Eg:text-align: center
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the
content in <th> or <td>.
Eg:vertical-align: bottom;
CSS Table Style
Table Padding
To control the space between the border and the content in a table, use the padding property
on <td> and <th> elements:
Example
th, td {
padding: 15px;
text-align: left;
Horizontal Dividers
Add the border-bottom property to <th> and <td> for horizontal dividers:
Example
th, td {
border-bottom: 1px solid #ddd;
}
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
Example
tr:hover {background-color: coral;}
Striped Tables
For zebra-striped tables, use the nth-child() selector and add a background-
color to all even (or odd) table rows:
Example
tr:nth-child(even) {background-color: #f2f2f2;}
Table Color
The example below specifies the background color and text color of <th>
elements:
Example
th {
background-color: #04AA6D;
color: white;
}
CSS Layout – Overflow
The overflow property specifies whether to clip the content or to add scrollbars when
the content of an element is too big to fit in the specified area.
visible - Default. The overflow is not clipped. The content renders outside the
element's box
Hidden - The overflow is clipped, and the rest of the content will be invisible
Scroll - The overflow is clipped, and a scrollbar is added to see the rest of the
content
auto - Similar to scroll, but it adds scrollbars only when necessary
Example::
<html>
<head>
<style>
div {
background-color: coral;
width: 200px;
height: 65px;
border: 1px solid;
overflow: visible;
}
</style>
</head>
<body>
<h2>Overflow: visible</h2>
<p>By default, the overflow is visible, meaning that it is not clipped and it renders outside the element's
box:</p>
<div>You can use the overflow property when you want to have better control of the layout. The overflow
property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow-x and overflow-y
The overflow-x and overflow-y properties specifies whether to change the overflow of content
just horizontally or vertically (or both):
Eg:
<style>
div {
background-color: coral;
width: 200px;
height: 65px;
border: 1px solid black;
overflow-x: hidden;
overflow-y: scroll;
}
</style>
CSS Layout - float and clear
The CSS clear property specifies what elements can float beside the cleared element and on
which side.
The float Property
The float property is used for positioning and formatting content e.g. let an image float left to the
text in a container.
⚫ In its simplest use, the float property can be used to wrap text around images.
Example - float: right;
The following example specifies that an image should float to the right in a text:
<!DOCTYPE html>
<html>
<head>
<style>
img {
float: left;
}
</style>
</head>
<body>
<h2>Float Left</h2>
<p>In this example, the image will float to the left in the paragraph, and the text in the paragraph will wrap around the image.</p>
</body>
</html>
Float Next To Each Other
Normally div elements will be displayed on top of each other. However, if we use float: left
we can let elements float next to each other:
Example:
<!DOCTYPE html>
<html> <body>
<head>
<style> <h2>Float Next To Each Other</h2>
div {
float: left; <p>In this example, the three divs will float
padding: 15px; next to each other.</p>
}
.div1 { <div class="div1">Div 1</div>
background: red; <div class="div2">Div 2</div>
} <div class="div3">Div 3</div>
.div2 {
background: yellow; </body>
} </html>
.div3 {
background: green;
}
</style>
</head>
CSS Layout - clear and clearfix
The clear property specifies what should happen with the element that is next to
a floating element.
none - The element is not pushed below left or right floated elements. This is
default
left - The element is pushed below left floated elements
right - The element is pushed below right floated elements
both - The element is pushed below both left and right floated elements
inherit - The element inherits the clear value from its parent
The clearfix Hack
If a floated element is taller than the containing element, it will "overflow"
outside of its container. We can then add a clear fix hack to solve this problem:
Example: <body>
<style> <h2>Without Clearfix</h2>
div { <p>This image is floated to the right. It is also taller than the element
border: 3px solid containing it, so it overflows outside of its container:</p>
<div>
#4CAF50;
<img class="img1" src="pineapple.jpg" alt="Pineapple" width="170"
padding: 5px; height="170">
} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus
.img1 { imperdiet...
float: right; </div>
} <h2 style="clear:right">With Clearfix</h2>
<p>We can fix this by adding a clearfix class with overflow: auto; to the
.img2 {
containing element:</p>
float: right; <div class="clearfix">
} <img class="img2" src="pineapple.jpg" alt="Pineapple" width="170"
.clearfix { height="170">
overflow: auto; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus
} imperdiet...
</div>
</style> </body>
</html>
<!DOCTYPE html>
<html>
<head>
<style> <body>
.div1 {
float: left; <h2>Without clear</h2>
padding: 10px; <div class="div1">div1</div>
border: 3px solid #73AD21; <div class="div2">div2 - Notice that div2 is after div1 in the HTML
} code. However, since div1 floats to the left, the text in div2 flows
.div2 { around div1.</div>
padding: 10px; <br><br>
border: 3px solid red;
} <h2>With clear</h2>
.div3 { <div class="div3">div3</div>
float: left; <div class="div4">div4 - Here, clear: left; moves div4 down below the
padding: 10px; floating div3. The value "left" clears elements floated to the left. You
border: 3px solid #73AD21; can also clear "right" and "both".</div>
}
.div4 { </body>
padding: 10px; </html>
border: 3px solid red;
clear: left;
}
</style>
</head>
CSS Layout - The display Property
The display property is the most important CSS property for controlling layout.
<div>
<h1> - <h6>
<p>
<form>
<header>
<footer>
<section>
Inline Elements
An inline element does not start on a new line and only takes up as much width as necessary.
This is an inline <span> element inside a paragraph.
Position Property
The position property specifies the type of positioning method used for an element.
There are five different position values:
⚫ static
⚫ relative
⚫ fixed
⚫ absolute
⚫ sticky
Elements are then positioned using the top, bottom, left, and right properties. However, these
properties will not work unless the position property is set first. They also work differently depending
on the position value.
1. position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right
properties.
An element with position: static; is not positioned in any special way; it is always
positioned according to the normal flow of the page:
CSS Fixed Positioning
The fixed positioning property helps to put the text fixed on the browser. This
fixed test is positioned relative to the browser window, and doesn't move even you
scroll the window.
Example:
<!DOCTYPE html> <html>
<head>
<style>
p.pos_fixed {
position: fixed;
top: 50px;
right: 5px;
color: blue;
}
</style>
</head>
<body>
<p>Some text...</p><p>Some text...</p><p>Some text...</p><p>........</p><p>.... ...</p
><p>........</p><p>........</p><p>........</p><p>........</p>
<p>........ </p>
<p class="pos_fixed">This is the fix positioned text.</p>
</body>
</html>
CSS Relative Positioning
The relative positioning property is used to set the element relative to its normal position.
Eg:
<!DOCTYPE html>
<html>
<head>
<style>
h2.pos_left {
position: relative;
left: -10px; }
h2.pos_right {
position: relative;
left: 30px; }
</style>
</head>
<body>
<h2>This is a heading with no position</h2>
<h2 class="pos_left">This heading is positioned left according to its normal position</h2>
<h2 class="pos_right">This heading is positioned right according to its normal position</h2>
<p>The style "left:-30px" subtracts 30 pixels from the element's original left position.</p>
<p>The style "left:30px" adds 30 pixels to the element's original left position.</p>
</body>
</html>
CSS Absolute Positioning:
The absolute positioning is used to position an element relative to the first parent element that
has a position other than static. If no such element is found, the containing block is HTML.
With the absolute positioning, you can place an element anywhere on a page.
<!DOCTYPE html>
<html>
<head>
<style>
h2 {
position: absolute;
left: 150px;
top: 250px;
}
</style>
</head>
<body>
<h2>This heading has an absolute position</h2>
<p> The heading below is placed 150px from the left and 250px from the top of the page.</p>
</body>
</html>
position: sticky;
An element with position: sticky; is positioned based on the user's scroll position.A sticky element
toggles between relative and fixed, depending on the scroll position.
Note: Internet Explorer does not support sticky positioning. Safari requires a -webkit- prefix (see example below).
<html>
<head>
<style>
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
padding: 5px;
background-color: #cae8ca;
border: 2px solid #4CAF50;
}
</style>
</head>
<body>
<p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>
<div class="sticky">I am sticky!</div>
<div style="padding-bottom:2000px">
<p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll position.</p>
</div>
</body>
</html>
Combined Example
<html>
<head> div.fixed { <body>
<style> position: fixed; <h2>position: relative;</h2>
div.sticky { bottom: 0; <div class="sticky">I am sticky!</div>
right: 0; <p>An element with position: relative; is
position: -webkit-sticky; positioned relative to its normal
width: 300px;
position: sticky; border: 3px solid #73AD21; position:</p>
top: 0; <div class="static">
} This div element has position: static;
padding: 5px; div.relative1 { </div></br></br></br>
background-color: #cae8ca; position: relative; <div class="relative">
border: 2px solid #4CAF50; width: 400px; This div element has position: relative;
} height: 200px; </div></br></br></br></br></br></br>
<div class="fixed">
div.relative { border: 3px solid #73AD21; This div element has position: fixed;
position: relative; } </div></br></br></br>
left:100px; div.absolute { <div class="relative1">This div element
border: 3px solid #73AD21; position: absolute; has position: relative;
top: 80px; <div class="absolute">This div element
} has position: absolute;</div>
div.static { right: 0;
</div>
position: static; width: 200px;
<div style="padding-bottom:2000px">
height: 100px; </div>
border: 3px solid #73AD21;
border: 3px solid #73AD21; </body>
} }</style></head> </html>
Example for sentence position inside the Image
<!DOCTYPE html>
<html>
<head>
<style> .bottomleft {
.container { position: </head>
position: relative; absolute; <body>
} bottom: 8px; <div class="container">
.topleft { left: 16px; <img src="1.jpg" alt="Cinque Terre" width="1000"
position: absolute; font-size: 18px; height="300">
top: 8px; } <div class="topleft">Top Left</div>
.topright { <div class="center">Centered</div>
left: 16px;
position: absolute; <div class="bottomright">Bottom Right</div>
font-size: 18px;
top: 8px; <div class="bottomleft">Bottom Left</div>
} <div class="topright">Top Right</div>
right: 16px;
.center { font-size: 18px; </div>
position: absolute; } </body>
top: 50%; img { </html>
width: 100%; width: 100%;
text-align: center; height: auto;
font-size: 18px; opacity: 0.3;
} }
.bottomright { </style>
position: absolute;
bottom: 0px;
right: 0px;
CSS Opacity / Transparency
The opacity property can take a value from 0.0 - 1.0. The lower the value, the more transparent
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
}
img:hover {
opacity: 1.0;
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property is often used together with the :hover selector to change the opacity on mouse-
over:</p>
<img src="img_forest.jpg" alt="Forest" width="170" height="100">
<img src="img_mountains.jpg" alt="Mountains" width="170" height="100">
<img src="img_5terre.jpg" alt="Italy" width="170" height="100">
</body>
</html>
<html>
<head>
<style>
div.background { <body>
background: url(klematis.jpg)
repeat; <div class="background">
border: 2px solid black; <div class="transbox">
} <p>This is some text that is placed in the
transparent box.</p>
div.transbox { </div>
margin: 30px; </div>
background-color: #ffffff;
border: 1px solid black; </body>
opacity: 0.6; </html>
}
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
</style>
CSS Navigation Bar
In our examples we will build the navigation bar from a standard HTML list.
Example explained:
display: block; - Displaying the links as block elements makes the whole link area
clickable (not just the text), and it allows us to specify the width (and padding, margin,
height, etc. if you want)
EXAMPLE OF BASIC VERTICAL NAVIGATION BAR
<!DOCTYPE html>
<html> /* Change the link color on hover */
<head> li a:hover {
<style> background-color: #555;
ul { color: white;}
list-style-type: none; </style>
margin: 0; </head>
padding: 0; <body>
width: 200px; <h2>Vertical Navigation Bar</h2>
background-color: #f1f1f1; <ul>
} <li><a href="#home">Home</a></li>
li a { <li><a href="#news">News</a></li>
display: block; <li><a
color: #000; href="#contact">Contact</a></li>
padding: 8px 16px; <li><a href="#about">About</a></li>
text-decoration: none; </ul>
} </body>
</html>
<!DOCTYPE html>
<html> li a:hover:not(.active) {
<head> background-color: #555;
<style> color: white;
ul { }
list-style-type: none; </style></head>
margin: 0; <body>
padding: 0; <h2>Vertical Navigation Bar</h2>
width: 200px; <p>In this example, we create an "active" class
background-color: #f1f1f1;} with a green background color and a white text.
li a { The class is added to the "Home" link.</p>
display: block; <ul>
color: #000; <li><a class="active"
padding: 8px 16px; href="#home">Home</a></li>
text-decoration: none; <li><a href="#news">News</a></li>
} <li><a href="#contact">Contact</a></li>
li a.active { <li><a href="#about">About</a></li>
background-color: #04AA6D; </ul>
color: white;} </body>
</html>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
height: 100%; /* Full height */
position: fixed; /* Make it stick, even on scroll */
overflow: auto; /* Enable scrolling if the sidenav has too much content */}
<div style="margin-left:25%;padding:1px 16px;height:1000px;">
<h2>Fixed Full-height Side Nav</h2>
<h3>Try to scroll this area, and see how the sidenav sticks to the page</h3>
<p>Notice that this div element has a left margin of 25%. This is because the side navigation is
set to 25% width. If you remove the margin, the sidenav will overlay/sit on top of this div.</p>
<p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when the
sidenav is too long (for example if it has over 50 links inside of it).</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
</div>
Horizontal Navigation Bar Examples
Create a basic horizontal navigation bar with a dark background color and change the
background color of the links when the user moves the mouse over them
li a {
<!DOCTYPE html> display: block;
<html> color: white;
<head> text-align: center;
<style> padding: 14px 16px;
ul { text-decoration: none;}
list-style-type: none; li a:hover {background-color: #111;}
margin: 0; </style></head>
padding: 0; <body>
overflow: hidden; <ul>
<li><a class="active" href="#home">Home</a></li>
background-color: #333;
<li><a href="#news">News</a></li>
}
<li><a href="#contact">Contact</a></li>
li { <li><a href="#about">About</a></li>
float: left; </ul>
} </body>
</html>
CSS Dropdowns
Create a dropdown menu that allows the user to choose an option from a list
HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button>
element.
Use a container element (like <div>) to create the dropdown content and add whatever you
want inside of it.
Wrap a <div> element around the elements to position the dropdown content correctly with
CSS.
CSS) The .dropdown class uses position:relative, which is needed when we want the
dropdown content to be placed right below the dropdown button (using position:absolute).
The .dropdown-content class holds the actual dropdown content. It is hidden by default,
and will be displayed on hover (see below). Note the min-width is set to 160px. Feel free to
change this. Tip: If you want the width of the dropdown content to be as wide as the
dropdown button, set the width to 100% (and overflow:auto to enable scroll on small
screens).
Instead of using a border, we have used the CSS box-shadow property to make the
dropdown menu look like a "card".
The :hover selector is used to show the dropdown menu when the user moves the mouse
over the dropdown button.
CSS Dropdowns
<html> .dropdown-content a:hover {background-color: #f1f1f1}
<head><style> .dropdown:hover .dropdown-content {
.dropbtn { display: block;
background-color: #4CAF50; }
color: white; .dropdown:hover .dropbtn {
padding: 16px; background-color: #3e8e41;
font-size: 16px; }
border: none; </style>
cursor: pointer;} </head>
.dropdown { <body>
position: relative; <h2>Dropdown Menu</h2>
display: inline-block;} <p>Move the mouse over the button to open the dropdown
.dropdown-content { menu.</p>
display: none; <div class="dropdown">
position: absolute; <button class="dropbtn">Dropdown</button>
background-color: #f9f9f9; <div class="dropdown-content">
min-width: 160px; <a href="#">Link 1</a>
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); <a href="#">Link 2</a>
z-index: 1;} <a href="#">Link 3</a>
.dropdown-content a { </div>
color: black; </div>
padding: 12px 16px; <p><strong>Note:</strong> We use href="#" for test links. In a real
text-decoration: none; web site this would be URLs.</p>
display: block;} </body>
</html>
Dropdown Image
How to add an image and other content inside the dropdown box
<html>
<head> <body>
<style> <h2>Dropdown Image</h2>
.dropdown { <p>Move the mouse over the image below to
position: relative; open the dropdown content.</p>
display: inline-block; <div class="dropdown">
} <img src="1.jpg" alt="Cinque Terre"
.dropdown-content { width="100" height="50">
display: none; <div class="dropdown-content">
position: absolute; <img src="1.jpg" alt="Cinque Terre"
background-color: #f9f9f9; width="300" height="200">
min-width: 160px;
<div class="desc">Beautiful Cinque
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
Terre</div>
} </div>
.dropdown:hover .dropdown-content { </div>
display: block;} </body>
.desc { </html>
padding: 15px;text-align: center;
}</style></head>
CSS Image Gallery
The example above applies to all <input> elements. If you only want to style a specific input type,
you can use attribute selectors:
Syntax
The syntax of pseudo-elements:
selector::pseudo-element {
property: value;
}
The ::first-line Pseudo-element
The ::first-line pseudo-element is used to add a special style to the first line of a text.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
color: #ff0000;
font-size: xx-large;}
p::first-line {
color: #0000ff;
font-variant: small-caps;}
</style>
</head>
<body>
<p>You can combine the ::first-letter and ::first-line pseudo-elements to add a special effect to the first letter and
the first line of a text!</p>
</body>
</html>
The following properties The following properties apply to
apply to the ::first-line the ::first-letter pseudo- element:
pseudo-element:
⚫ font properties
⚫ font properties ⚫ color properties
⚫ color properties ⚫ background properties
⚫ background properties ⚫ margin properties
⚫ word-spacing ⚫ padding properties
⚫ letter-spacing ⚫ border properties
⚫ text-decoration ⚫ text-decoration
⚫ vertical-align ⚫ vertical-align (only if "float" is
⚫ text-transform "none")
⚫ line-height ⚫ text-transform
⚫ clear ⚫ line-height
⚫ float
⚫ clear
All CSS Pseudo Elements
CSS counters are "variables" maintained by CSS whose values can be incremented by
CSS rules (to track how many times they are used). Counters let you adjust the
appearance of content based on its placement in the document.
Automatic Numbering With Counters
CSS counters are like "variables". The variable values can be incremented by CSS
rules (which will track how many times they are used).
<!DOCTYPE html>
<html lang="en">
<head> /* Change color on hover */
<title>CSS Website Layout</title> .topnav a:hover {
<meta charset="utf-8"> background-color: #ddd;
<meta name="viewport" content="width=device-width, initial- color: black;
scale=1">
}
<style>
*{ </style>
box-sizing: border-box;} </head>
body { <body>
margin: 0;} <div class="header">
/* Style the top navigation bar */ <h1>Header</h1>
.topnav { </div>
overflow: hidden;
<div class="topnav">
background-color: #333;
} <a href="#">Link</a>
/* Style the topnav links */ <a href="#">Link</a>
.topnav a { <a href="#">Link</a>
float: left; </div>
display: block; </body>
color: #f2f2f2;
</html>
text-align: center;
padding: 14px 16px;
Content
The layout in this section, often depends on the target users. The most common
layout is one (or combining them) of the following:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Website Layout</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
*{
box-sizing: border-box;
}
body {
margin: 0;
}
/* Style the footer */
.footer {
background-color: #f1f1f1;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<div class="footer">
<p>Footer</p>
</div>
Eg:Website
CSS Transitions
CSS transitions allows you to change property values smoothly, over a given
duration.
In this chapter you will learn about the following properties:
⚫ transition
⚫ transition-delay
⚫ transition-duration
⚫ transition-property
⚫ Transition-timing-function
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s, height 2s, transform 2s;
}
div:hover {
width: 300px;
height: 300px;
transform: rotate(180deg);
}
</style>
</head>
<body>
<h1>Transition + Transform</h1>
<p>Hover over the div element below:</p>
<div></div>
</body>
CSS Animations
⚫ animation-name
⚫ animation-duration
⚫ animation-delay
⚫ animation-iteration-count
⚫ animation-direction
⚫ animation-timing-function
⚫ animation-fill-mode
⚫ animation
What are CSS Animations?
Keyframes hold what styles the element will have at certain times.
When you specify CSS styles inside the @keyframes rule, the animation will
gradually change from the current style to the new style at certain times.
CSS animations do not affect an element before the first keyframe is played or after the
last keyframe is played. The animation-fill-mode property can override this behavior.
The animation-fill-mode property specifies a style for the target element when the
animation is not playing (before it starts, after it ends, or both).
none - Default value. Animation will not apply any styles to the element before or after it
is executing
forwards - The element will retain the style values that is set by the last keyframe
(depends on animation-direction and animation-iteration-count)
backwards - The element will get the style values that is set by the first keyframe
(depends on animation-direction), and retain this during the animation-delay period
both - The animation will follow the rules for both forwards and backwards, extending
the animation properties in both directions
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation-name: example;
animation-duration: 3s;
animation-delay: 2s;
animation-fill-mode: both;
}
@keyframes example {
from {top: 0px; background-color: yellow;}
to {top: 200px; background-color: blue;}
}
</style>
</head>
<body>
<h1>CSS Animation</h1>
<p>Let the div element get the style values set by the first keyframe before the animation starts, and retain the
style values from the last keyframe when the animation ends:</p>
<div></div>
</body>
Animation Shorthand Property
The example below uses six of the animation properties:
Example
div {
animation-name: example;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
}
The same animation effect as above can be achieved by using the shorthand animation
property:
Example
div {
animation: example 5s linear 2s infinite alternate;
}
CSS Box Sizing
The CSS box-sizing property allows us to include the padding and border in
an element's total width and height.
This means: When you set the width/height of an element, the element often
appears bigger than you have set (because the element's border and
padding are added to the element's specified width/height).
Without the CSS box-sizing Property With the CSS box-sizing Property
<!DOCTYPE html>
<html> The box-sizing property allows us to
<head> include the padding and border in an element's
<style>
.div1 { total width and height.
width: 300px;
height: 100px;
border: 1px solid blue; If you set box-sizing: border-box; on an element,
} padding and border are included in the width and
.div2 { height:
width: 300px;
height: 100px;
padding: 50px; Syntax:
border: 1px solid red; box-sizing: border-box;
}
</style>
</head>
<body>
<h1>Without box-sizing</h1>
<div class="div1">This div is smaller (width is 300px and height
is 100px).</div>
<br>
<div class="div2">This div is bigger (width is also 300px and
height is 100px).</div>
</body>
</html>
CSS3 Introduced Media Queries
Tip: You can also add the cursor property with a value of "not-allowed", which will display
a "no parking sign" when you mouse over the button:
Example:
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
Button on Image
<!DOCTYPE html>
<html>
<head> .container .btn:hover {
<style> background-color: black;
.container { color: white;
position: relative; }
width: 100%; </style>
max-width: 400px;} </head>
.container img { <body>
width: 100%; <h2>Button on Image</h2>
height: auto;} <p>Add a button on an image:</p>
.container .btn {
position: absolute;
<div class="container">
top: 50%;
<img src="1.jpg" alt="Snow"
left: 50%;
background-color: #f1f1f1; style="width:100%">jgjhghgfghghfghgfh
color: black; <button class="btn">Button</button>
font-size: 16px; </div>
padding: 16px 30px;
border: none; </body>
cursor: pointer; </html>
border-radius: 5px;
Animated Buttons
Add an arrow on hover:
.button span {
.button { cursor: pointer;
<body>
display: inline-block; display: inline-block;
border-radius: 4px; position: relative; <h2>Animated
background-color: transition: 0.5s; Button</h2>
}
#f4511e;
border: none; .button span:after {
<button
color: #FFFFFF; content: '\00bb'; class="button">
text-align: center; position: absolute; <span>Hover </span>
font-size: 28px; opacity:0; </button>
transition: 0.5s;
padding: 20px;
}
width: 200px; </body>
transition: all 0.5s; .button:hover span { </html>
cursor: pointer; padding-right: 25px;
margin: 5px; }
.button:hover span:after {
}
opacity:1;
}
Animated Button-"ripple" effect on click:
<!DOCTYPE html>
<html> position: absolute;
<head> padding-top: 300%;
<style> padding-left: 350%;
.button { margin-left: -20px !important;
position: relative; margin-top: -120%;
background-color: #4CAF50; opacity: 0;
border: none; transition: 0.8s
font-size: 28px; }
color: #FFFFFF; .button:active:after {
padding: 20px; padding: 0;
width: 200px; margin: 0;
text-align: center; opacity: 1;
transition-duration: 0.4s; transition: 0s
text-decoration: none; }
overflow: hidden; </style>
cursor: pointer; </head>
} <body>
.button:after { <h2>Animated Button - Ripple Effect</h2>
content: ""; <button class="button">Click Me</button>
background: #f1f1f1; </body>
display: block; </html>
Animated Button - "Pressed Effect"
<!DOCTYPE html>
<html>
<head>
<style>
.button {
display: inline-block;
padding: 15px 25px;
font-size: 24px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none; color: #fff;
background-color: #4CAF50;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;}
.button:hover {background-color: #3e8e41}
.button:active {
background-color: #3e8e41;
box-shadow: 0 5px #666;
transform: translate(80px);}
</style></head><body>
<h2>Animated Button - "Pressed Effect"</h2>
JAVA SCRIPTS
Object
Any entity that has state and behavior is known as
an object
Example: A dog is an object because it
has states like color, name, breed, etc. as well as
behaviors like wagging the tail, barking, eating, etc.
Class
Collection of objects is called class. It is a logical
entity.
Inheritance
When one object acquires all the properties and behaviors of a parent object, it
is known as inheritance. It provides code reusability.
Polymorphism
If one task is performed in different ways, it is known as polymorphism.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For
example phone call, we don't know the internal processing.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped with different medicines.
Network-centric applications are applications that send data to
the server and are interconnected by a communications network.
Eq:FB
(The network-centric content model allows a user to access data from any
device connected to the Internet. )
Why Study JavaScript?
JavaScript is one of the 3 languages all web developers must
learn:
Client side validation - This is really important to verify any user input before
submitting it to the server and Javascript plays an important role in validting
those inputs at front -end itself.
Manipulating HTML Pages - Javascript helps in manipulating HTML page on the
fly. This helps in adding and deleting any HTML tag very easily using javascript
and modify your HTML to change its look and feel based on different devices
and requirements.
User Notifications - You can use Javascript to raise dynamic pop-ups on the
webpages to give different types of notifications to your website visitors.
Back-end Data Loading - Javascript provides Ajax library which helps in
loading back-end data while you are doing some other processing. This really
gives an amazing experience to your website visitors.
Presentations - JavaScript also provides the facility of creating presentations
which gives website look and feel. JavaScript provides RevealJS and
BespokeJS libraries to build a web-based slide presentations.
Server Applications - Node JS is built on Chrome's Javascript runtime for
building fast and scalable network applications. This is an event based library
Features of JavaScript
To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
Example
<script src="myScript.js"></script>
You can place an external script reference in <head> or <body> as you like.
The script will behave as if it was located exactly where the <script> tag is
located.
function sayHello() {
alert("Hello there"); }
</script>
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
If the function was invoked from a statement, JavaScript will "return" to execute the code after the
invoking statement.
Functions often compute a return value. The return value is "returned" back to the "caller":
Example
JavaScript Output
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My First Paragraph.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Example:
<!DOCTYPE html>
<html>
<body>
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JavaScript Print
The only exception is that you can call the window.print() method in the
browser to print the content of the current window.
Example
<!DOCTYPE html>
<html>
<body>
<button onclick="window.print()">Print this page</button>
</body>
</html>
JavaScript Statements
JavaScript Programs
A computer program is a list of "instructions" to be "executed" by a computer.
JavaScript Statements
JavaScript statements are composed of:
This statement tells the browser to write "Hello Dolly." inside an HTML element with
id="demo":
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>In HTML, JavaScript statements are executed by the browser.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello Dolly.";
</script>
</body>
</html>
Most JavaScript programs contain many JavaScript statements.
The statements are executed, one by one, in the same order as they are
written.JavaScript programs (and JavaScript statements) are often called
JavaScript code.
Semicolons ;
Semicolons separate JavaScript statements.
Add a semicolon at the end of each executable statement:
JavaScript White Space
JavaScript ignores multiple spaces. You can add white space to your script to make it more
readable.
Example
document.getElementById("demo").innerHTML =
"Hello Dolly!";
JavaScript Code Blocks
JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.
One place you will find statements grouped together in blocks, is in JavaScript functions:
Example
function myFunction() {
document.getElementById("demo1").innerHTML = "Hello Dolly!";
document.getElementById("demo2").innerHTML = "How are you?";
}
Javascript Data Types
JavaScript provides different data types to hold different types of values.
There are two types of data types in JavaScript.
1. JavaScript Strings
A string (or a text string) is a series of characters like "John Doe".
Strings are written with quotes. You can use single or double quotes:
Example
let carName1 = "Volvo XC60"; // Using double quotes
let carName2 = 'Volvo XC60'; // Using single quotes
2. JavaScript Numbers
JavaScript has only one type of numbers.
Example
let x1 = 34.00; // Written with decimals
let x2 = 34; // Written without decimals
3. JavaScript Booleans
Booleans can only have two values: true or false.
Example
let x = 5;
let y = 5;
let z = 6;
(x == y) // Returns true
(x == z) // Returns false
4. JavaScript Arrays
JavaScript arrays are written with square brackets.
The following code declares (creates) an array called cars, containing three items (car
names):
Example
const cars = ["Saab", "Volvo", "BMW"];
Array indexes are zero-based, which means the first item is [0], second is [1], and so
on.
5. JavaScript Objects
JavaScript objects are written with curly braces {}.
A car has properties like weight and color, and methods like start and
stop:
All cars have the same properties, but the property values differ from car
to car.
All cars have the same methods, but the methods are performed at
different times.
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to a variable named car:
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
Example
const person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};
objectName.propertyName
or
objectName["propertyName"]
Object Methods
Objects can also have methods.
⚫ Using let
⚫ Using const
⚫ Using nothing
In this example, x, y, and z, are variables, declared with the var keyword
& a, b, and c, are variables, declared with the let keyword & aa,bb,and
cc, are undeclared variables.
When to Use JavaScript var?
The var keyword is used in all JavaScript code from 1995 to 2015.
If you want your code to run in older browsers, you must use var.
If you think the value of the variable can change, use let.
Example:
<!DOCTYPE html> let a = 5;
<html> let b = 7;
<body> let c = a + b;
<h1>JavaScript Variables</h1> document.getElementById("demo2").innerHTML =
"The value of c is: " + c;
<p>In this example, x, y, and z are variables.</p>
aa = 5;
<p id="demo1"></p> bb = 8;
<p id="demo2"></p> cc = aa + bb;
<p id="demo3"></p> document.getElementById("demo3").innerHTML =
"The value of cc is: " + cc;
<script> </script>
var x = 5; </body>
var y = 6; </html>
var z = x + y;
document.getElementById("demo1").innerHTML =
"The value of z is: " + z;
JavaScript Identifiers
All JavaScript variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
⚫ Names can also begin with $ and _ (but we will not use it in this tutorial).
Before ES6 (2015), JavaScript had only Global Scope and Function Scope.
ES6 introduced two important new JavaScript keywords: let and const.
Variables declared inside a { } block cannot be accessed from outside the block:
Example
{
let x = 2;
}
// x can NOT be used here
Variables declared with the var keyword can NOT have block scope.
Variables declared inside a { } block can be accessed from outside the block.
Example
{
var x = 2;
}
// x CAN be used here
Redeclaring Variables
Redeclaring a variable using the var keyword can impose problems.
Redeclaring a variable inside a block will also redeclare the variable outside the block:
Example
var x = 10;
// Here x is 10
{
var x = 2;
// Here x is 2
}
// Here x is 2
Redeclaring a variable using the let keyword can solve this problem.
Redeclaring a variable inside a block will not redeclare the variable outside the block:
Example
let x = 10;// Here x is 10
{
let x = 2;// Here x is 2
}
// Here x is 10
JavaScript Const
The const keyword was introduced in ES6 (2015).
Cannot be Reassigned
A const variable cannot be reassigned:
Example
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
Correct
const PI = 3.14159265359;
Incorrect
const PI;
PI = 3.14159265359;
When to use JavaScript const?
Always declare a variable with const when you know that the value should not
be changed.
A new Array
A new Object
A new Function
A new RegExp
Constant Arrays
const cars = ["Saab", "Volvo", "BMW"];
Constant Objects
const car = {type:"Fiat", model:"500", color:"white"};
JAVASCRIPT OPERATOR
➢ Arithmetic Operators
➢ Assignment Operators
➢ Comparison Operators
➢ Logical Operators
➢ Conditional Operators
➢ Type Operators
Note:
The numbers (in an arithmetic operation) are called operands.
The operation (to be performed between the two operands) is
defined by an operator.
JavaScript Arithmetic Operators
Operator Description
+ Addition
- Subtraction
EX:
* Multiplication
<p id="demo"></p>
** Exponentiation (ES2016)
/ Division
% <script>
Modulus (Division Remainder)
++ Increment3;
let a =
-- let x = (100 + 50) * a;
Decrement
document.getElementById
Arithmetic Operations ("demo").innerHTML = x;
A typical arithmetic operation operates on two numbers. </script>
Example
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The ++ Operator</h2>
<p id="demo"></p>
<script>
let x = 5;
x++;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Decrementing
The decrement operator (--) decrements numbers.
Example
let x = 5;
x--;
let z = x;
Exponentiation
The exponentiation operator (**) raises the first operand to the power of
the second operand.
Example
let x = 5;
let z = x ** 2;
Example
let x = 100 + 50 * 3;
Is the result of example above the same as 150 * 3, or is it the same as 100 + 150?
Is the addition or the multiplication done first?
As in traditional school mathematics, the multiplication is done first.
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
And (as in school mathematics) the precedence can be changed by using parentheses:
Example
let x = (100 + 50) * 3;
When using parentheses, the operations inside the parentheses are computed first.
When many operations have the same precedence (like addition and subtraction), they are computed
from left to right:
Example
let x = 100 + 50 - 3;
JavaScript Assignment Operators
The += Operator
The Addition Assignment Operator adds a value to a variable.
Addition Assignment Examples
let x = 10;
x += 5;
let text = "Hello"; text += " World";
The -= Operator
The Subtraction Assignment Operator subtracts a value from a variable.
Subtraction Assignment Example
let x = 10;
X -= 5;
The *= Operator
The Multiplication Assignment Operator multiplies a variable.
Multiplication Assignment Example
let x = 10;
x *= 5;
Conditional Statements (JavaScript -
if...else Statement)
Decision Making
JavaScript supports the following forms of if..else statement −
if statement
if...else statement
The if statement is the fundamental control statement that allows JavaScript to make
decisions and execute statements conditionally.
Syntax
The syntax for a basic if statement is as follows −
if (expression) {
Statement(s) to be executed if expression is true
}
Here a JavaScript expression is evaluated. If the resulting value is true, the given
statement(s) are executed. If the expression is false, then no statement would be not
executed. Most of the times, you will use comparison operators while making
decisions.
Example
<html>
<body>
<script type = "text/javascript">
<!--
var age = 20;
The 'if...else' statement is the next form of control statement that allows JavaScript to
execute statements in a more controlled way.
Syntax
if (expression) {
Statement(s) to be executed if expression is true
} else {
Statement(s) to be executed if expression is false
} Example:
<html>
<body>
<script type = "text/javascript">
var age = 15;
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
<html>
<body>
<script type = "text/javascript">
var book = "maths";
if( book == "history" ) {
document.write("<b>History Book</b>");
} else if( book == "maths" ) {
document.write("<b>Maths Book</b>");
} else if( book == "economics" ) {
document.write("<b>Economics Book</b>");
} else {
document.write("<b>Unknown Book</b>");
}
</script>
<p>Set the variable to different value and then try...</p>
</body>
<html>
JavaScript Loops
Loops are handy, if you want to run the same code over and over again, each time with
a different value.
Instead of writing:
text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";
You can write:
for (let i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
Different Kinds of Loops
JavaScript supports different kinds of loops:
Expression 3 is executed (every time) after the code block has been executed.
<!DOCTYPE html> From the example above, you can read:
<html>
<body>
Expression 1 sets a variable before the loop starts (let i =
<h2>JavaScript For Loop</h2> 0).Normally you will use expression 1 to initialize the variable
used in the loop (let i = 0).
<p id="demo"></p>
<script> This is not always the case. JavaScript doesn't care. Expression
let text = ""; 1 is optional.
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "<br>"; You can initiate many values in expression 1 (separated by
} comma):
document.getElementById("demo").innerHTML =
text;
Expression 2 defines the condition for the loop to run (i must be
</script> less than 5).Often expression 2 is used to evaluate the condition
of the initial variable.If expression 2 returns true, the loop will
</body> start over again. If it returns false, the loop will end.
</html>
Syntax
while (condition) {
// code block to be executed
}
Example
In the following example, the code in the loop will run, over and over again, as long as a variable (i) is
less than 10:
<!DOCTYPE html>
<html>
<body> Note: If you forget to increase the
<h2>JavaScript While Loop</h2>
<p id="demo"></p>
variable used in the condition, the loop
<script> will never end. This will crash your
let text = ""; browser.
let i = 0;
while (i < 10) {
text += "<br>The number is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
The Do While Loop
The do while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
Example
The example below uses a do while loop. The loop will always be executed at least on
even if the condition is false, because the code block is executed before the condition
tested:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
let text = ""
let i = 0;
do {
text += "<br>The number is " + i;
i++;
}
while (i < 10);
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
JavaScript Switch Statement
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
This is how it works:
<tr>
<td align = "right">Name</td>
<td><input type = "text" name = "Name" /></td>
</tr>
<tr>
<td align = "right">EMail</td>
<td><input type = "text" name = "EMail" /></td>
</tr>
<tr>
<td align = "right">Zip Code</td>
<td><input type = "text" name = "Zip" /></td>
</tr>
<tr>
<td align = "right">Country</td>
<td>
<select name = "Country">
<option value = "-1" selected>[choose yours]</option>
<option value = "1">USA</option>
<option value = "2">UK</option>
<option value = "3">INDIA</option>
</select>
</td>
</tr>
<tr>
<td align = "right"></td>
Dialog Box
An alert dialog box is mostly used to give a warning message to the users.
For example, if one input field requires to enter some text but the user does
not provide any input, then as a part of validation, you can use an alert box
to give a warning message.
Example:
<html>
<head>
<script type = "text/javascript">
function Warn() {
alert ("This is a warning message!");
document.write ("This is a warning message!");
}
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "Warn();" />
</form>
</body>
</html>
Confirmation Dialog Box
A confirmation dialog box is mostly used to take user's consent on any option. It displays a
dialog box with two buttons: OK and Cancel.
If the user clicks on the OK button, the window method confirm() will return true. If
the user clicks on the Cancel button, then confirm() returns false. You can use a confirmation
dialog box as follows.
<html>
<head>
<script type = "text/javascript"> <body>
<p>Click the following button to see the result:
function Confirmation() { </p>
var retVal = confirm("Do you want to continue ?"); <form>
if( retVal == true ) { <input type = "button" value = "Click Me"
document.write ("User wants to continue!"); onclick = "Confirmation();" />
return true; </form>
} else { </body>
document.write ("User does not want to continue!"); </html>
return false;
}
}
</script>
</head>
Prompt Dialog Box
The prompt dialog box is very useful when you want to pop-up a text box to
get user input. Thus, it enables you to interact with the user. The user needs
to fill in the field and then click OK.
This dialog box is displayed using a method called prompt() which takes two
parameters:
(i) a label which you want to display in the text box
and (ii) a default string to display in the text box.
ADDITION OF TWO NUMBERS
<html>
<html>
<head>
<title>addition</title> <head>
<script> <title>addition</title>
let number1 = prompt("Enter your 1st Number: "); <script>
let number2 = prompt("Enter your 2nd Number: "); var a = prompt("Enter first number");
let result = parseFloat(number1) + var b = prompt("Enter second
parseFloat(number2); number");
alert(result); var x=parseInt(a);
var y=parseInt(b);
</script>
alert(x+y);
</head>
</html> </script>
</head>
</html>
JavaScript Events
When JavaScript is used in HTML pages, JavaScript can "react" on these events.
HTML Events
Example
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button>
<button onclick="this.innerHTML=Date()">The time is?</button>
<p id="demo"></p>
<button onclick="displayDate()">The time is?</button>
<script>
function displayDate() {
document.getElementById("demo1").innerHTML = Date();
}
</script>
<p id="demo1"></p>
</body>
</html>
Common HTML Events
Here is a list of some common HTML events:
JavaScript HTML DOM
HTML DOM properties are values (of HTML Elements) that you can set or change.
A property is a value that you can get or set (like changing the content of an HTML
element).
In the example above the getElementById method used id="demo" to find the element.
The innerHTML property is useful for getting or replacing the content of HTML elements.
The innerHTML property can be used to get or change any HTML element, including <html>
and <body>.
JavaScript HTML DOM
Document
The HTML DOM Document Object
The document object represents your web page.
If you want to access any element in an HTML page, you always start with
accessing the document object.
Below are some examples of how you can use the document object to access and
manipulate HTML.
To do so, you have to find the elements first. There are several ways to do this:
Example
const element = document.getElementsByTagName("p");
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Finding HTML Elements by Tag Name.</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method.</p>
<p id="demo"></p>
<script>
const element = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML = 'The text in first paragraph (index 0) is: ' +
element[0].innerHTML;
</script>
</body>
</html>
JavaScript HTML DOM Animation
A Basic Web Page
To demonstrate how to create HTML animations with JavaScript, we will use
a simple web page:
Animation Code
JavaScript animations are done by programming gradual changes in an
element's style.
The changes are called by a timer. When the timer interval is small, the
animation looks continuous.
The basic code is:
Example
id = setInterval(frame, 5);
function frame() {
if (/* test for finished */) {
clearInterval(id);
} else {
/* code to change the element style */ }}
<!DOCTYPE html>
<html> <script>
<style> function myMove() {
#container { let id = null;
width: 400px; const elem =
height: 400px; document.getElementById("animate");
position: relative; let pos = 0;
background: yellow; clearInterval(id);
} id = setInterval(frame, 5);
#animate { function frame() {
width: 50px; if (pos == 350) {
height: 50px; clearInterval(id);
position: absolute; } else {
background-color: red; pos++;
} elem.style.top = pos + "px";
</style> elem.style.left = pos + "px";
<body> }
<p><button onclick="myMove()">Click }
Me</button></p> }
<div id ="container"> </script>
<div id ="animate"></div> </body>
</div> </html>
Reacting to Events
A JavaScript can be executed when an event occurs, like when a user clicks on an
HTML element.
To execute code when a user clicks on an element, add JavaScript code to an HTML
event attribute:
onclick=JavaScript
Examples of HTML events:
Example
Assign an onclick event to a button element:
<script>
document.getElementById("myBtn").onclick = displayDate;
</script>
In the example above, a function named displayDate is assigned to an
HTML element with the id="myBtn".
<!DOCTYPE html>
<html>
<body>
<script>
function mOver(obj) {
obj.innerHTML = "Thank You"
}
function mOut(obj) {
obj.innerHTML = "Mouse Over Me"
}
</script>
</body>
</html>
The onmousedown, onmouseup and onclick Events
The onmousedown, onmouseup, and onclick events are all parts of a mouse-click.
First when a mouse-button is clicked, the onmousedown event is triggered, then, when
the mouse-button is released, the onmouseup event is triggered, finally, when the
mouse-click is completed, the onclick event is triggered.
Example:
<html>
<body>
<div onmousedown="mDown(this)" onmouseup="mUp(this)"
style="background-color:#D94A38;width:90px;height:20px;padding:40px;">
Click Me</div>
<script>
function mDown(obj) {
obj.style.backgroundColor = "#1ec5e5";
obj.innerHTML = "Release Me";
}
function mUp(obj) {
obj.style.backgroundColor="#D94A38";
obj.innerHTML="Thank You";
}</script></body></html>
JavaScript return
What is Multimedia?
Multimedia comes in many different formats. It can be almost anything you
can hear or see, like images, music, sound, videos, records, films, animations,
and more.
Web pages often contain multimedia elements of different types and formats.
Multimedia Formats
Multimedia elements (like audio or video) are stored in media files.
The most common way to discover the type of a file, is to look at the file
extension.
Multimedia files have formats and different extensions like: .wav, .mp3, .mp4,
.mpg, .wmv, and .avi.
Note: Only MP4, WebM, and Ogg video are supported by the HTML
standard.
Common Video Formats
Common Audio Formats
MP3 is the best format for compressed recorded music. The term MP3 has become
synonymous with digital music.
If your website is about recorded music, MP3 is the choice.
Note: Only MP3, WAV, and Ogg audio are supported by the HTML standard.
HTML
Video
<!DOCTYPE html>
<html>
<body>
</body>
</html>
How it Works
The controls attribute adds video controls, like play, pause, and volume.
It is a good idea to always include width and height attributes. If height and width are not set,
the page might flicker while the video loads.
The <source> element allows you to specify alternative video files which the browser may
choose from. The browser will use the first recognized format.
The text between the <video> and </video> tags will only be displayed in browsers that do not
support the <video> element.
Example
<video width="320" height="240" autoplay>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
HTML Audio
The HTML <audio> element is used to play an audio file on a web
page.
Example
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</body>
</html>
Make Image Slider Using HTML CSS JavaScript
https://swiperjs.com
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css"/>
</head>
<body>
<div class="container">
<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide"><img src="C:\Users\Admin\OneDrive\Desktop\images\1.jpg"></div>
<div class="swiper-slide"><img src="C:\Users\Admin\OneDrive\Desktop\images\2.jpg"></div>
<div class="swiper-slide"><img src="C:\Users\Admin\OneDrive\Desktop\images\3.jpg"></div>
<div class="swiper-slide"><img src="C:\Users\Admin\OneDrive\Desktop\images\4.jpg"></div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>
<script>
const swiper = new Swiper('.swiper', {
autoplay: {
delay:3000,
disableOnInteraction: false,},
loop: true,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
</script>
</body>
style.css
* {margin:0px;
padding:0px;
box-sizing:border-box}
.container{
width:100%;
height:100vh;
display:flex;
align-items:center;
justify-content:center;
background-color:#222;
}
.swiper{
width:80%;
height:fit-content;
}
.swiper-slide img{
width:100%;
}
.swiper .swiper-button-prev, .swiper .swiper-button-next{
color:white;}
.swiper .swiper-pagination-bullet-active{background-color:#fff;}