MODULE 2 Part 1 CSS
MODULE 2 Part 1 CSS
</body>
</html>
• In above example,
• p is a selector in CSS (it points to the HTML element you want to
style: <p>).
• font-family is a property, and times new roman is the property
value
• font-size is a property, and 20px is the property value
LEVELS OF STYLE SHEETS
• The three levels of style sheets, in order from lowest level to highest
level, are inline, document level, and external.
• Inline style sheets apply to the content of a single XHTML element.
• Document-level style sheets(internal ) apply to the whole body of a
document.
• External style sheets can apply to the bodies of any number of
documents.
Inline CSS
• An inline CSS is used to apply a unique style to a single HTML element.
• An inline CSS uses the style attribute of an HTML element.
Example: inline.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
</body>
</html>
Document-level style sheets or Internal CSS
• An internal CSS is used to define a style for a single HTML page.
• An internal CSS is defined in the <head> section of an HTML page,
within a <style> element.
Example: internal.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body {background-color: blue;}
h1 {color: yellow;}
p {color: red;}
</style>
</head>
<body>
<h1>welcome</h1>
<p>hi</p>
<h1>Web Programming</h1>
<p>Bangalore</p>
</body>
</html>
External CSS
• An external style sheet is used to define the style for many HTML pages.
• To use an external style sheet, add a link to it in the <head> section of each
HTML page:
• The external style sheet can be written in any text editor.
• The file must not contain any HTML code, and must be saved with a .css
extension.
• It uses the <link> tag on every pages and the <link> tag should be put inside
the head section.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
<h1><center>BANGALORE</center></h1>
<p><b><center>HYDERABAD</center></b></p>
</body>
</html>
Syntax:
• <link rel="stylesheet" type="text/css" href="style.css" />
Example: external.xhtml
• The REL attribute is used to define the relationship between the linked file
and the HTML document. REL=StyleSheet specifies
a persistent or preferred style.A persistent style is one that is always applied
when style sheets are enabled.
• (Hypertext REFerence) The HTML code used to create a link to another
page
CSS Selectors
• 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.
• There are several different types of selectors in CSS.
CSS Element Selector
CSS Id Selector
CSS Class Selector
CSS Universal Selector
CSS Group Selector
The CSS element Selector
• The element selector selects HTML elements based on the element name.
Example: element.xhtml
• Here, all <p> elements on the page will be center-aligned, with a red text
color:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
The CSS id Selector
• The id selector uses the id attribute of an HTML element to select a
specific element.
• The id of an element is unique within a page, so the id selector is used to
select one unique element.
• To select an element with a specific id, write a hash (#) character,
followed by the id of the element.
Example: id.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</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: class.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
.mca {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="mca">MANGALORE</h1>
<p class="mca">BANGALORE</p>
</body>
</html>
The CSS Universal Selector
• The universal selector (*) selects all HTML elements on the page.
• Example: universal.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
*{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<p>WELCOME</p>
<p >MCA</p>
<p>DEPARTMENT</p>
</body>
</html>
The CSS Grouping Selector
• The grouping selector selects all the HTML elements with the same style
definitions.
• Look at the following CSS code (the h1, h2, and p elements have the same
style definitions):
Example: group.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
h1 { text-align: center; color: red; }
h2 {text-align: center; color: green; }
p {text-align: center; color: blue; }
</style>
</head>
<body>
<h1>Hello </h1>
<h2>HI</h2>
<p>WELCOME</p>
</body>
</html>
• It will be better to group the selectors, to minimize the code.
• To group selectors, separate each selector with a comma.
• Example:group1.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
h1, h2, p {text-align: center; color: red; }
</style>
</head>
<body>
<h1>Hello</h1>
<h2>HI</h2>
<p>WELCOME</p>
</body>
</html>
• CSS Font Properties
• CSS Font size: This property is used to increase or decrease the size of the
font.
• CSS Font style: This property is used to make the font bold, italic or oblique
• CSS Font weight: This property is used to increase or decrease the boldness
and lightness of the font.
CSS Font Color
• CSS font color is a standalone attribute in CSS
• It is used to change the color of the text.
• There are three different formats to define a color:
By a color name
By hexadecimal value
By RGB
Example: font.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body {
font-size: 200%;
}
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>
CSS Font Family
In CSS there are five generic font families:
• Serif fonts have a small stroke at the edges of each letter. They create a
sense of formality and elegance.
• Sans-serif fonts have clean lines (no small strokes attached). They create a
modern and minimalistic look.
• Monospace fonts - here all the letters have the same fixed width. They
create a mechanical look.
• To shorten the code, it is also possible to specify all the individual font
properties in one property.
• font-style
• font-variant
• font-weight
• font-size/line-height
• font-family
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Font Properties</title>
<style type = "text/css">
.one {font-family: 'lucida calligraphy'; font-weight:bold; font-size:30pt; color: purple; }
.two { font-family: 'Arial'; color: green; }
.three { font:'times new roman'; }
</style>
</head>
<body>
<p class = "one">MASTER OF COMPUTER APPLICATIONS</p>
<h1 class = "two">MASTER OF COMPUTER APPLICATIONS</h1>
<p class = "three">MASTER OF COMPUTER APPLICATIONS</p>
</body>
</html>
Pseudo Classes:
• Pseudo class selectors are used if the properties are to be changed
dynamically.
• For example: when mouse movement happens, in other words, hover
happens or focus happens.
Example:pseudo.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample CSS</title>
<style type = "text/css">
input:hover
{ font-family: 'Gabriola'; color: red; font-size:100; }
</style>
</head>
<body>
<form>
NAME: <input type = "text" />
</form>
</body>
</html>
LIST PROPERTIES
In HTML, there are two main types of lists:
• unordered lists (<ul>) - the list items are marked with bullets
• ordered lists (<ol>) - the list items are marked with numbers or letters
CSS Syntax
color: color|initial|inherit;
Property Values
Value Description
color Specifies the text color.
initial Sets this property to its default value. inherit
Inherits this property from its parent element.
We can define the color of an element by using the following ways:
• Hexadecimal notation.
example: body {color: #92a8d1;}
• HSL(Hue, Saturation, and Lightness).
example: body {color: hsl(89, 43%, 51%);}
Example: color.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body
{color: red;}
h1 {color: #00ff00;}
.ex {color: rgb(0,0,255);}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<p><pre>hello.</pre></p>
<p class="ex">welcome.</p>
</body>
</html>
Text Property
• CSS has a lot of properties for formatting text.
Text Color
Text Alignment
Text Decoration
Text Transformation
Text Shadow
Text Spacing
Text Color
• The color property is used to set the color of the text.
Example: textcolor.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
body {color: blue;}
h1 {color: green;}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<p>HI</p>
</body>
</html>
Text Alignment
• The text-align property is used to set the horizontal alignment of a text.
• A text can be left or right aligned, centered, or justified.
Example: textalign.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
h1 {text-align: center;}
h2 {text-align: left;}
h3 {text-align: right;}
</style>
</head>
<body>
<h1>MSc</h1>
<h2>MCA </h2>
<h3>MBA</h3>
</body>
</html>
Text Decoration:
• The text-decoration property is used to specify some special features of
text.
• The available values are line-through, overline, underline, and none,
which is the default.
Example :decoration.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Text Decoration</title>
<style >
.one{ text-decoration: line-through;}
.two{ text-decoration: overline; }
.three{text-decoration: underline;}
</style>
</head>
<body>
<h1 class = "one">MCA</h1> <br/>
<h1 class = "two">MCA</h1> <br/>
<h1 class = "three">MCA</h1><br/>
</body>
</html>
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 :trans.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
.uppercase { text-transform: uppercase; }
.lowercase{ text-transform: lowercase; }
.capitalize { text-transform: capitalize;}
</style>
</head>
<body>
<h1>Using the text-transform property</h1>
<p class="uppercase">hi</p>
<p class="lowercase">HELLO</p>
<p class="capitalize">welocome to mca</p>
</body>
</html>
Text Shadow
• The text-shadow property adds shadow to text.
• In its simplest use, you only specify the horizontal shadow (2px) and the
vertical shadow (2px).
Example: shadow.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
h1 {
text-shadow: 2px 2px;
}
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
Text Spacing
the following are text spacing properties:
• 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: (indent.xhtml)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
p {text-indent: 150px; }
</style>
</head>
<body>
<h1>Using text-indent</h1>
<p><pre>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.'</pre></p>
</body>
</html>
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: letter.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
h2 {letter-spacing: 25px;}
h3 {letter-spacing: 20px;}
</style>
</head>
<body>
<h1>Using letter-spacing</h1>
<h2>This is heading 1</h2>
<h3>This is heading 2</h3>
</body>
</html>
Line Height
• The line-height property is used to specify the space
between lines:
Example: line.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
.small{line-height: 3;}
.big {line-height: 1.8;}
</style>
</head>
<body>
<p class="small"><pre>
This is a paragraph with a smaller line-height.
This is a paragraph with a smaller line-height.</pre>
</p>
<p class="big"><pre>
This is a paragraph with a bigger line-height.
This is a paragraph with a bigger line-height.</pre>
</p>
</body>
</html>
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: wordspace.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
.one{word-spacing: 10px;}
.two { word-spacing: 23px;}
</style>
</head>
<body>
<p class="one">This is a paragraph with larger word spacing.</p>
<p class="two">This is a paragraph with smaller word spacing.</p>
</body>
</html>
white-space
• The white-space property specifies how white-space inside an element is
handled.
• This example demonstrates how to disable text wrapping inside an element.
• The following example demonstrates how white space inside an element is
handled. Possible values are normal, pre.
Example:white.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<p style = "white-space:normal">
This text has a line break and the white-space pre setting
tells the browser to honor it just like the XHTML pre tag.
</p>
</body>
</html>
The CSS Box Model
• Each element in an HTML document is treated as a rectangular box by
CSS.
• This is the default layout scheme and can be customized as per our
requirements.
• The positioning of elements, their content, and their surrounding
elements are done following the box model of CSS.
• Content
This includes the actual data in the form of text, image or other media
content.
The width and height properties modify the dimensions of this box.
• Padding
The space between the outer edge of the content and its border refers to
padding.
This box can be resized by the padding property.
Edge-specific properties like padding-left, padding-bottom, etc. help in
achieving custom spacing.
• Border
The distance between the outer edge of the padding and the inner edge of
the margin defines the border of an element.
By default, its width is set to 0.
The border property is used to define an element’s border.
Individual edges can also be styled.
• Margin
The space between an element’s box and its surrounding elements’ box is
defined as margin.
Example:boxmodel.html
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey; width: 300px; border: 15px solid green; padding: 50px;margin: 20px;
}
</style>
</head>
<body>
<h2>Demonstrating the Box Model</h2>
<p>hi</p>
<div>welcome</div>
</body>
</html>
CSS Borders
• The CSS border properties allow you to specify the style, width, and color
of an element's border.
• The border-style property can have from one to four values (for the top
border, right border, bottom border, and the left border).
The following values are allowed:
dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double border
groove - Defines a 3D grooved border. The effect depends on the border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color value
none - Defines no border
hidden - Defines a hidden border
example:boder.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
p.dotted {border-style: dotted;} p.dashed {border-style: dashed;} p.solid {border-style: solid;}
</style>
</head>
<body>
<h2>The border-style Property</h2>
<p>This property specifies what kind of border to display:</p>
<p class="dotted">A dotted border.</p>
<p class="dashed">A dashed border.</p>
<p class="solid">A solid border.</p>
</body>
</html>
CSS Border Width
• The border-width property specifies the width of the four borders.
• The width can be set as a specific size (in px, pt, cm, em, etc) or by using
one of the three pre-defined values: thin, medium, or thick:
• example:borderwidth.xhtml
<!DOCTYPE html>
<html>
<head>
<style>
p.one { border-style: solid; border-width: 5px; }
p.two { border-style: solid; border-width: medium; }
</style>
</head>
<body>
<h2>The border-width Property</h2>
<p class="one">Some text.</p>
<p class="two">Some text.</p>
</body>
</html>
• CSS Border Color
• The border-color property is used to set the color of the four borders.
• The color can be set by:
<head>
<style>
div {
border: 1px solid black; margin-top: 100px; margin-bottom: 100px;
margin-right: 150px; margin-left: 80px; background-color: lightblue;
}
</style>
</head>
<body>
<div>welcome.</div>
</body>
</html>
• CSS Padding
• Padding is used to create space around an element's content, inside of
any defined borders.
• Padding - Individual Sides
padding-top
padding-right
padding-bottom
padding-left
• All the padding properties can have the following values:
length - specifies a padding in px, pt, cm, etc.
% - specifies a padding in % of the width of the containing element
example: padding.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
div {
border: 1px solid black; padding-top: 50px; padding-right: 30px;
padding-bottom: 50px; padding-left: 80px;
}
</style>
</head>
<body>
<div>welcome </div>
</body>
</html>
CSS background properties:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
• background (shorthand property)
CSS background-color
The background-color property specifies the background color of an
element.
Example
body { background-color: lightblue; }
Opacity / Transparency
The opacity property specifies the opacity/transparency of an element.
It can take a value from 0.0 - 1.0.
The lower value, the more transparent:
Example
div { background-color: green; opacity: 0.3;}
CSS background-image
The background-image property specifies an image to use as the
background of an element.
THE <span> AND <div> TAGS
span tag
• A <span> element which is used to color a part of a text
• The <span> tag is an inline container used to mark up a part of a text, or a
part of a document.
• The <span> tag is easily styled by CSS or manipulated with JavaScript using
the class or id attribute.
• The <span> tag is much like the <div> element, but <div> is a block-level
element and <span> is an inline element.
Example:
<!DOCTYPE html>
<html>
<body>
<p>hi <span style="color:blue;">hello</span></p>
</body>
</html>
div tag
• The <div> tag defines a division or a section in an HTML document.
• The <div> tag is used as a container for HTML elements - which is then
styled with CSS or manipulated with JavaScript.
• The <div> tag is easily styled by using the class or id attribute.
• Any sort of content can be put inside the <div> tag!
Example: divtag.xhtml