1bcom_sem3_unit5_css__notes
1bcom_sem3_unit5_css__notes
A selector is used to create a link between the rule and the HTML tag. A
declaration specifies the styles to the selector. Declarations must be using
colons and terminated using semicolons
Each declaration contains two parts – Property Name and Property Value
separated by colon. A CSS declaration always ends with a semicolon, and
declaration blocks are surrounded by curly braces.
Example:
body h1
{ {
background-color: #eebd2; color: #eebd2;
} background-color: #d8a29b;
font-family: “Book Antiqua”,
Times,serief;
}
Example:
<html>
<head>
<style>
h1
{
font-family: Arial; font-style: italic; color: red;
}
</style>
</head>
<body>
<h1>this is a text with internal/embedded styles</h1>
</body>
</html>
3. External Style:
In this method, the styles are written in external file with .css extension and
referenced by html document. External style sheet are used to declare styles
separately and implement for multiple pages.
<LINK> Tag:
2
External style sheets are called using the <link> tag. . It is placed in the head
section of HTML document
Syntax:
<link rel ="stylesheet" type="text/css" href="filename.css">
Where, href – represents name and location of the external style sheet.
Example:
Step 1: open notepad and create a file with css styles. Save it with
mystyle.css
p
{
font-family:arial;
font-size:medium;
color:blue;
}
h1
{
font-family:arial black;
background-color:blue;
color:white;
}
Step 2: open notepad and type the following code. Save it with External.html
<html>
<head>
<link rel=”stylesheet” type=”text/css” href=”mystyle.css”>
</head>
<body>
<h1> external css style </h1>
<p> This is a text with external style </p>
</body>
</html>
Step 3: open the External.html file using browser.
Syntax:
<style>
.name { Style definitions }
</style>
Example:
.greenboldtext {font-size: small; color: #008080; font-weight: bold ;}
3
To reference a style class in HTML code, simply specify the class name in the
class attribute of an element
<p class=”greenboldtext”>HTML</p>
c. Including Stylesheets:
We can include stylesheets in the <head> tag using <link> and <style>
tags. The <link> tag defines the relationship between a document and an
external resource. The <link> tag is most used to link to style sheets.
Ex:
<link rel=”StyleSheet” href=”mainstyles.css “ type=”text/css”
media=”screen” >
<style type=”text/css”>
<! - - @import url(http://www.smiggins.com/style.css) -- >
</style>
Example1:
<html><head>
<style>
#box
{
width: 70%;
margin: auto;
padding: 20px;
border: 1px solid #666;
background: #ffffff;
}
</style>
<body><p>This is first <div id=box>css based web page</div>with
html</p>
</body>
</html>
Example2:
<html><body>
<p>This is some text.</p>
<div style="color:#0000FF">
<h3>This is a heading in a div element</h3>
<p>This is some text in a div element.</p>
</div>
<p>This is some text.</p>
</body> </html>
b. Span - <span>
SPAN is similar to DIV. It changes the style of the text. These are inline
tags (inline elements are span, img, a, em, strong). It is used as a
selector in style sheet and also supports class and id selectors. No line
break is created when a span is declared. The <span> element is often
used as a container for some text.
Syntax:
<span> contents</span>
Example1:
5
<html><head>
<style>
.first { color:red; }
.italic { font-style:italic;}
</style>
<body>
<p>This is first <span class=first>css based web page</span>with
html</p>
</body></html>
Example 2:
<html>
<body>
<p>My mother has <span style="color:blue;fontweight: bold">blue</span>
eyes and my father has <span style="color:darkolivegreen;font-
weight:bold">dark green</span>
eyes.</p>
</body></html>
6
PROPERTIES AND VALUES
There are mainly certain types of properties of font, color and background, text,
boxes etc.
1. FONT
a. Font-family Property
The font-family property specifies the font family for the text.
Fonts are identified by giving the name of a font. 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")
CSS allows specifying a comma-separated list of fonts to use for a particular
style.
Syntax:
Font-family: family name [Generic family]
Example:
<html><head>
<style>
<! - -
p.serif{font-family:"Times New Roman",Times,serif;}
p.sansserif{font-family:Arial,Helvetica,sans-serif;}
//- - >
</style>
</head>
<body>
<h1>CSS font-family</h1>
<p class="serif">This is a paragraph in the Times New Roman font.</p>
<p class="sansserif">This is a paragraph in the Arial font.</p>
</body>
</html>
Note: if the user does not have the specified font name it will use the same
generic family.
b. Font-size Property
The font-size property specifies the font size of text.
The possible relative values are larger, smaller and absolute values are small,
medium, and large.
Syntax:
Font-size: small | medium | large | smaller | larger |length |
percentage
Example:
<head>
7
<style>
<! - -
p.normal {font-style:normal;}
p.italic {font-style:italic;}
p.oblique {font-style:oblique;}
//- - >
</style>
</head>
<body>
<p class="normal">This is a paragraph, normal.</p>
<p class="italic">This is a paragraph, italic.</p>
<p class="oblique">This is a paragraph, oblique.</p>
</body>
</html>
c. Font-style property
The font-style property specifies font style for text. It used to set text to none,
italic or oblique.
Syntax:
Font-weight: none | italic | oblique
Example:
<html>
<head>
<style>
<! - -
h1 {font-size:250%;}
h2 {font-size:200%;}
p {font-size:100%;}
//- - >
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
</body>
</html>
d. Font-weight property
The font-weight property specifies the weight of the text.
The possible relative values are - bold, normal, bolder, lighter and absolute
values are 100, 200, 300,….., 900.
Syntax:
Font-weight: normal |bold |bolder |lighter | 100 |200 |……….| 900
Example:
<html>
<head>
<style>
<! - -
p.normal {font-weight:normal;}
p.light {font-weight:lighter;}
p.thick {font-weight:bold;}
p.thicker {font-weight:900;}
//- - >
8
</style>
</head>
<body>
<p class="normal">This is a paragraph.</p>
<p class="light">This is a paragraph.</p>
<p class="thick">This is a paragraph.</p>
<p class="thicker">This is a paragraph.</p>
</body>
</html>
2. TEXT
a. Text-align Property
The text-align property used to align the text.
The possible values are left, center, right and justify.
Syntax: Text-align: left | center | right | justify
Example:
<html>
<head>
<style>
<! - -
h1 {text-align:center}
h2 {text-align:left}
h3 {text-align:right}
//- - >
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>
</html>
b. Text-decoration property
The text-decoration property is used to set or remove decorations from text.
The possible values are none, overline, line-through and underline.
Syntax:
text-decoration : none| overline | line-through | underline
Example:
<html>
<head>
<style>
<! - -
h1 {text-decoration:overline;}
p {text-decoration:line-through;}
//- - >
</style>
</head>
<body>
<h1>This is heading 1</h1>
<p>This is a paragraph</p>
</body>
</html>
9
c. Text-transform property
The text-transform property specifies the case (or capitalization) of the text.
The possible values are none, lowercase, uppercase and capitalize
Syntax:
text-transform: none | lowercase |uppercase |capitalize
Example:
<html>
<head>
<style>
<! - -
p.uppercase {text-transform:uppercase;}
p.lowercase {text-transform:lowercase;}
p.capitalize {text-transform:capitalize;}
//- - >
</style>
</head>
<body>
<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>
</body>
</html>
c. Background-image Property
The background-image property specifies the background image.
Syntax: Background-image: none | URL (location
10
Example:
<html>
<head>
<style>
<! - -
body{background-image:url('img_tree.png');}
//- - >
</style>
</head>
<body>
<h1>Hello World!</h1>
</body></html>
Attributes:
Z-index: n
The browser maintains a stack of layers order of content. The z-index
property allows layer overlapping elements. If there are two
overlapping CSS positioned elements, the element with the higher
priority will appear on top of the other.
Position: absolute | relative
The position property changes how elements are positioned on your
page. Possible values are static, relative, absolute or fixed. It is optional
and defaults to absolute.
Left: n, Top: n
Left – display position from left.
Top - Display position from top.
Width: n, Height: n
Height - Specify the height of an element to display content.
Width - Specify the width of an element to display content.
Example:
<html>
<head>
<title>Absolute Positioning</title>
<style type = "text/css">
.background_image
{
position: absolute; top: 0px; left: 0px; z-index: 1;
}
.foreground_image
{
position: absolute; top: 25px; left: 100px; z-index: 2;
11
}
.text
{
position: absolute; top: 25px; left: 100px; z-index: 3;font-size: 20pt;
}
</style>
</head>
<body>
<p>
<img src="background.jpg" class="background_image" alt = "First positioned
image" height=200 width=200><br>
<img src="foreground.jpg" class="foreground_image" alt = "Second positioned
image" height=200 width=200>
</p>
<p class = "text">Sample Text</p></body></html>
CSS Borders:
The CSS border properties allow us to specify the style, width, and color of an
element's border.
The CSS border properties are used to determine the style, variety, width, and
flow of the borders of a component. These properties include:
1) CSS border-style:
The Border style property is used to specify the border type or style.
The border-style property contains four values (for the top border, right border,
bottom border, and the left border).
Ex:
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}
12
2) CSS border-width
The border-width property is used to set the border's width
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
Ex:
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
3) CSS border-color
The border-color property is used to set the color of the four borders.
p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: dotted;
border-color: blue;
}
Line Height:
The CSS line height property is used to define the minimal height of line boxes
within the element. It sets the differences between two lines of your content.
CSS line-height property values are
value description
numbe It specifies a number that is multiplied with the current font size
13
r to set the line height.
CSS Padding:
CSS Padding property is used to define the space between the element content
and the element border.
CSS padding is affected by the background colors. It clears an area around the
content.
CSS Padding Properties
Property Description
14
padding-left It is used to set left padding of an element.
Example
<html> <head>
<style>
p{
background-color: pink;
}
p.padding {
padding-top: 50px;
padding-right: 100px;
padding-bottom: 150px;
padding-left: 200px;
}
</style>
</head>
<body>
<p>This is a paragraph with no specified padding.</p>
<p class="padding">This is a paragraph with specified paddings.</p>
</body>
</html>
CSS Margin:
CSS Margin property is used to define the space around elements. It is
completely transparent and doesn't have any background color. It clears an
area around the element.
15
margin-left it is used to set left margin of an element.
length It is used to specify a margin pt, px, cm, etc. its default value is
0px.
Example:
<html> <head>
<style>
p{
background-color: pink;
}
p.ex {
margin-top: 50px;
margin-bottom: 50px;
margin-right: 100px;
margin-left: 100px;
}
</style>
</head>
<body>
<p>This paragraph is not displayed with specified margin. </p>
<p class="ex">This paragraph is displayed with specified margin.</p>
</body>
</html>
Example:
<head>
<style>
p{
width: 200px;
height: 300px;
padding: 15px;
border: 10px solid red;
margin: 5px;
}
</style>
</head>
<body>
<p>Content</p>
</body>
</html>
17
18
19