Edited Uint2
Edited Uint2
Introduction to XML
XML is a software- and hardware-independent tool for storing and transporting data.
What is XML?
The tags in the example above (like <to> and <from>) are not defined in any XML standard.
These tags are "invented" by the author of the XML document.
HTML works with predefined tags like <p>, <h1>, <table>, etc.
With XML, the author must define both the tags and the document structure.
XML is Extensible
Most XML applications will work as expected even if new data is added (or removed).
Imagine an application designed to display the original version of note.xml (<to> <from>
<heading> <body>).
Then imagine a newer version of note.xml with added <date> and <hour> elements, and a
removed <heading>.
The way XML is constructed, older version of the application can still work:
<note>
<date>2015-09-01</date>
<hour>08:30</hour>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
OUTPUT:
Old Version
Note
To: Tove
From: Jani
Reminder
New Version
Note
To: Tove
From: Jani
The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to
use.
XM
L Documents Must Have a Root Element
XML documents must contain one root element that is the parent of all other elements:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
Output:
Note
To: Tove
From: Jani
The XML prolog is optional. If it exists, it must come first in the document.
XML documents can contain international characters, like Norwegian øæå or French êèé.
To avoid errors, you should specify the encoding used, or save your XML files as UTF-8.
XML Tree
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
An XML tree starts at a root element and branches from the root to child elements.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent, child, and sibling are used to describe the relationships between elements.
Parents have children. Children have parents. Siblings are children on the same level (brothers
and sisters).
All elements can have text content (Harry Potter) and attributes (category="cooking").
XML Elements
An XML document contains XML Elements. What is an XML Element?
An XML element is everything from (including) the element's start tag to (including) the
element's end tag.
<price>29.99</price>
text
attributes
other elements
or a mix of the above
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<title>, <author>, <year>, and <price> have text content because they contain text (like 29.99).
<bookstore> and <book> have element contents, because they contain elements.
<element />
XML Attributes
Attributes are designed to contain data related to a specific element.
Attribute values must always be quoted. Either single or double quotes can be used.
For a person's gender, the <person> element can be written like this:
<person gender="female">
or like this:
<person gender='female'>
If the attribute value itself contains double quotes you can use single quotes, like in this example:
XML Namespaces
XML Namespaces provide a method to avoid element name conflicts.
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when trying
to mix XML documents from different XML applications.
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain a
<table> element, but the elements have different content and meaning.
When using prefixes in XML, a namespace for the prefix must be defined.
The namespace can be defined by an xmlns attribute in the start tag of an element.
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="https://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
OUTPUT:
The xmlns attribute in the first <table> element gives the h: prefix a qualified namespace.
The xmlns attribute in the second <table> element gives the f: prefix a qualified namespace.
When a namespace is defined for an element, all child elements with the same prefix are
associated with the same namespace.
Note: The namespace URI is not used by the parser to look up information.
XML Parsers
An XML parser is a software library or package that provides interfaces for client applications to
work with an XML document. The XML Parser is designed to read the XML and create a way
for programs to use XML.
XML parser validates the document and check that the document is well formatted.
Let's understand the working of XML parser by the figure given below:
Types of XML Parsers
1. DOM
2. SAX
A DOM document is an object which contains all the information of an XML document. It is
composed like a tree structure. The DOM Parser implements a DOM API. This API is very
simple to use.
A DOM Parser creates an internal structure in memory which is a DOM document object and the
client applications get information of the original XML document by invoking methods on this
document object.
Advantages
1) It supports both read and write operations and the API is very simple to use.
Disadvantages
1) It is memory inefficient. (consumes more memory because the whole XML document needs
to loaded into memory).
2) It is comparatively slower than other parsers.
A SAX Parser implements SAX API. This API is an event based API and less intuitive.
Clients does not know what methods to call, they just overrides the methods of the API and place
his own code inside method.
Advantages
Disadvantages
2) Clients never know the full information because the data is broken into pieces.
XSLT
XSL (eXtensible Stylesheet Language) is a styling language for XML.
XML CODE
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Private Dancer</title>
<artist>Tina Turner</artist>
<country>UK</country>
<company>Capitol</company>
<price>8.90</price>
<year>1983</year>
</cd>
<cd>
<title>Midt om natten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>
XSLT CODE
OUTPUT:
My CD Collection
Title Artist
The Document Object Model (DOM) defines a standard for accessing and manipulating
documents:
The HTML DOM defines a standard way for accessing and manipulating HTML documents. It
presents an HTML document as a tree-structure.
The XML DOM defines a standard way for accessing and manipulating XML documents. It
presents an XML document as a tree-structure.
txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
The XML DOM is a standard for how to get, change, add, and delete XML elements.
XML DTD
DTD stands for Document Type Definition. It defines the legal building blocks of an XML
document. It is used to define document structure with a list of legal elements and attributes.
Its main purpose is to define the structure of an XML document. It contains a list of legal
elements and define the structure with the help of them.
The purpose of a DTD is to define the structure and the legal elements and attributes of an XML
document:
Note.dtd:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
A DOCTYPE declaration can also be used to define special characters or strings, used in the
document:
Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp " ">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<footer>&writer; ©right;</footer>
</note>
OUTPUT:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<footer>Writer: Donald Duck. Copyright: W3Schools.</footer>
</note>
Tip: An entity has three parts: it starts with an ampersand (&), then comes the entity name, and it
ends with a semicolon (;).
XML Schema
An XML Schema describes the structure of an XML document, just like a DTD.
An XML document with correct syntax is called "Well Formed".
An XML document validated against an XML Schema is both "Well Formed" and "Valid".
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
CSS Introduction
What is CSS?
The CSS provides the style to the HTML element, which the browser interprets. After being
interpreted by the browser, the CSS style property will be applied to all the elements of the
HTML. We can provide style property to the HTML element in three parts. These three parts are
as follows.
1. Selector
It is an HTML tag. All the style properties of the CSS will be applied to the selector. The selector
tag like <h1> or <table> etc.
2. Property
It is a type of attribute that is present in HTML tags. All the attributes of the HTML will be
converted to the CSS properties. The CSS properties like color, border, etc.
3. Value
In HTML, these are assigned to the properties. For example, the color property can have a value
of either red or #F1F1F1, etc.
Syntax:
1. selector { property: value }
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded
by curly braces.
Example
In this example all <p> elements will be center-aligned, with a red text color:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
OUTPUT:
Hello World!
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
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule
set. CSS selectors select HTML elements according to its id, class, type, attribute etc.
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: blue;
</style>
</head>
<body>
</body>
</html>
OUTPUT:
Me too!
And me!
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element. An id is
always unique within the page so it is chosen to select a single, unique element.
It is written with the hash character (#), followed by the id of the element.
EXAMPLE:
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Javatpoint.com</p>
<p>This paragraph will not be affected.</p>
</body>
</html>
OUTPUT:
Hello Javatpoint.com
The class selector selects HTML elements with a specific class attribute. It is used with a period
character . (full stop symbol) followed by the class name.
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
</style>
</head>
<body>
</body>
</html>
OUTPUT:
This heading is blue and center-aligned.
The universal selector is used as a wildcard character. It selects all the elements on the pages.
<!DOCTYPE html>
<html>
<head>
<style>
*{
color: green;
font-size: 20px;
</style>
</head>
<body>
<h2>This is heading</h2>
<p>And me!</p>
</body>
</html>
OUTPUT:
This is heading
Me too!
And me!
The grouping selector is used to select all the elements with the same style definitions.
Grouping selector is used to minimize the code. Commas are used to separate each selector in
grouping.
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: blue;
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
<p>This is a paragraph.</p>
</body>
</html>
OUTPUT:
Hello Javatpoint.com
Hello Javatpoint.com (In smaller font)
This is a paragraph.
CSS Background
CSS background property is used to define the background effects on element. There are 5 CSS
background properties that affects the HTML elements:
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position
1) CSS background-color
The background-color property is used to specify the background color of the element.
<!DOCTYPE html>
<html>
<head>
<style>
h2,p{
background-color: #b0d4de;
}
</style>
</head>
<body>
<h2>My first CSS page.</h2>
<p>Hello Javatpoint. This is an example of CSS background-color.</p>
</body>
</html>
Output:
2) CSS background-image
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper1.gif");
margin-left:100px;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
</body>
</html>
OUTPUT:
Hello Javatpoint.com
3) CSS background-repeat
By default, the background-image property repeats the background image horizontally and
vertically. Some images are repeated only horizontally or vertically.
background-repeat: repeat-x;
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient-bg.png");
background-repeat: repeat-x;
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
</body>
</html>
OUTPUT:
Hello Javatpoint.com
4) CSS background-attachment
The background-attachment property is used to specify if the background image is fixed or scroll
with the rest of the page in browser window. If you set fixed the background image then the
image will not move during scrolling in the browser. Let?s take an example with fixed
background image.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-repeat: no-repeat;
background-attachment: fixed;
margin-left:200px;
</style>
</head>
<body>
<p>If you do not see any scrollbars, Resize the browser window.</p>
</body>
</html>
OUTPUT:
5) CSS background-position
The background-position property is used to define the initial position of the background image.
By default, the background image is placed on the top-left of the webpage.
1. center
2. top
3. bottom
4. left
5. right
<html>
<head>
<style>
body {
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
</style>
</head>
<body>
</body>
</html>
OUTPUT:
CSS Cursor
Every day, we already use customized cursors. This interaction is made possible by using
modified cursors, such as when you hover over buttons, the pointer cursor transforms into a
hand, or when you hover over the text and the cursor transforms into a text cursor.
These cursors provide options for action at the precise spot you are hovering over. Examples
include cursors that indicate you should click links, drag and drop elements, zoom in and out of
objects, and more.
Using photos as submit buttons on forms is a useful application of this capability. By default, a
hand appears instead of a pointer when a cursor is above a link. However, a form's submit button
does not cause it to change form. This serves as a visual cue that the picture is clickable anytime
someone hovers over an image that is a submit button.
This property is specified by zero or more <url> values separated by commas, followed by one
keyword value as required, and each <url> will refer to the image file.
Syntax
cursor: value;
<!DOCTYPE html>
<html>
<head>
<title> CSS cursor property </title>
<style>
.wait {
cursor: wait;
}
h1 {
color: red;
}
</style>
</head>
<body>
<center>
<h1>Welcome to the page</h1>
<p>To change the mouse cursor, move the mouse over the text</p>
</center>
</body>
</html>
Output
CSS Text
Text Color
The color property is used to set the color of the text. The color is specified by:
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: blue;
h1 {
color: green;
</style>
</head>
<body>
<p>Another paragraph.</p>
</body>
</html>
OUTPUT:
This is heading 1
This is an ordinary paragraph. Notice that this text is blue. The default text color for a page is defined in the body
selector.
Another paragraph.
Text Alignment
The following example shows center aligned, and left and right aligned text (left alignment is
default if text direction is left-to-right, and right alignment is default if text direction is right-to-
left):
When the text-align property is set to "justify", each line is stretched so that every line has equal
width, and the left and right margins are straight (like in magazines and newspapers):
div {
text-align: justify;
}
Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
h2 {
text-align: left;
h3 {
text-align: right;
div {
padding: 10px;
width: 200px;
height: 200px;
text-align: justify;
</style>
</head>
<body>
<h1>Heading 1 (center)</h1>
<h2>Heading 2 (left)</h2>
<h3>Heading 3 (right)</h3>
<p>The three headings above are aligned center, left and right.</p>
<div>
In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind
ever since. 'Whenever you feel like criticizing anyone,' he told me, 'just remember that all the people in this world
haven't had the advantages that you've had.'
</div>
</body>
</html>
OUTPUT:
Heading 1 (center)
Heading 2 (left)
Heading 3 (right)
The three headings above are aligned center, left and right.
Text Decoration
text-decoration-line
text-decoration-color
text-decoration-style
text-decoration-thickness
text-decoration
Add
a Decoration Line to Text
Tip: You can combine more than one value, like overline and underline to display lines both
over and under a text.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: overline;
h2 {
text-decoration: line-through;
h3 {
text-decoration: underline;
p.ex {
</style>
</head>
<body>
<p><strong>Note:</strong> It is not recommended to underline text that is not a link, as this often confuses
the reader.</p>
</body>
</html>
OUTPUT:
Note: It is not recommended to underline text that is not a link, as this often confuses the reader.
The text-decoration-color property is used to set the color of the decoration line.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration-line: overline;
text-decoration-color: red;
h2 {
text-decoration-line: line-through;
text-decoration-color: blue;
h3 {
text-decoration-line: underline;
text-decoration-color: green;
}
p{
text-decoration-color: purple;
</style>
</head>
<body>
</body>
</html>
OUTPUT:
The text-decoration-style property is used to set the style of the decoration line.
h1 {
text-decoration-line: underline;
text-decoration-style: solid;
}
Specify the Thickness for the Decoration Line
The text-decoration-thickness property is used to set the thickness of the decoration line.
h2 {
text-decoration-line: underline;
text-decoration-thickness: 5px;
}
Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter
of each word:
Example
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
Text Spacing
text-indent
letter-spacing
line-height
word-spacing
white-space
Text Indentation
The text-indent property is used to specify the indentation of the first line of a text:
Example
p{
text-indent: 50px;
}
Letter Spacing
The letter-spacing property is used to specify the space between the characters in a text.
The following example demonstrates how to increase or decrease the space between characters:
Example
h1 {
letter-spacing: 5px;
}
h2 {
letter-spacing: -2px;
}
Line Height
Example
p.small {
line-height: 0.8;
}
p.big {
line-height: 1.8;
}
Word Spacing
The word-spacing property is used to specify the space between the words in a text.
The following example demonstrates how to increase or decrease the space between words:
Example
p.one {
word-spacing: 10px;
}
p.two {
word-spacing: -2px;
}
White Space
Example
p{
white-space: nowrap;
}
Text Shadow
In its simplest use, you only specify the horizontal shadow (2px) and the vertical shadow (2px):
CSS Font
CSS Font property is used to control the look of texts. By the use of CSS font property you can
change the text size, color, style and more. You have already studied how to make text bold or
underlined. Here, you will also know how to resize your font using percentage.
CSS font color is a standalone attribute in CSS although it seems that it is a part of CSS fonts. It
is used to change the color of the text.
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 100%;
}
h1 { color: red; }
h2 { color: #9000A1; }
p { color:rgb(0, 220, 98); }
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
</body>
</html>
Output:
This is heading 1
This is heading 2
This is a paragraph
Serif: Serif fonts include small lines at the end of characters. Example of serif: Times new
roman, Georgia etc.
Sans-serif: A sans-serif font doesn't include the small lines at the end of characters. Example of
Sans-serif: Arial, Verdana etc.
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 100%;
}
h1 { font-family: sans-serif; }
h2 { font-family: serif; }
p { font-family: monospace; }
}
</style>
</head>
<body>
<h1>This heading is shown in sans-serif.</h1>
<h2>This heading is shown in serif.</h2>
<p>This paragraph is written in monospace.</p>
</body>
</html>
Output:
CSS font size property is used to change the size of the font.
These are the possible values that can be used to set the font size:
<html>
<head>
</head>
<body>
</body>
</html>
OUTPUT:
This font size is extremely small.
CSS Font style property defines what type of font you want to display. It may be italic, oblique,
or normal.
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 100%;
}
h2 { font-style: italic; }
h3 { font-style: oblique; }
h4 { font-style: normal; }
}
</style>
</head>
<body>
<h2>This heading is shown in italic font.</h2>
<h3>This heading is shown in oblique font.</h3>
<h4>This heading is shown in normal font.</h4>
</body>
</html>
Output:
CSS font variant property specifies how to set font variant of an element. It may be normal and
small-caps.
<!DOCTYPE html>
<html>
<head>
<style>
p { font-variant: small-caps; }
h3 { font-variant: normal; }
</style>
</head>
<body>
<h3>This heading is shown in normal font.</h3>
<p>This paragraph is shown in small font.</p>
</body>
</html>
Output:
CSS font weight property defines the weight of the font and specify that how bold a font is. The
possible values of font weight may be normal, bold, bolder, lighter or number (100, 200..... upto
900).
<!DOCTYPE html>
<html>
<body>
<p style="font-weight:bold;">This font is bold.</p>
<p style="font-weight:bolder;">This font is bolder.</p>
<p style="font-weight:lighter;">This font is lighter.</p>
<p style="font-weight:100;">This font is 100 weight.</p>
<p style="font-weight:200;">This font is 200 weight.</p>
<p style="font-weight:300;">This font is 300 weight.</p>
<p style="font-weight:400;">This font is 400 weight.</p>
<p style="font-weight:500;">This font is 500 weight.</p>
<p style="font-weight:600;">This font is 600 weight.</p>
<p style="font-weight:700;">This font is 700 weight.</p>
<p style="font-weight:800;">This font is 800 weight.</p>
<p style="font-weight:900;">This font is 900 weight.</p>
</body>
</html>
Output:
CSS Lists
<!DOCTYPE html>
<html>
<head>
<style>
ul.a {
list-style-type: circle;
ul.b {
list-style-type: square;
ol.c {
list-style-type: upper-roman;
ol.d {
list-style-type: lower-alpha;
</style>
</head>
<body>
<ul class="a">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="b">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ol class="c">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ol class="d">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
</body>
</html>
OUTPUT:
Coffee
Tea
Coca Cola
I. Coffee
II. Tea
III. Coca Cola
a. Coffee
b. Tea
c. Coca Cola
CSS Tables
Table Borders
The example below specifies a solid border for <table>, <th>, and <td> elements:
Firstname Lastname
Peter Griffin
Lois Griffin
Example:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
OUTPUT:
Add a border to a table:
Firstname Lastname
Peter Griffin
Lois Griffin
The components that can be depicted on the web page consist of one or more than one
rectangular box.
A CSS box model is a compartment that includes numerous assets, such as edge, border, padding
and material. It is used to develop the design and structure of a web page. It can be used as a set
of tools to personalize the layout of different components. According to the CSS box model, the
web browser supplies each element as a square prism.
The CSS box model contains the different properties in CSS. These are listed below.
o Border
o Margin
o Padding
o Content
Border Field
It is a region between the padding-box and the margin. Its proportions are determined by the
width and height of the boundary.
Margin Field
This segment consists of the area between the boundary and the edge of the border.
The proportion of the margin region is equal to the margin-box width and height. It is better to
separate the product from its neighbor nodes.
Padding Field
This field requires the padding of the component. In essence, this area is the space around the
subject area and inside the border-box. The height and the width of the padding box decide its
proportions.
Content Field
Material such as text, photographs, or other digital media is included in this area.
It is constrained by the information edge, and its proportions are dictated by the width and height
of the content enclosure.
Typically, when you assign the width and height of an attribute using the CSS width and height
assets, it means you just positioned the height and width of the subject areas of that component.
The additional height and width of the unit box is based on a range of influences.
The specific area that an element box may occupy on a web page is measured as follows-
<!DOCTYPE html>
<head>
<style>
.main
font-size:30px;
font-weight:bold;
Text-align:center;
.gfg
margin-left:50px;
width:300px;
height:200px;
text-align:center;
padding:50px;
.gfg1
font-size:40px;
font-weight:bold;
color:black;
margin-top:60px;
background-color:purple;
.gfg2
font-size:20px;
font-weight:bold;
background-color:white;
</style>
</head>
<body>
</div>
</body>
</html>
OUTPUT:
CSS position
The position property specifies the type of positioning method used for an element (static,
relative, fixed, absolute or sticky).
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.
position: static;
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:
<!DOCTYPE html>
<html>
<head>
<style>
div.static {
position: static;
</style>
</head>
<body>
<h2>position: static;</h2>
<p>An element with position: static; is not positioned in any special way; it is always positioned according to the
normal flow of the page:</p>
<div class="static">
</div>
</body>
</html>
OUTPUT:
position: static;
An element with position: static; is not positioned in any special way; it is always positioned according to
the normal flow of the page:
position: relative;
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it
to be adjusted away from its normal position. Other content will not be adjusted to fit into any
gap left by the element.
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which means it always
stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are
used to position the element.
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;
}
This <div> element has position: fixed;
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor
(instead of positioned relative to the viewport, like fixed).
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.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
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. It is
positioned relative until a given offset position is met in the viewport - then it "sticks" in place
(like position:fixed).
Note: Internet Explorer does not support sticky positioning. Safari requires a -webkit- prefix (see
example below). You must also specify at least one of top, right, bottom or left for sticky
positioning to work.
In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll
position.
Example
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
CSS float
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.
The following example specifies that an image should float to the right in a text:
<h2>Float Right</h2>
<p>In this example, the image will float to the right in the paragraph, and the text in the
paragraph will wrap around the image.</p>
</body>
</html>
CSS Gradients
Gradient Backgrounds
CSS
gradients let you display smooth transitions between two or more specified colors.
To create a linear gradient you must define at least two color stops. Color stops are the colors
you want to render smooth transitions among. You can also set a starting point and a direction
(or an angle) along with the gradient effect.
Syntax
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
The following example shows a linear gradient that starts at the top. It starts red, transitioning to
yellow:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
background-color: red; /* For browsers that do not support gradients */
background-image: linear-gradient(red, yellow);
}
</style>
</head>
<body>
<div id="grad1"></div>
</body>
</html>
OUTPUT:
Example
#grad {
background-image: linear-gradient(red, yellow);
}
The following example shows a linear gradient that starts from the left. It starts red, transitioning
to yellow:
left to right
Example
#grad {
background-image: linear-gradient(to right, red , yellow);
}
text-shadow
box-shadow
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
OUTPUT:
Text-shadow effect!
Multiple Shadows
To add more than one shadow to the text, you can add a comma-separated list of shadows.
The following example shows a red and blue neon glow shadow:
</body>
</html>
OUTPUT:
CSS 2D Transforms
CSS transforms allow you to move, rotate, scale, and skew elements.
With the CSS transform property you can use the following 2D transformation methods:
translate()
rotate()
scaleX()
scaleY()
scale()
skewX()
skewY()
skew()
matrix()
The translate() method moves an element from its current position (according to the parameters
given for the X-axis and the Y-axis).
The following example moves the <div> element 50 pixels to the right, and 100 pixels down
from its current position:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
transform: translate(50px,100px);
}
</style>
</head>
<body>
<div>
This div element is moved 50 pixels to the right, and 100 pixels down from its current position.
</div>
</body>
</html>
OUTPUT:
The translate() Method
This div element is moved 50 pixels to the right, and 100 pixels down from its current position.
The following example rotates the <div> element clockwise with 20 degrees:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
}
div#myDiv {
transform: rotate(20deg);
}
</style>
</head>
<body>
<div>
This a normal div element.
</div>
<div id="myDiv">
This div element is rotated clockwise 20 degrees.
</div>
</body>
</html>
OUTPUT:
The scale() method increases or decreases the size of an element (according to the parameters
given for the width and height).
The following example increases the <div> element to be two times of its original width, and
three times of its original height:
Example
div {
transform: scale(2, 3);
}
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 150px;
width: 200px;
height: 100px;
background-color: yellow;
border: 1px solid black;
transform: scale(2,3);
}
</style>
</head>
<body>
<h1>The scale() Method</h1>
<div>
This div element is two times of its original width, and three times of its original height.
</div>
</body>
</html>
OUTPUT:
This div element is two times of its original width, and three times of its original
height.
The following example increases the <div> element to be two times of its original width:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 150px;
width: 200px;
height: 100px;
background-color: yellow;
border: 1px solid black;
transform: scaleX(2);
}
</style>
</head>
<body>
<div>
This div element is two times of its original width.
</div>
</body>
</html>
OUTPUT:
The following example increases the <div> element to be three times of its original height:
Example
div {
transform: scaleY(3);
}
The skewX() Method
The skewX() method skews an element along the X-axis by the given angle.
The following example skews the <div> element 20 degrees along the X-axis:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
}
div#myDiv {
transform: skewX(20deg);
}
</style>
</head>
<body>
<h1>The skewX() Method</h1>
<p>The skewX() method skews an element along the X-axis by the given angle.</p>
<div>
This a normal div element.
</div>
<div id="myDiv">
This div element is skewed 20 degrees along the X-axis.
</div>
</body>
</html>
OUTPUT:
The skewX() method skews an element along the X-axis by the given angle.
The skewY() method skews an element along the Y-axis by the given angle.
The following example skews the <div> element 20 degrees along the Y-axis:
Example
div {
transform: skewY(20deg);
}
The skew() Method
The skew() method skews an element along the X and Y-axis by the given angles.
The following example skews the <div> element 20 degrees along the X-axis, and 10 degrees
along the Y-axis:
Example
div {
transform: skew(20deg, 10deg);
}
The matrix() Method
The matrix() method combines all the 2D transform methods into one.
The matrix() method take six parameters, containing mathematic functions, which allows you to
rotate, scale, move (translate), and skew elements.
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
}
div#myDiv1 {
transform: matrix(1, -0.3, 0, 1, 0, 0);
}
div#myDiv2 {
transform: matrix(1, 0, 0.5, 1, 150, 0);
}
</style>
</head>
<body>
<p>The matrix() method combines all the 2D transform methods into one.</p>
<div>
This a normal div element.
</div>
<div id="myDiv1">
Using the matrix() method.
</div>
<div id="myDiv2">
Another use of the matrix() method.
</div>
</body>
</html>
OUTPUT:
The matrix() method combines all the 2D transform methods into one.
CSS 3D Transforms
Mouse over the elements below to see the difference between a 2D and a 3D transformation:
2D rotate
3D rotate
CSS 3D Transforms Methods
With the CSS transform property you can use the following 3D transformation methods:
rotateX()
rotateY()
rotateZ()
The rotateX() Method
The rotateX() method rotates an element around its X-axis at a given degree:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
}
#myDiv {
transform: rotateX(150deg);
}
</style>
</head>
<body>
<p>The rotateX() method rotates an element around its X-axis at a given degree.</p>
<div>
This a normal div element.
</div>
<div id="myDiv">
This div element is rotated 150 degrees.
</div>
</body>
</html>
OUTPUT:
The rotateX() method rotates an element around its X-axis at a given degree.
The rotateY() method rotates an element around its Y-axis at a given degree:
Example
#myDiv {
transform: rotateY(150deg);
}
The rotateZ() Method
The rotateZ() method rotates an element around its Z-axis at a given degree:
Example
#myDiv {
transform: rotateZ(90deg);
}
CSS Transitions
CSS transitions allows you to change property values smoothly, over a given duration.
CSS
How to Use CSS Transitions?
Note: If the duration part is not specified, the transition will have no effect, because the default
value is 0.
The following example shows a 100px * 100px red <div> element. The <div> element has also
specified a transition effect for the width property, with a duration of 2 seconds:
Example
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
The transition effect will start when the specified CSS property (width) changes value.
Now, let us specify a new value for the width property when a user mouses over the <div>
element:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
div:hover {
width: 300px;
</style>
</head>
<body>
<p>Hover over the div element below, to see the transition effect:</p>
<div></div>
</body>
</html>
OUTPUT:
Hover over the div element below, to see the transition effect:
The transition-timing-function property specifies the speed curve of the transition effect.
The transition-timing-function property can have the following values:
ease - specifies a transition effect with a slow start, then fast, then end slowly (this is
default)
linear - specifies a transition effect with the same speed from start to end
ease-in - specifies a transition effect with a slow start
ease-out - specifies a transition effect with a slow end
ease-in-out - specifies a transition effect with a slow start and end
cubic-bezier(n,n,n,n) - lets you define your own values in a cubic-bezier function
The following example shows some of the different speed curves that can be used:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
div:hover {
width: 300px;
}
</style>
</head>
<body>
<p>Hover over the div elements below, to see the different speed curves:</p>
<div id="div1">linear</div><br>
<div id="div2">ease</div><br>
<div id="div3">ease-in</div><br>
<div id="div4">ease-out</div><br>
<div id="div5">ease-in-out</div><br>
</body>
</html>
OUTPUT:
Hover over the div elements below, to see the different speed curves:
linear
ease
ease-in
ease-out
ease-in-out
CSS Animations
CSS
You can change as many CSS properties you want, as many times as you want.
To use CSS animation, you must first specify some keyframes for the animation.
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.
The following example binds the "example" animation to the <div> element. The animation will
last for 4 seconds, and it will gradually change the background-color of the <div> element from
"red" to "yellow":
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
@keyframes example {
to {background-color: yellow;}
</style>
</head>
<body>
<h1>CSS Animation</h1>
<div></div>
</body>
</html>
OUTPUT:
CSS Animation