Unit- II CSS.pptx
Unit- II CSS.pptx
Definition
• CSS stands for Cascading Style Sheets
• CSS describes how HTML elements are to be displayed on screen,
paper, or in other media
• CSS saves a lot of work. It can control the layout of multiple web
pages all at once
• External stylesheets are stored in CSS files
Syntax for CSS
A CSS rule-set consists of a selector and a declaration block:
<head>
<link rel="stylesheet" type="text/css" href="my
style.css">
</head>
An external style sheet can be written in any text editor. The file should not contain any html tags.
The style sheet file must be saved with a .css extension.
Here is how the "mystyle.css" file looks like:
“mystyle.css“
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Internal Style Sheet
An internal style sheet may be used if one single page has a unique style.
Example
Internal styles are defined within the <style> element, inside the <head> section of an HTML page:
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Inline Styles
• An inline style may be used to apply a unique style for a single element.
• To use inline styles, add the style attribute to the relevant element. The style attribute can contain
any CSS property.
Example
Inline styles are defined within the "style" attribute of the relevant element:
<h1 style="color:blue;margin-left:30px;">This is a heading</h1>
CSS - Font
CSS Font Families
In CSS, there are two types of font family names:
generic family - a group of font families with a similar look (like "Serif" or "Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")
h2 {
font-size: 30px;
}
p{
font-size: 14px;
}
Set Font Size With Em
To allow users to resize the text (in the browser menu), many developers use em instead of pixels.
The em size unit is recommended by the W3C.
1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is
16px.
The size can be calculated from pixels to em using this formula: pixels/16=em
h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}
h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}
p{
font-size: 0.875em; /* 14px/16=0.875em */
}
Font Weight
The font-weight property specifies the weight of a font:
Example
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
Font Variant
The font-variant property specifies whether or not a text should be displayed in a small-caps font.
In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appears in a
smaller font size than the original uppercase letters in the text.
Example
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps; }
Font-Colors
Background Color:
You can set the background color for HTML elements:
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Text Color
Example:
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Border Color
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
Font-Colors
Color Values:
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
h1 {
color: green;
}
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.
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):
Example:
h1 {
When the text-align property is set to "justify", each line is
text-align: center; stretched so that every line has equal width, and the left and
} right margins are straight (like in magazines and newspapers):
Example
h2 {
text-align: left; div {
text-align: justify;
} }
h3 {
text-align: right;
}
Text Decoration Text Transformation
The text-decoration property is used to set or remove decorations
The text-transform property is used to specify uppercase and
from text. lowercase letters in a text.
The value text-decoration: none; is often used to remove underlines It can be used to turn everything into uppercase or lowercase
from links: letters, or capitalize the first letter of each word:
Example Example
a{ p.uppercase {
text-decoration: none; text-transform: uppercase;
} }
The other text-decoration values are used to decorate text:
p.lowercase {
Example
text-transform: lowercase;
h1 { }
text-decoration: overline;
} p.capitalize {
text-transform: capitalize;
h2 { }
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
Text Indentation Line Height
The text-indent property is used to specify the indentation of The line-height property is used to specify the space between lines:
the first line of a text: Example
p.small {
Example line-height: 0.8;
p { text-indent: 50px;} }
Letter Spacing
p.big {
The letter-spacing property is used to specify the space line-height: 1.8;
between the characters in a text. }
The following example demonstrates how to increase or Text Direction
decrease the space between characters: The direction property is used to change the text direction of an element:
p{
Example
direction: rtl;}
h1 { letter-spacing: 3px;} Word Spacing
h2 { letter-spacing: -3px;} The word-spacing property is used to specify the space between the words
in a text.
Text Shadow The following example demonstrates how to increase or decrease the
The text-shadow property adds shadow to text. space between words:
Example
The following example specifies the position of the horizontal h1 {
shadow (3px), the position of the vertical shadow (2px) and
word-spacing: 10px;
the color of the shadow (red):
}
h1 { h2 {
text-shadow: 3px 2px red;} word-spacing: -5px;
}
CSS Lists
HTML Lists and CSS List Properties
Example