Cascading Style Sheets (CSS) : CSS Is Used To Control The Style of A Web Document in A Simple and Easy Way
Cascading Style Sheets (CSS) : CSS Is Used To Control The Style of A Web Document in A Simple and Easy Way
CSS is used to control the style of a web document in a simple and easy way.
CSS describes the appearance, layout, and presentation of information on a web page
HTML describes the content of the page
CSS describes how information is to be displayed, not what is being displayed
Can be embedded in HTML document or placed into separate .css file
CS380
2 CSS stands for “Cascading Style Sheets”
Sheets: the “sheets” are like templates, or a set of rules, for determining how the webpage will
look.
Cascading: refers to the procedure that determines which style will apply to a certain section,
if you have more than one style rule.
Style: how you want a certain part of your page to look. You can set things like color,
margins, font, etc for things like tables, paragraphs, and headings.
Applications of CSS
CSS saves time-You can write CSS once and then reuse same sheet in multiple HTML pages. You can define a
style for each HTML element and apply it to as many Web pages as you want.
Pages load faster - If you are using CSS, you do not need to write HTML tag attributes every time. Just write
one CSS rule of a tag and apply it to all the occurrences of that tag. So less code means faster download times.
Easy maintenance - To make a global change, simply change the style, and all elements in all the web pages
will be updated automatically.
Superior styles to HTML - CSS has a much wider array of attributes than HTML, so you can give a far better
look to your HTML page in comparison to HTML attributes.
Multiple Device Compatibility - Style sheets allow content to be optimized for more than one type of device.
By using the same HTML document, different versions of a website can be presented for handheld devices such
as PDAs and cell phones or for printing.
Global web standards - Now HTML attributes are being deprecated and it is being recommended to use CSS.
So its a good idea to start using CSS in all the HTML pages to make them compatible to future browsers.
Basic CSS rule syntax or Style Specification Format
4
Format1
selector {
property: value;
property: value;
...
property: value;
} CSS
p {
font-family: sans-serif;
color: red;
} CSS
A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in
your document. A style rule is made of three parts −
Selector − A selector is an HTML tag to which a style will be applied. This could be any tag like <h1> or <table> etc.
Property − A property is a type of attribute of HTML tag. Put simply, all the HTML attributes are converted into CSS
properties. They could be color, border etc.
Value − Values are assigned to properties. For example, color property can have value either red or #F1F1F1 etc.
Format 2
5 example
<p style="font-size: 10pt; color: red; font-weight: bold; font-family: Arial;">
Format-3
Example:
<html>
<head>
<title>Title</title>
<style type="text/css">
<!--[STYLE INFORMATION GOES HERE] -->
</style>
</head>
<body>
[DOCUMENT BODY GOES HERE]
</body>
</html>
Levels of Style Sheets (or) Types of CSS
6
Example
<p style="font-size: 10pt; color: red; font-weight: bold; font-family: Arial, Helvetica, sans-serif">
This is a local stylesheet declaration. </p>
On the browser:
Example of inline
<html>
<body>
<h1 style="color:blue;">A Blue Heading</h1>
<p style="color:red;">A red paragraph.</p>
</body>
</html>
10 2. Global
Example:
<html>
<head>
<title>Title</title>
<style type="text/css">
<!--[STYLE INFORMATION GOES HERE] -->
</style>
</head>
<body>
[DOCUMENT BODY GOES HERE]
</body>
</html>
Example of Internal
<html>
<head>
<style type="text/css"> >
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
13 3. Linked
CLASS SELECTOR Using Class Selector we can assign different styles to the same
element.
These different styles appear on different occurrences of that element.
For example
H1.RedText { font-size:20pt; color:red; }
<body>
<h1 class=“RedText”>Heading</h1>
</body>
GENERIC SELECTOR
We define the class in generalized form, that particular class can be applied to any tag.
For example
.RedText { font-size:20pt; color:red; }
<body>
<h1 class=“RedText”>Heading</h1>
</body>
ID SELECTOR
To define a special style for special element we can use “id Selector”. The id Selector is
similar to the Class Selector.
Syntax:-
#nameof_id{property:value list;}
For example
#RedText { font-size:20pt; color:red; }
<body> <h1 id=“RedText”>Heading</h1>
</body>
UNIVERSAL SELECTOR
This Selector is denoted by ”*”.
This selector applied to all the elements in the document.
For example
* { font-size:20pt; color:red; }
PSEUDO CLASSES
Using Pseudo classes we can give special effects on the selector.
There some pseudo classes which are more commonly used
• Hover-it applies when its associated element has the mouse cursor over it.
• Focus-it applies when its associated element has focus.
Syntax:-
selector:pseudo-class {property:value list;}
Example of pseudo-class
Example-2
Example-1
<html>
<html>
<head>
<head> <style>
<style> input:hover{color:red;}
p:hover{color:red;} input:focus{color:green;}
p:focus{color:green;} </style>
</head>
</style>
<body>
</head>
<form>
<body>
<label>enter name:
<p>hi welcome to html</p> <input type="text"></label>
<body> </form>
</html> <body>
Anchor Pseudo-classes
Links can be displayed in different ways:
<html><head> <body>
CS380
26 font-family
p {
font-family: Georgia;
}
h2 {
font-family: "Courier New";
} CSS
CS380
27 More about font-family
p {
font-family: Garamond, "Times New Roman", serif;
} CSS
CS380
29 font-size
p {
font-size: 24pt;
} CSS
CS380
30 font-weight, font-style
p {
font-weight: bold;
font-style: italic;
} CSS
Either of the above can be set to normal to turn them off (e.g.
headings)
CS380
Font Shorthands
To shorten the code, it is also possible to specify all the individual font properties in one property.
The font property is a shorthand property for:
font-style
font-variant
font-weight
font-size/line-height
font-family
Example
Use font to set several font properties in one declaration:
p.a {
font: 20px Arial, sans-serif;
}
p.b {
font: italic small-caps bold 30px Georgia, serif;
}
Example of font
<html>
<head>
<style type="text/css">
p.a{font:20px Arial;}
p.b{font:italic small-caps bold 30px Georgia;}
</style>
</head>
<body>
<p class="a">hi iam using style of a class</p>
<p class="b">hi iam using style of b class</p>
</body>
</html>
33 text-align & text-decoration
h2 { text-align: center; }
p {
text-decoration: underline;
}
CS380
Text Color and Background Color
In this example, we define both the background-color property and the color property:
Example
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
Text Direction & Text Transformation
The direction and unicode-bidi properties can be used to change the text direction of an element:
Example
p {
direction: rtl;
unicode-bidi: bidi-override;
}
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 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.
Example
h1 {
letter-spacing: 3px;
}
h2 {
letter-spacing: -3px;
}
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):
Text shadow effect!
Example
h1 {
text-shadow: 2px 2px;
}
Next, add a color (red) to the shadow:
Example
h1 {
text-shadow: 2px 2px red;
}
38 2. The list-style-type property
ol { list-style-type: lower-roman; }
ul { list-style-type: square;}
CSS
Possible values:
i. none : No marker
ii. disc (default), circle, square
iii. Decimal: 1, 2, 3, etc.
iv. decimal-leading-zero: 01, 02, 03, etc.
v. lower-roman: i, ii, iii, iv, v, etc.
vi. upper-roman: I, II, III, IV, V, etc.
vii. lower-alpha: a, b, c, d, e, etc.
viii. upper-alpha: A, B, C, D, E, etc.
x. lower-greek: alpha, beta, gamma, etc.
CS380
Example of list style
<html> <ul class="b">
<head> <li>Coffee</li>
<style> <li>Tea</li>
<li>Coca Cola</li>
ul.a {list-style-type: circle;}
</ul>
ul.b { list-style-type: square;}
<p>Example of ordered lists:</p>
ol.c {list-style-type: upper-roman;}
<ol class="c">
ol.d {list-style-type: lower-alpha;} <li>Coffee</li>
</style> <li>Tea</li>
</head> <li>Coca Cola</li>
<body> </ol>
<h2>Lists</h2> <ol class="d">
<p>Example of unordered lists:</p> <li>Coffee</li>
<ul class="a"> <li>Tea</li>
<li>Coffee</li> <li>Coca Cola</li>
<li>Tea</li> </ol>
<li>Coca Cola</li></ul> </body>
</html
40 3.CSS properties for colors
p {
color: red;
background-color: yellow;
}
CSS
property description
color color of the element's text
color that will appear behind the
background-color
element
CS380
41 Specifying colors
p { color: red; }
h2 { color: rgb(128, 0, 196); }
h4 { color: #FF8800; }
CSS
CS380
43 background-image
body {
background-image: url("images/draft.jpg");
}
CSS
CS380
44 background-repeat
body {
background-image: url("images/draft.jpg");
background-repeat: repeat-x;
}
CSS
CS380
45 background-position
body {
background-image: url("images/draft.jpg");
background-repeat: no-repeat;
background-position: 370px 20px;
} CSS
value consists of two tokens, each of which can be top, left, right,
bottom, center, a percentage, or a length value in px, pt, etc.
value can be negative to shift left/up by a given amount
CS380
46 Grouping selectors
CS380
CSS Box Model
In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of:
margins, borders, padding, and the actual content. The image below illustrates the box model:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between
elements.
CSS Margins
The CSS margin properties are used to generate space around elements, OUTSIDE of any
defined border.
The margins are completely transparent - and cannot have a background color.
CSS has properties for specifying the margin for each side of an element:
• margin-top
• margin-right
• margin-bottom
• margin-left
All the margin properties can have the following values:
• auto - the browser calculates the margin.
• length - specifies a margin in px, pt, cm, etc.
• % - specifies a margin in % of the width of the containing element.
• inherit - specifies that the margin should be inherited from the parent element.
Example of margins
Set different margins for all four sides of a <p> element:
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
Margin - Shorthand Property
So, here is how it works:
p { margin: 100px 150px 100px 80px; }
If the margin property has four values:
margin: 20px 20px 55px 200px; – top margin is 20px – right margin is 20px – bottom margin is 55px – left margin is 200px
If the margin property has three values:
margin: 27px 10px 35px; – top margin is 27px – right and left margins are 10px – bottom margin is 35px
If the margin property has two values:
margin: 45px 70px; – top and bottom margins are 45px – right and left margins are 70px
If the margin property has one value:
margin: 25px; – all four margins are 25px
Use of The auto Value
You can set the margin property to auto to horizontally center the element within its container.
CSS Borders
The CSS border properties allow you to specify the style, width, and color of an element's border.
The border-style property specifies what kind of border to display.
The following values are allowed:
1. dotted - Defines a dotted border
2. dashed - Defines a dashed border
3. solid - Defines a solid border
4. double - Defines a double border
5. groove - Defines a 3D grooved border. The effect depends on the border-color value
6. ridge - Defines a 3D ridged border. The effect depends on the border-color value
7. inset - Defines a 3D inset border. The effect depends on the border-color value
8. outset - Defines a 3D outset border. The effect depends on the border-color value
9. none - Defines no border
10. hidden - Defines a hidden border
Example
p {border-style: dotted;}
p{border-style: solid;}
CSS Border Width
The border-width property specifies the width of the four borders.
CSS Border Color
The border-color property is used to set the color of the four borders.
Example
p.one {
border-style: solid;
border-width: 5px;
border-color: red;
}
p.two {
border-style: solid;
border-width: medium;
border-color: blue;
}
p.three {
border-style: dotted;
border-width: 2px;
border-color: green;
}
p.four {
border-style: dotted;
border-width: thick;
border-color: yellow;
}
CSS Border Sides
CSS Border - Individual Sides
From the examples on the previous pages, you have seen that it is possible to specify a
different border for each side.
In CSS, there are also properties for specifying each of the borders (top, right, bottom, and
left):
Example
p {
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
CSS Border - Shorthand Property
Like you saw in the previous page, there are many properties to consider when dealing with borders.
To shorten the code, it is also possible to specify all the individual border properties in one property.
The border property is a shorthand property for the following individual border properties:
1. border-width
2. border-style (required)
3. border-color
Example
p {
border: 5px solid red;
}
CSS Padding
Padding is used to create space around an element's content, inside of any defined borders.
The padding property is a shorthand property for the following individual padding properties:
1. padding-top
2. padding-right
3. padding-bottom
4. padding-left
Example
Use the padding shorthand property with four values:
p {
padding: 25px 50px 75px 100px;
}
P{margin: 20px 20px 55px 200px; border: 5px solid red; padding: 25px 50px 75px 100px;}
HTML <span> Tag
The <span> tag is an inline container used to mark up a part of a text or a part of a document.
Example
A <span> element which is used to color a part of a text:
<p>My mother has <span style="color:blue;font-weight:bold">blue</span> eyes and my father
has <span style="color:darkolivegreen;font-weight:bold">dark green</span> eyes.</p>
Example
<html><head><style type=“text/css”>
.spanred{font-size:24pt;color:red;font-family:ariel;}</style></head>
<body>
<p>this is the <span class=“spanred”>span text</span></p>
</body></html>
HTML <div> Tag
<div> tag is used to apply a style to a section of a document.
Example
<html><head><style>
.myDiv {
border: 5px outset red;
background-color: lightblue;
text-align: center;}
</style></head>
<body>
<h1>The div element</h1>
<div class="myDiv">
<h2>This is a heading in a div element</h2>
<p>This is some text in a div element.</p>
</div>
<p>This is some text outside the div element.</p>
</body>
</html>
End of Unit-2