Css 3
Css 3
Css 3
• HTML5 Mark-up
• Browser Support
• Forms
• Canvas
• SVG
• Geo location
The <input> element in HTML4
The <input> element in HTML4
The <input> element in HTML4
<html>
<form action = "http://example.com/cgiscript.pl" method = "post">
<p>
<label for = "firstname">first name: </label>
<input type = "text" id = "firstname"><br />
</body>
</html>
The <input> element in HTML5
- This attribute on <input> and <textarea> elements provide a hint to the user
of what can be entered in the field.
<!DOCTYPE HTML>
<html>
<body>
Enter email :
<input type = "email" name = "newinput” placeholder = "[email protected]"/>
<input type = "submit" value = "submit" />
</form>
</body>
</html>
The <input> element in HTML5
<!DOCTYPE HTML>
<html>
<body>
</form>
</body>
</html>
The <input> element in HTML5
<!DOCTYPE HTML>
<html>
<body>
</form>
</body>
</html>
HTML5 Graphics Elements
HTML5 offers new semantic elements to define different parts of a web page:
• <Canvas>
- The <canvas> tag is used to draw graphics, on the fly, via scripting (usually
JavaScript).
- The <canvas> tag is only a container for graphics, you must use a script to
actually draw the graphics.
HTML5 Graphics Elements
HTML5 offers new semantic elements to define different parts of a web page:
• <Canvas>
- The <canvas> element must have an id attribute so it can be referred to by
JavaScript.
- The width and height attribute is necessary to define the size of the canvas.
<canvas id="myCanvas" width="200" height="100"></canvas>
Set the fill style of the drawing object to the color red:
ctx.fillStyle = "#FF0000";
The fillRect(x,y,width,height) method draws a rectangle, filled with the fill style, on
the canvas:
To create a circle with arc( ): Set start angle to 0 and end angle to 2*Math.PI.
The x and y parameters define the x- and y-coordinates of the center of the circle.
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
</script>
</body>
</html>
HTML5 Graphics Elements
To draw text on a canvas, the most important property and methods are:
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillStyle = "red"; // Text color
ctx.textAlign = "center"; // TextAlignment
ctx.fillText("Hello World",10,50);
OR
ctx.strokeText("Hello World", 10, 50);
</script>
</body>
</html>
Drawing a Line
closePath()
3 This method marks the current subpath as closed, and starts a new subpath with a
point the same as the start and end of the newly closed subpath.
fill()
4 This method fills the subpaths with the current fill style.
stroke()
5 This method strokes the subpaths with the current stroke style.
lineTo(x, y)
6 This method adds the given point to the current subpath, connected to the
previous one by a straight line.
Drawing a Line
HTML5 canvas provides the following two important properties to apply colors to a
shape −
Sr.No. Method and Description
fillStyle
1 This attribute represents the color or style to use inside the shapes.
strokeStyle
This attribute represents the color or style to use for the lines around
2
shapes.
HTML5 Graphics Elements
Canvas – Gradients
Gradients can be used to fill rectangles, circles, lines, text, etc. Shapes on the canvas
are not limited to solid colors.
The addColorStop() method specifies the color stops, and its position along the
gradient. Gradient positions can be anywhere between 0 to 1.
To use the gradient, set the fillStyle or strokeStyle property to the gradient, then draw
the shape (rectangle, text, or a line).
<!DOCTYPE html>
<html>
<body>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Create gradient
var grd = ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
</body>
</html>
Create Gradient
HTML5 canvas allows us to fill and stroke shapes using linear and radial gradients using
the following methods −`
Sr.No. Method and Description
addColorStop(offset, color)
This method adds a color stop with the given color to the gradient at the given
1 offset. Here 0.0 is the offset at one end of the gradient, 1.0 is the offset at the other
end.
</body>
</html>
SVG
SVG Code explanation:
• An SVG image begins with an <svg> element
• The width and height attributes of the <svg> element define the width and
height of the SVG image
• The <circle> element is used to draw a circle
• The cx and cy attributes define the x and y coordinates of the center of the
circle. If cx and cy are not set, the circle's center is set to (0, 0)
• The r attribute defines the radius of the circle
• The stroke and stroke-width attributes control how the outline of a shape
appears.
• We set the outline of the circle to a 4px green "border"
• The fill attribute refers to the color inside the circle. We set the fill color to
yellow
• The closing </svg> tag closes the SVG image
SVG
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>
SVG
<!DOCTYPE html>
<html>
<body>
</body>
</html>
SVG
<!DOCTYPE html>
<html>
<body>
</body>
</html>
</body>
</html>
•The cx attribute defines the x coordinate of the center of the ellipse
• The cy attribute defines the y coordinate of the center of the ellipse
• The rx attribute defines the horizontal radius
• The ry attribute defines the vertical radius
SVG
<!DOCTYPE html>
<html>
<body>
</body>
</html>
SVG
<!DOCTYPE html>
<html>
<body>
</body>
</html>
</body>
</html>
Differences between SVG and Canvas
SVG Canvas
Vector based (composed of shapes) Raster based (composed of pixel)
Multiple graphical elements, which become the Single element similar to <img> in behavior.
part of the page's DOM tree Canvas diagram can be saved to PNG or JPG
format
Modified through script and CSS Modified through script only
Good text rendering capabilities Poor text rendering capabilities
Give better performance with smaller number Give better performance with larger number of
of objects or larger surface, or both objects or smaller surface, or both
Better scalability. Poor scalability.
Can be printed with high quality at any Not suitable for printing on higher resolution.
resolution. Pixelation does not occur Pixelation may occur
Audio / Video
• The controls attribute adds audio controls, like play, pause, and volume.
• The <source> element allows you to specify alternative audio files which the browser may
choose from.
•The text between the <audio> and </audio> tags will only be displayed in browsers that do
autobuffer
This Boolean attribute if specified, the video will automatically begin buffering even
2
if it's not set to automatically play.
controls
If this attribute is present, it will allow the user to control video playback, including
3
volume, seeking, and pause/resume playback.
height
4 This attribute specifies the height of the video's display area, in CSS pixels.
loop
This Boolean attribute if specified, will allow video automatically seek back to the
5
start after reaching at the end.
Audio / Video
<!DOCTYPE html>
<html>
<body>
</body>
</html>
HTML5 Local Storage
• Cookies are small pieces of data which a server can store in the browser.
• The cookie is sent by the browser along with all future HTTP requests to the
• HTML5 local storage can store somewhere between 2MB and 10MB data in
the browser (per origin - domain name).
• You can also remove a particular item from the storage if it exists, by passing the key
name to the removeItem() method, like localStorage.removeItem("first_name").
• However, if you want to remove the complete storage use the clear() method, like
localStorage.clear().
• The clear() method takes no arguments, and simply clears all key/value pairs from
localStorage at once, so think carefully before you using it.
HTML5 Local Storage
• HTML5 local storage offers a simple key - value store, like a hash table or
dictionary object.
•The local storage object looks very similar to a regular JavaScript object, with
the exception that it is stored in the browser, even if the page is unloaded.
<body>
</body>
</html>
HTML5 SessionStorage
<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter() {
if (typeof(Storage) !== "undefined") {
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
} else {
sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the
button " + sessionStorage.clickcount + " time(s) in this session.";
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does
not support web storage...";
}
}
</script>
</head>
HTML5 SessionStorage
<body>
</body>
</html>
HTML5 Geolocation
•The Geolocation API of HTML5 helps in identifying the user’s location, which
can be used to provide location specific information or route navigation details
to the user.
• The geolocation property of the global navigator object helps in detecting the
browser support for the Geolocation API.
if (navigator.geolocation)
{
// Get the user's current position
} else
{
alert('Geolocation is not supported in your browser');
}
HTML5 Geolocation
Get the user’s current location
• The current location of the user can be obtained using the getCurrentPosition
of the navigator.geolocation object.
• If the location data is fetched successfully, the success callback function will be
invoked with the obtained position object as its input parameter.
• Otherwise, the error callback function will be invoked with the error object as its
input parameter.
HTML5 Geolocation
if (navigator.geolocation)
{
// Get the user's current position
navigator.geolocation.getCurrentPosition(showPosition, showError, optn);
} else
{
alert('Geolocation is not supported in your browser');
}
HTML5 Geolocation
• You can hold the mouse button down over an element and drag it to another
location. If you want to drop the element there, just release the mouse button.
• If you want to drag an element, you need to set the draggable attribute to
true for that element.
• Set an event listener for dragstart that stores the data being dragged.
• The event listener dragstart will set the allowed effects (copy, move,
link, or some combination).
HTML5 Drag and Drop
• The dragenter event, which is used to determine whether or not the drop
target is to accept the drop. If the drop is to be accepted, then this event
has to be canceled.
• Finally, the drop event, which allows the actual drop to be performed.
HTML5 Drag and Drop
First of all: To make an element draggable, set the draggable attribute to true:
<img draggable="true">
In the example above, the ondragstart attribute calls a function, drag(event), that
specifies what data to be dragged.
The dataTransfer.setData() method sets the data type and the value of the
dragged data:
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
HTML5 Drag and Drop
• The ondragover event specifies where the dragged data can be dropped.
event.preventDefault()
HTML5 Drag and Drop
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
HTML5 Drag and Drop
Events for Drag and Drop feature
Event Description
Drag It fires every time when the mouse is moved while the object is being
dragged.
Dragstart It is a very initial stage. It fires when the user starts dragging object.
Dragenter It fires when the user moves his/her mouse cursur over the target
element.
Dragover This event is fired when the mouse moves over an element.
Dragleave This event is fired when the mouse leaves an element.
Drop Drop It fires at the end of the drag operation.
Dragend It fires when user releases the mouse button to complete the drag
operation.
HTML5 Drag and Drop
Drag and Drop Events
Event Description
ondragstart Fires when the user starts dragging an element.
ondragenter Fires when a draggable element is first moved into a drop listener.
ondragover Fires when the user drags an element over a drop listener.
ondragleave Fires when the user drags an element out of drop listener.
ondrag Fires when the user drags an element anywhere; fires constantly but can
give X and Y coordinates of the mouse cursor.
ondrop Fires when the user drops an element into a drop listener successfully.
ondragend Fires when the drag action is complete, whether it was successful or not.
This event is not fired when dragging a file to the browser from the
desktop.
References
• https://www.dcpehvpm.org/E-Content/BCA/BCA-II/Web%20Technology/the-
complete-reference-html-css-fifth-edition.pdf
• https://www.studytonight.com/html5-references/