module 2
module 2
WITH JAVASCRIPT
Module 2
INTRODUCTION TO DYNAMIC DOCUMENTS
Dynamic document, as the name suggests refers
to a document that is dynamic in such a way that
it is customizable and changeable according to
user input.
For example:-
Using CSS we can change the background color
of the web page each time the user clicks a button
on the webpage.
Using JavaScript we can ask the user to enter
his/her name and then display it dynamically on
the webpage.
POSITIONING ELEMENTS
<html lang="en">
<head>
<link rel="stylesheet" href="./styles.css" /> <title>CSS
positions</title>
<style>
.first { background-color: lightgreen; }
.second { background-color: lightcoral; }
.third { background-color: lightgoldenrodyellow; }
</style>
</head>
<body> <div class="container">
<span>Container</span>
<div class="first">First</div>
<div class="second">Second</div>
<div class="third">Third</div> </div>
</body> </html>
The Key to DHTML: The position Attribute
The CSS position attribute specifies the type of
positioning applied to an element.
The four possible values for this attribute are:
1. static
This is the default value of the position property. If
the position of an element is static, the element will
render in order, based on the position of it in the
original document flow.
2. absolute
it is positioned based on the closest positioned
element (which has position other than static).
.second { background-color: lightcoral;
position: absolute;
left: 20px;
top: 70px; }
3. Fixed
With fixed positioning we also have access
to top, left, right, bottom properties. In this case
the element is positioned relative to the browser
window's viewport.
So if we set top 70px and left 20px on a fixed
positioned element it will appear 70 pixels from
the top of the viewport and 20px from the left
edge of the viewport. Fixed positioning also
removes the document from the normal
document flow.
.second { background-color: lightcoral; position:
fixed; left: 20px; top: 70px; }
4. relative
If we set the position of an element to relative, it
will appear in the document as it would by
default using static. The trick is that by setting
position relative, we gain access to the following
CSS properties: top, left, right, bottom. With
these we can add an offset to the specific
direction. So for example if we set left: 20px. The
element will be placed 20 pixel to the right. If we
would have provided -20px it would push the
content to the left with 20px.
.second { background-color: lightcoral; position:
relative; left: 20px; }
Specifying the Position and Size of Elements
Once you have set the position attribute of an element
to something other than static, you can specify the
position of that element with some combination of the
left , top, right, and bottom attributes.
The most common positioning technique is to specify
the left and top attributes, which specify the distance
from the left edge of the containing element (usually
the document itself ) to the left edge of the element,
and the distance from the top edge of the container to
the top edge of the element.
<div style="position: absolute; left: 100px; top:
100px;">
place an element 100 pixels from the left and 100
pixels from the top of the document
you can also use right and bottom to specify the
position of the bottom and right edges of an
element relative to the bottom and right edges of
the containing element.
<div style="position: absolute; right: 0px; bottom:
0px”>
To position an element so that its top edge is 10
pixels from the top of the window and its right
edge is 10 pixels from the right of the window,
you can use these styles:
position: fixed; right: 10px; top: 10px;
In addition to the position of elements, CSS allows
you to specify their size. This is most commonly done
by providing values for the width and height style
attributes.
<div style="position: absolute; left: 10px; right: 10px;
width: 10px; height: 10px; background-color: blue">
values for the position and size attributes were
specified with the suffix "px". This stands for pixels.
The CSS standard allows measurements to be done in
a number of other units, including inches ("in"),
centimeters ("cm"), points ("pt"), and ems ("em" -- a
measure of the line height for the current font). Pixel
units are most commonly used with DHTML
programming.
CSS also allows you to specify the position and size of
an element as a percentage of the size of the
containing element
Element size and position details
It is important to understand some details about
how the left , right, width, top, bottom, and
height attributes work.
First, width and height specify the size of an
element's content area only; they do not include
any additional space required for the element's
padding, border, or margins.
To determine the full onscreen size of an element
with a border, you must add the left and right
padding and left and right border widths to the
element width, and you must add the top and
bottom padding and top and bottom border
widths to the element's height.
Moving elements
XML example: