Unit Ii-1
Unit Ii-1
SVG- Iframes - HTML5 Video and Audio tags - CSS Specificity - Box model - Margins,
padding and border – Inline and block elements - Structuring pages using Semantic Tags –
Positioning with CSS: Positions, Floats, z-index – CSS with CSS Preprocessors: SASS
SVG
SVG stands for Scalable Vector Graphics
SVG is used to define vector-based graphics for the Web
SVG defines the graphics in XML format
Every element and every attribute in SVG files can be animated
SVG is a W3C recommendation
SVG integrates with other W3C standards such as the DOM and XSL
SVG Advantages
Advantages of using SVG over other image formats (like JPEG and GIF) are:
SVG images can be created and edited with any text editor
SVG images can be searched, indexed, scripted, and compressed
SVG images are scalable
SVG images can be printed with high quality at any resolution
SVG images are zoomable
SVG graphics do NOT lose any quality if they are zoomed or resized
SVG is an open standard
SVG files are pure XML
Embed SVG Directly Into HTML Pages
<!DOCTYPE html>
<html>
<body>
<h1>MyfirstSVG</h1>
</body>
</html>
1
SVG Code explanation:
SVG Shapes
SVG has some predefined shape elements that can be used by developers:
Rectangle <rect>
Circle <circle>
Ellipse <ellipse>
Line <line>
Polyline <polyline>
Polygon <polygon>
Path <path>
Example
Code explanation:
2
The width and height attributes of the <rect> element define the height
and the width of the rectangle
The style attribute is used to define CSS properties for the rectangle
The CSS fill property defines the fill color of the rectangle
The CSS stroke-width property defines the width of the border of the
rectangle
The CSS stroke property defines the color of the border of the rectangle
Example
Code explanation:
3
An ellipse is closely related to a circle. The difference is that an ellipse has an x
and a y radius that differs from each other, while a circle has equal x and y
radius:
Example
Code explanation:
Code explanation:
The <polygon> element is used to create a graphic that contains at least three
sides.
4
Polygons are made of straight lines, and the shape is "closed" (all the lines
connect up).
Example
Code explanation:
The points attribute defines the x and y coordinates for each corner of the
polygon
The <polyline> element is used to create any shape that consists of only
straight lines (that is connected at several points):
M = moveto
L = lineto
H = horizontal lineto
V = vertical lineto
C = curveto
S = smooth curveto
Q = quadratic Bézier curve
T = smooth quadratic Béziercurveto
A = elliptical Arc
Z = closepath
Note: All of the commands above can also be expressed with lower letters.
Capital letters means absolutely positioned, lower cases means relatively
positioned.
5
The example below defines a path that starts at position 150,0 with a line to
position 75,200 then from there, a line to 225,200 and finally closing the path
back to 150,0:
Example
Write a text:
I love SVG!
Example
Example
6
HTML Iframes
An inline frame is used to embed another document within the current HTML
document.
An inline frame is used to embed another document within the current HTML
document.
Syntax
Tip: It is a good practice to always include a title attribute for the <iframe>. This
is used by screen readers to read out what the content of the iframe is.
To remove the border, add the style attribute and use the CSS border property:
Example
With CSS, you can also change the size, style and color of the iframe's border:
7
Example
The target attribute of the link must refer to the name attribute of the iframe:
Example
HTML5 features include native audio and video support without the need for
Flash.
The HTML5 <audio> and <video> tags make it simple to add media to a
website. You need to set src attribute to identify the media source and include a
controls attribute so the user can play and pause the media.
Embedding Video
</video>
The current HTML5 draft specification does not specify which video formats
browsers should support in the video tag. But most commonly used video
formats are −
Ogg − Ogg files with Thedora video codec and Vorbis audio codec.
mpeg4 − MPEG4 files with H.264 video codec and AAC audio codec.
8
Can use <source> tag to specify media along with media type and many other
attributes. A video element allows multiple source elements and browser will
use the first recognized format −
<!DOCTYPE HTML>
<html>
<body>
<videowidth="300"height="200"controlsautoplay>
<sourcesrc="/html5/foo.ogg"type="video/ogg"/>
<sourcesrc="/html5/foo.mp4"type="video/mp4"/>
Your browser does not support the <video> element.
</video>
</body>
</html>
The HTML5 video tag can have a number of attributes to control the look and feel
and various functionalities of the control −
autoplay
autobuffer
2 This Boolean attribute if specified, the video will automatically begin
buffering even if it's not set to automatically play.
controls
3 If this attribute is present, it will allow the user to control video playback,
including volume, seeking, and pause/resume playback.
4 height
9
This attribute specifies the height of the video's display area, in CSS
pixels.
loop
5 This Boolean attribute if specified, will allow video automatically seek
back to the start after reaching at the end.
preload
6 This attribute specifies that the video will be loaded at page load, and
ready to run. Ignored if autoplay is present.
poster
7
This is a URL of an image to show until the user plays or seeks.
src
8 The URL of the video to embed. This is optional; you may instead use the
<source> element within the video block to specify the video to embed.
width
9
This attribute specifies the width of the video's display area, in CSS pixels.
Embedding Audio
HTML5 supports <audio> tag which is used to embed sound content in an HTML
or XHTML document as follows.
</audio>
The current HTML5 draft specification does not specify which audio formats
browsers should support in the audio tag. But most commonly used audio
formats are ogg, mp3 and wav.
10
You can use <source&ggt; tag to specify media along with media type and
many other attributes. An audio element allows multiple source elements and
browser will use the first recognized format −
<!DOCTYPE HTML>
<html>
<body>
<audiocontrolsautoplay>
<sourcesrc="/html5/audio.ogg"type="audio/ogg"/>
<sourcesrc="/html5/audio.wav"type="audio/wav"/>
</audio>
</body>
</html>
The HTML5 audio tag can have a number of attributes to control the look and
feel and various functionalities of the control −
autoplay
2 autobuffer
11
This Boolean attribute if specified, the audio will automatically begin
buffering even if it's not set to automatically play.
controls
loop
preload
5 This attribute specifies that the audio will be loaded at page load, and
ready to run. Ignored if autoplay is present.
src
6 The URL of the audio to embed. This is optional; you may instead use the
<source> element within the video block to specify the video to embed.
CSS SPECIFICITY
If there are two or more CSS rules that point to the same element, the
selector with the highest specificity value will "win", and its style
declaration will be applied to that HTML element.
Example
In this example, we have used the "p" element as selector, and specified a red
color for this element. The text will be red:
<html>
<head>
<style>
12
p {color: red;}
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Example 2
In this example, we have added a class selector (named "test"), and specified a
green color for this class. The text will now be green (even though we have
specified a red color for the element selector "p"). This is because the class
selector is given higher priority:
<html>
<head>
<style>
.test {color: green;}
p {color: red;}
</style>
</head>
<body>
</body>
</html>
Example 3
In this example, we have added the id selector (named "demo"). The text will
now be blue, because the id selector is given higher priority:
<html>
<head>
<style>
#demo {color: blue;}
.test {color: green;}
p {color: red;}
</style>
</head>
<body>
13
<p id="demo" class="test">Hello World!</p>
</body>
</html>
Example 4
In this example, we have added an inline style for the "p" element. The text will
now be pink, because the inline style is given the highest priority:
<html>
<head>
<style>
#demo {color: blue;}
.test {color: green;}
p {color: red;}
</style>
</head>
<body>
</body>
</html>
Specificity Hierarchy
There are four categories which define the specificity level of a selector:
Start at 0, add 100 for each ID value, add 10 for each class value (or pseudo-
class or attribute selector), add 1 for each element selector or pseudo-element.
14
Selector Specificity Value Calculation
p 1 1
p.test 11 1 + 10
.test 10 10
p.test1.test2 21 1 + 10 + 10
The selector with the highest specificity value will win and take effect!
15
THE CSS BOX MODEL
The CSS box model is a container that contains multiple properties including
borders, margin, padding, and the content itself. It is used to create the design
and layout of web pages. It can be used as a toolkit for customizing the layout
of different elements. The web browser renders every element as a
rectangular box according to the CSS box model. Box-Model has multiple
properties in CSS. Some of them are given below:
content: This property is used to displays the text, images, etc, that can be
sized using the width & height property.
padding: This property is used to create space around the element, inside
any defined border.
border: This property is used to cover the content & any padding, & also
allows to set the style, color, and width of the border.
margin: This property is used to create space around the element ie., around
the border area.
Content Area: This area consists of content like text, images, or other
media content. It is bounded by the content edge and its dimensions
are given by content-box width and height.
Border Area: It is the area between the box’s padding and margin. Its
dimensions are given by the width and height of the border.
Margin Area: This area consists of space between border and margin.
The dimensions of the Margin area are the margin-box width and the
margin-box height. It is useful to separate the element from its
neighbors.
For setting the width & height property of an element(for properly rendering
the content in the browser), we need to understand the working of the CSS
Box model.
16
Content Area: This area consists of content like text, images, or other
media content. It is bounded by the content edge and its dimensions
are given by content-box width and height.
Border Area: It is the area between the box’s padding and margin. Its
dimensions are given by the width and height of the border.
Margin Area: This area consists of space between border and margin.
The dimensions of the Margin area are the margin-box width and the
margin-box height. It is useful to separate the element from its
neighbors.
For setting the width & height property of an element(for properly rendering
the content in the browser), we need to understand the working of the CSS
Box model.
17
The box model allows us to add a border around elements, and to define space
between elements.
Example
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Example 1: This example illustrates the use of the CSS Box model for aligning
& displaying it properly.
HTML
<!DOCTYPE html>
<head>
<title>CSS Box Model</title>
<style>
.main {
font-size: 36px;
font-weight: bold;
Text-align: center;
}
.gfg {
margin-left: 60px;
border: 50px solid #009900;
width: 300px;
height: 200px;
text-align: center;
padding: 50px;
}
.gfg1 {
font-size: 42px;
font-weight: bold;
color: #009900;
margin-top: 60px;
background-color: #c5c5db;
}
18
.gfg2 {
font-size: 18px;
font-weight: bold;
background-color: #c5c5db;
}
</style>
</head>
<body>
<divclass="main">CSS Box-Model Property</div>
<divclass="gfg">
<divclass="gfg1">GeeksforGeeks</div>
<divclass="gfg2">
A computer science portal for geeks
</div>
</div>
</body>
</html>
Output:
19
Example 2: This example illustrates the Box Model by implementing the various
properties.
HTML
<!DOCTYPE html>
<head>
<style>
.main {
font-size: 32px;
font-weight: bold;
text-align: center;
}
#box {
padding-top: 40px;
width: 400px;
height: 100px;
border: 50px solid green;
margin: 50px;
text-align: center;
font-size: 32px;
font-weight: bold;
}
</style>
</head>
<body>
<divclass="main">CSS Box-Model Property</div>
<divid="box">GeeksforGeeks</div>
</body>
</html>
Output:
20
HTML BLOCK AND INLINE ELEMENTS
Every HTML element has a default display value, depending on what type of
element it is.
Block-level Elements
A block-level element always takes up the full width available (stretches out
to the left and right as far as it can).
Example
<p>Hello World</p>
<div>Hello World</div>
<address><article><aside><blockquote><canvas><dd><div>
<dl><dt><fieldset><figcaption><figure><footer><form><h1>-
<h6><header><hr><li><main><nav><noscript><ol><p><pre><section
><table><tfoot><ul><video>
Inline Elements
21
The inline elements in HTML:
<a><abbr><acronym><b><bdo><big><br><button><cite><code>
<dfn><em><i><img><input><kbd><label><map><object><output>
<q><samp><script><select><small><span><strong><sub><sup>
<textarea><time><tt><var>
Example
<span>Hello World</span>
An inline element cannot contain a block-level element!
A semantic element clearly describes its meaning to both the browser and the
developer.
Examples of non-semantic elements: <div> and <span> - Tells nothing about its
content.
Many web sites contain HTML code like: <div id="nav"> <div class="header">
<div id="footer"> to indicate navigation, header, and footer.
In HTML there are some semantic elements that can be used to define different
parts of a web page:
<article>
<aside>
<details>
<figcaption>
<figure>
<footer>
<header>
<main>
<mark>
<nav>
22
<section>
<summary>
<time>
Chapters
Introduction
News items
Contact information
A web page could normally be split into sections for introduction, content, and
contact information.
23
An article should make sense on its own, and it should be possible to distribute
it independently from the rest of the web site.
Forum posts
Blog posts
User comments
Product cards
Newspaper articles
Example
Use CSS to style the <article> element:
<html>
<head>
<style>
.all-browsers {
margin: 0;
padding: 5px;
background-color: lightgray;
}
.browser {
background: white;
}
<article class="all-browsers">
<h1>Most Popular Browsers</h1>
<article class="browser">
24
<h2>Google Chrome</h2>
<p>Google Chrome is a web browser developed by Google, released in
2008. Chrome is the world's most popular web browser today!</p>
</article>
<article class="browser">
<h2>Mozilla Firefox</h2>
<p>Mozilla Firefox is an open-source web browser developed by Mozilla.
Firefox has been the second most popular web browser since January,
2018.</p>
</article>
<article class="browser">
<h2>Microsoft Edge</h2>
<p>Microsoft Edge is a web browser developed by Microsoft, released in
2015. Microsoft Edge replaced Internet Explorer.</p>
</article>
</article>
</body>
</html>
Note: You can have several <header> elements in one HTML document.
However, <header> cannot be placed within a <footer>, <address> or
another <header> element.
Example
<article>
<header>
<h1>What Does WWF Do?</h1>
<p>WWF's mission:</p>
</header>
<p>WWF's mission is to stop the degradation of our planet's natural
25
environment,
and build a future in which humans live in harmony with nature.</p>
</article>
authorship information
copyright information
contact information
sitemap
back to top links
related documents
Example
<footer>
<p>Author: Hege Refsnes</p>
<p><a href="mailto:hege@example.com">hege@example.com</a></p>
</footer>
Example
<nav>
<a href="/html/">HTML</a> |
<a href="/css/">CSS</a> |
<a href="/js/">JavaScript</a> |
<a href="/jquery/">jQuery</a>
</nav>
26
HTML <aside> Element
The <aside> element defines some content aside from the content it is placed in
(like a sidebar).
Example
Display some content aside from the content it is placed in:
<p>My family and I visited The Epcot center this summer. The weather was
nice, and Epcot was amazing! I had a great summer together with my
family!</p>
<aside>
<h4>Epcot Center</h4>
<p>Epcot is a theme park at Walt Disney World Resort featuring exciting
attractions, international pavilions, award-winning fireworks and seasonal
special events.</p>
</aside>
Example
<figure>
<img src="pic_trulli.jpg" alt="Trulli">
<figcaption>Fig1. - Trulli, Puglia, Italy.</figcaption>
</figure>
27
Always declare the document type as the first line in your document.
<!DOCTYPE html>
28
6. Always Specify alt, width, and height for Images
Always specify the alt attribute for images. This attribute is important if the
image for some reason cannot be displayed.
Also, always define the width and height of images. This reduces flickering,
because the browser can reserve space for the image before loading.
HTML allows spaces around equal signs. But space-less is easier to read and
groups entities better together.
When using an HTML editor, it is NOT convenient to scroll right and left to read
the HTML code.
For readability, add blank lines to separate large or logical code blocks.
For readability, add two spaces of indentation. Do not use the tab key.
The contents of a page title is very important for search engine optimization
(SEO)! The page title is used by search engine algorithms to decide the order
when listing pages in search results.
The position property specifies the type of positioning method used for an
element.
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.
position: static;
Static positioned elements are not affected by the top, bottom, left, and
right properties.
Example
div.static {
position: static;
30
position: relative;
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
position: fixed;
A fixed element does not leave a gap in the page where it would normally have
been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS
that is used.
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
position: absolute;
31
However; if an absolute positioned element has no positioned ancestors, it uses
the document body, and moves along with page scrolling.
Note: Absolute positioned elements are removed from the normal flow, and can
overlap elements.
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
position: sticky;
A sticky element toggles between relative and fixed, depending on the scroll
position. It is positioned relative until a given offset position is met in the
viewport - then it "sticks" in place (like position:fixed).
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
32
border: 2px solid #4CAF50;
}
The CSS clear property specifies what elements can float beside the cleared
element and on which side.
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.
img {
float: right;
}
img {
float: left;
}
img {
float: none;
}
The z-index property specifies the stack order of an element (which element
should be placed in front of, or behind, the others).
33
An element can have a positive or negative stack order:
This is a heading
Because the image has a z-index of -1, it will be placed behind the text
Example
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
Example
Here we see that an element with greater stack order is always above an
element with a lower stack order:
<html>
<head>
<style>
.container {
position: relative;
}
.black-box {
position: relative;
z-index: 1;
border: 2px solid black;
height: 100px;
margin: 30px;
}
.gray-box {
position: absolute;
z-index: 3;
34
background: lightgray;
height: 60px;
width: 70%;
left: 50px;
top: 50px;
}
.green-box {
position: absolute;
z-index: 2;
background: lightgreen;
width: 35%;
left: 270px;
top: -15px;
height: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="black-box">Black box</div>
<div class="gray-box">Gray box</div>
<div class="green-box">Green box</div>
</div>
</body>
</html>
CSS PREPROCESSOR
35
CSS is an incredibly complicated and nuanced language, and in an effort
to make it’s usage easier, developers often turn to using preprocessors
such as SASS or LESS.
1. Sass
2. LESS
3. Stylus
4. PostCSS
SASS
The SASS is the abbreviation of Syntactically Awesome Style Sheets. It is
a CSS pre-processor with syntax advancements.
The style sheets in the advanced syntax are processed by the program
and compiled into regular CSS style sheets, which can be used in the
website.
36
File name index.html
<!DOCTYPE html>
<html>
<head>
<title>SASS</title>
</head>
<body>
<ul>
<li>Algo</li>
<li>DS</li>
<li>Languages</li>
<li>Interviews</li>
<li>CS subjects</li>
</ul>
</div>
</body>
</html>
37
Variables: Variables can be used to store CSS values that may be reused. To
declare a variable in SASS, the ‘$’ character is used. For eg, $v_name.
$fs: 30px;
$bgcolor: #00ff40;
#dl {
font-size: $fs;
color: $bgcolor;
padding: $pd;
38
color: $col2;
}
}
After compiling the CSS code save it file by style.css.
#dl {
font-size: 30px;
color: #00ff40;
padding: 100px 350px;
}
#dl li {
color: #ff0066e1;
}
Mixins
Mixins helps to make a set of CSS properties reusable i.e. it saves one code and
use it again and again. It can be included in other CSS rules by using
the @include directive.
Example: This example describes the use of @mixin & @include.
$fs: 30px;
$bgcolor: #00ff40;
#col2: #ff0066e1;
$pd: 100px 350px;
@mixin font_style() {
font-family: sans-serif;
font-size: $fs;
color: blue;
}
#dl {
@include font_style();
padding: $pd;
}
After compiling the CSS code becomes:
39
#dl {
font-family: sans-serif;
font-size: 30px;
color: blue;
padding: 100px 350px;
}
Loops
Another common item in languages are loops, something else CSS lacks. Loops
can be used to repeat the same instructions multiple times without having to
be reentered multiple times. An example with SASS would be:
.margin-#{vfoo} {
This loop saves us from having the to write the same code multiple times to
change the margin size.
If/Else Statements
Yet another feature which CSS lacks are If/Else statements. These will run a
set of instructions only if a given condition is true. An example of this in SASS
would be:
} else {
40