Ch 04 CSS Notes
Ch 04 CSS Notes
Learning objectives:
After the Completion of this unit you should be able to know
What a style sheet is and how it actually styles a web
page The importance of CSS.
How to create a style sheet and link an html document
to the style sheet.
The basic building blocks of any style sheet: rules,
selectors, properties and values.
How External Style Sheets are stored in CSS files
Three ways of inserting a style sheet
Different selectors using CSS
Define background properties ,Background colour &
images handling
Font family, Style and Size using CSS
Designing Tables using CSS
Structure
3.1.1 Definition
3.1.2 Advantages
The cascade part of CSS means that more than one style
sheet can be attached to a document, and all of them can
influence the presentation. For example, a designer can have
a global style sheet for the whole site, but a local one for say,
controlling the link color and background of a specific page.
Or, a user can use own style sheet if s/he has problems seeing
the page, or if s/he just prefers a certain look.
3.2 CSS Syntax
Selector {property:
Selector
{
property1: some value;
property2: some value;
}
The declaration contains the property and value for the
selector. The property is the attribute you wish to change and
each property can take a value. The property and value are
separated by a colon and surrounded by curly braces:
body { background-color: black}
If the value of a property is more than one word, put quotes
around that value: body { font-family: "sans serif"; } If you
wish to specify more than one property, you must use a semi-
colon to separate each property. This rule defines a paragraph
that will have blue text that is centered.
p { text-align: center; color: blue }
You can group selectors. Separate each selector with a
comma. The example below groups headers 1, 2, and 3 and
makes them all yellow. h1, h2, h3 { color: yellow}
#welcome
{ color:red; text-
align:center;
}
3.3.3 The Class selectors
The class selector is used to specify a style for a group
of elements. Unlike the id selector, the class selector is
most often used on several elements. This allows you to
set a particular style for many HTML elements with the
same class. The class selector uses the HTML class
attribute, and is defined with a ".". In the example
below, all HTML elements with class="center" will be
center-aligned:
Imagine within the body element of our html page, we
have the following header element
<h2
class=”center”>Summary</h2>
We can then create a CSS rule with the class selector:
.center {text-align:center;}
You can also specify that only specific HTML elements
should be affected by a class. In the example below, all
p elements with class="center" will be center-aligned:
example
p.center {text-align:center;}
Some of the other selectors are used in CSS, they
are :
3.3.3.1 Universal selector
An asterisk (*) is the universal selector for CSS. It
matches a single element of any type. Omitting the
asterisk with simple selectors has the same effect. For
instance, *.warning and. warning are considered equal.
Rather than selecting elements of a specific type, the
universal selector quite simply matches the name of any
element type: Example-
*
{
Color:#000000;
}
This rule renders the content of every element in our
document in black.
3.3.3.2 Attribute Selector
You can also apply styles to HTML elements with
particular attributes. The style rule below will match all
the input elements having a type attribute with a value
of text:
input[type="
text"] {
a paragraph.</p>
3.5 Background image handling
The background-image property specifies an image to use as
the background of an element. By default, the image is
repeated so it covers the entire element. The background
image for a page can be set like this:body
{backgroundimage:url('paper.gif');}
Example
<html>
<head>
<Title>This is my Internal css
page</Title>
<style
type="text/css">
body
{ background-
image:url
( "C:/Users/SAI/Desktop/Desktop/100MSDCF/ 11.
jpg");
}
</style>
</head>
<body>
Background Image
</body>
</html>
body {background-
color:#b0c4de;} <p
style="background-
color:yellow;">
This text has a yellow background color. </p>
<p style="text-indent:1cm;">
This text will have first line indented by 1cm and
this line will remain at its actual position this is
done by CSS text-indent property.
</p>
Example
:
3.8 Font Management using CSS
<p style="font-family:georgia,garamond,serif;">
This text is rendered in either georgia, garamond, or the
default serif font depending on which font you have at your
system. </p>
Usually, all these properties are kept in the header part of the
HTML document. Remember a:hover MUST come after
a:link and a:visited in the CSS definition in order to be
effective. Also, a:active MUST come after a:hover in the CSS
definition as follows:
<style type="text/css">
a:link {color:
#000000} a:visited
{color: #006600}
a:hover {color:
#FFCC00} a:active
{color: #FF00CC}
</style>
<style type="text/css">
a:link
{color:#000000}
</style>
<style type="text/css">
a:visited {color:
#006600}
</style>
<a href="/html/index.htm">Click this link</a>
<style type="text/css">
a:active {color:
#FF00CC}
</style>
Value Description
None NA
disc A filled-in
(default) circle
Circle An empty
circle
Square A filled-in
square
Here are the values, which can be used for an ordered list:
Here is an example:
Value Description
None NA
Inside If the text goes onto a second line, the text will
wrap underneath the marker. It will also appear
indented to where the text would have started if
the list had a value of outside.
Outside If the text goes onto a second line, the text will
be aligned with the start of the first line (to the
right of the bullet).
Example :
<html>
<head>
<Title>This is my inline css page</Title>
</head>
<body>
<ul style="list-style-type:circle; list-
stlyeposition:outside;"><li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ul style="list-style-type:square;list-
styleposition:inside;"><li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol style="list-style-type:decimal;list-
stlyeposition:outside;"><li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol style="list-style-type:lower-alpha;list-style-
position:inside;">
<li>Maths</li>
<li>Social Science</li><li>Physics</li>
</ol>
</body>
</html>
This property can have two values collapse and separate. The
following example uses both the values:
<html>
<head>
<Title>This is my inline css page</Title>
</head>
<body>
<style type="text/css">
table.one
{border-collapse:collapse;}
table.two
{border-collapse:separate;}
td.a {
border-
style:dotted;
border-
width:3px;
border-
color:#000000;
padding: 10px;
}
td.b
{
border-
style:solid;
border-
width:3px;
border-color:#333333;
padding:10px;
}
</style>
<table class="one">
<caption>Collapse Border Example</caption>
<tr><td class="a"> Cell A Collapse
Example</td></tr>
<tr><td class="b"> Cell B Collapse
Example</td></tr>
</table><br />
<table class="two">
<caption>Separate Border Example</caption>
<tr><td class="a"> Cell A Separate
Example</td></tr>
<tr><td class="b"> Cell B Separate
Example</td></tr>
</table>
</body>
</html>
<style type="text/css">
table.on
e
{
border-collapse:separate; width:400px;
border-spacing:10px;
}
table.tw
o{
border-collapse:separate; width:400px;
border-spacing:10px 50px;
}
</style>
<table class="one" border="1">
<caption>Separate Border Example with
border-spacing</caption>
<tr><td> Cell A Collapse Example</td></tr>
<tr><td> Cell B Collapse Example</td></tr>
</table><br />
<table class="two" border="1">
<caption>Separate Border Example with
border-spacing</caption>
<tr><td> Cell A Separate Example</td></tr>
<tr><td> Cell B Separate Example</td></tr>
</table>
Example:
<html>
<head>
<Title>This is my inline css page</Title>
</head>
<body>
<style type="text/css">
table.empty
{ width:350px; border-
collapse:separate;
empty-cells:hide;
}
td.empty
{ padding:5px
; border-
style:solid;
border-
width:1px;
border-
color:#999999
;}
</style>
<table class="empty">
<tr>
<th></th>
<th>Title one</th>
<th>Title two</th>
</tr>
<tr>
<th>Row Title</th>
<td class="empty">value</td><td class="empty">value</td></tr>
<tr>
<th>Row Title</th>
<td class="empty">value</td><td class="empty"></td>
</tr>
</table>
</body>
</html>
The height and width properties allow you to set the height and
width for boxes. They can take values of a length, a percentage,
or the keyword auto. Here is an
example:
You can individually change the style of the bottom, left, top,
and right borders of an element using the following properties:
Here is an example:
Answer
Answer
Universal selector
An asterisk (*) is the universal selector for CSS. It matches a
single element of any type. Omitting the asterisk with simple
selectors has the same effect. For instance, *.warning
and.warning are considered equal. Rather than selecting
elements of a specific type
Q1. How many ways to build style sheet? what are they?
Answer
There are three ways of inserting a style sheet:
1. External style sheet
2. Internal style sheet
3. Inline style
Q2. Write the background properties of CSS
Answer
disc (default),Circle,Square.
Decimal,decimal-leading-zero,lower-alpha,upper-alpha,lower-
roman,upper-roman.
Answer
Properties of dimensions of a box.
Unit -4
CSS Advanced
Learning objectives:
After the Completion of this unit you should be able to know
Paragraph Padding
Image height and width setting
Block level and inline elements
Pseudo-class and elements
Various attribute of image
Different attribute values of selectors
Structure
can).
<p>
<form>
<header>
<footer>
<section>
<div> div is the standard block-level element. A block-
level element starts on a new line and stretches out to
the left and right as far as it can. Other common block-
level elements are p and form, and new in HTML5 are
header, footer, section, and more. </div>
4.2.2 Inline Elements
An inline element does not start on a
new line and only takes up as much
CSS Syntax
Display: value;
The values of display property have the following meanings:
Block -This value causes an element to generate a block
box.
Inline-block - This value causes an element to generate an
inline-level block container. The inside of an inline-block is
formatted as a block box, and the element itself is formatted as
an atomic inline-level box.
Inline - This value causes an element to generate one or more
inline boxes.
list-item - This value causes an element (e.g., LI in HTML) to
generate a principal block box and a marker box.
None - This value causes an element to not appear in the
formatting structure
Here are some examples of the
display property: p
{ display: block }
em {display:
inline } li
{ display: list-
item }
4.3 Setting width and Maxwidth using CSS
Setting the width of a block-level element will prevent it from
stretching out to the edges of its container. Then, you can set the
margins to auto, to horizontally center the element within its
container. The element will take up the specified width, and the
remaining space will be split equally between the two margins:
This <div> element has a width of 500px, and
margin set to auto.
NOTE: You can use the bottom or right values as well in the
same way as top and left.
Here is an example:
img { float:right; }
Example:
.text_line { clear:both;}
.after-box
{
clear: left;
border: 3px solid red;
}
</style>
</head>
<body>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="floating-box">Floating box</div>
<div class="after-box">Another box, after the floating boxes...</div>
</body>
</html>
The above program will produce the following result
Example:
.center
{
margin: auto;
width: 50%;
border: 3px solid
green;
padding: 10px;
}
Example:
.right {
position:
absolute;
right: 0px;
width: 300px;
border: 3px solid
#73AD21;
padding: 10px;
}
<!DOCTYPE html>
<html>
<head>
<style>
body
{
margin: 0;
padding: 0;
}
.container
{
position: relative;
width: 100%;
}
.right
{
position: absolute;
right: 0px;
width: 300px;
background-color: #b0e0e6;
}
</style>
</head>
<body>
<h2>Right Align</h2>
<div class="container">
<div class="right">
<p><b>Note: </b>When aligning using the
position property, always include the !DOCTYPE
declaration! If missing, it can produce strange
results in IE browsers.</p>
</div>
</div>
</body>
</html>
Example
div p
{
background-color: yellow;
}
4.8.2 Child Selector
The child selector selects all elements that are the immediate
children of a specified element.
Example
<div > p {
background-color: yellow;
}
4.8.3 Adjacent Sibling Selector
Example:
div + p {
background-color: yellow;
}
4.8.4 General Sibling Selector
The general sibling selector selects all elements that are
siblings of a specified element. The following example
selects all <p> elements that are siblings of <div>
elements: Example: div ~ p {
background-color: yellow;
}
Syntax
The syntax of pseudo-classes:
selector: pseudo-class
{
property: value;
}
4.9.1 Anchor Pseudo-classes
Links can be displayed in different ways:
Example
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited link
*/ a:visited {
color:
#00FF00;
}
/* mouse over
link */ a:hover
{ color:
#FF00FF;
}
/* selected
link */
a:active
{ color:
#0000FF;
}
Note: a: hover MUST come after a: link and a:visited in the
CSS definition in order to be effective!a:active MUST
come after a:hover in the CSS definition in order to be
effective! Pseudo-class names are not casesensitive.
div:hover p
{
display: block;
}
4.9.4 The: first-child Pseudo-class
The: first-child pseudo-class matches a specified element
that is the first child of another element.
4.9.5 Match the first <p> element
In the following example, the selector matches any <p>
element that is the first child of any element:
Example
p: first-child
{
color: blue;
}
Match the first <i> element in all <p> elements
In the following example, the selector matches the first
<i> element in all <p> elements:
Example
p i:first-child
{ color:
blue;
}
Selector Example Example description
:active a:active Selects the active link
Selects every
:checked input:checked checked <input>
element
Selects every
:disabled input:disabled disabled <input>
element
Selects every <p> element
:empty p:empty
that has no children
Selects every enabled
:enabled input:enabled
<input> element
Selects every <p>
:first-child p:first-child elements that is the first
child of its parent
Selects every <p> element
:first-of-type p:first-of-type that is the first <p>
element of its parent
Selects the <input>
:focus input:focus
element that has focus
:hover a:hover Selects links on mouse
over
Selects <input> elements
:in-range input:in-range with a value within a
specified range
Selects all <input>
:invalid input:invalid elements with an invalid
value
:lang(language) p:lang(it) Selects every <p>
element with a lang
attribute value starting
with "it"
Selects every <p>
:last-child p:last-child elements that is the last
child of its parent
Selects every <p> element
:last-of-type p:last-of-type that is the last <p>
element of its parent
:link a:link Selects all unvisited links
Selects every element that
:not(selector) :not(p)
is not a <p> element
Selects every <p> element
:nth-child(n) p:nth-child(2) that is the second child of
its parent
Selects every <p> element
that is the second child of
:nth-lastchild(n) p:nth-lastchild(2)
its parent, counting from
the last child
Selects every <p> element
that is the second <p>
:nth-last-
p:nth-last-oftype(2) element of its parent,
oftype(n)
counting from the last
child
Selects every <p>
:nth-of-type(n) p:nth-oftype(2) element that is the second
<p> element of its parent
:only-of-type p:only-oftype Selects every <p> element
that is the only <p>
element of its parent
Selects every <p> element
:only-child p:only-child that is the only child of its
parent
:optional input:optional Selects <input> elements
with no
"required" attribute
input:out- Selects <input>
ofrange elements with a
:out-of-range
value outside a
specified range
input:readonly Selects <input>
elements with a
:read-only
"readonly" attribute
specified
input:readwrite Selects <input>
:read-write elements with no
"readonly" attribute
Selects <input>
elements with a
:required input:required
"required" attribute
specified
:root root Selects the document's
root element
:target #news:target Selects the current
active #news
element (clicked
on a URL
containing that
anchor name)
Selects all <input>
:valid input:valid elements with a
valid value
:visited a:visited Selects all visited links
Value Description
:first-line Use this element to add special styles
to the first line of the text in a
selector.
:firstletter Use this element to add special style
to the first letter of the text in a
selector.
:before Use this element to insert some content
before an element.
:after Use this element to insert some content
after an element.
<style
type="text/css">
p:before {
content: url(/https/www.scribd.com/images/bullet.gif)
}
</style>
<p> This line will be preceded by a bullet.</p>
<p> This line will be preceded by a bullet.</p>
<p> This line will be preceded by a bullet.</p>
<style type="text/css">
p:after
{
content: url(/https/www.scribd.com/images/bullet.gif)
}
</style>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
<p> This line will be succeeded by a bullet.</p>
4.10.5 The ::selection Pseudo-element
The: selection pseudo-element matches the portion of an
element that is selected by a user. The following CSS
properties can be applied to:
selection: color, background, cursor, and outline. The
following example makes the selected text red on a yellow
background: Here is an example:
<!DOCTYPE html>
<html>
<head>
<style>
::-moz-selection { /* Code for Firefox */
color: red;
background:
yellow; }
::selection {
color: red;
background:
yellow;
}
</style>
</head>
<body>
<h1>Select some text on this page:</h1>
<p>This is a paragraph.</p>
<div>This is some text in a div element.</div>
<p><strong>Note:</strong> ::selection is not supported in
Internet
Explorer 8 and earlier versions.</p>
<p><strong>Note:</strong> Firefox supports an
alternative, the ::-mozselection property.</p>
</body>
</html>
The above program will produce the following result:
4.11 Creating a navigation bar
Example explained:
list-style-type:none - Removes the bullets. A navigation
bar does not need list markers
Setting margins and padding to 0 to remove browser
default settings
li a {
Display: block;
}
<!DOCTYPE html>
<html>
<head>
<style
> ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color:
#f1f1f1; border:
1px solid #555;
}
li a { display:
block; color:
#000; padding:
8px 16px; text-
decoration: none;
}
li
{
text-align: center;
border-bottom: 1px solid #555;
} li:last-child
{ border-
bottom: none;
}
li a.active {
background-color: #4CAF50;
color: white;
}
Li
a:hover:not(.active)
{
background-color: #555;
color: white;
}
</style>
</head>
<body>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
The above program will produce the following result
<!DOCTYPE html>
<html>
<head>
<style
> ul {
list-style-type:
none; margin: 0;
padding: 0;
overflow: hidden;
background-color:
#333;
}
li
{
flo
at:
lef
t;
li a { display:
block; color:
white; text-
align: center;
padding: 14px
16px; text-
decoration: none;
li a:hover
{ background-
color: #111;
}
</style>
</head>
<body>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Here is an example:
input[type="tex
t"
]
{
color: #000000;
}
The advantage to this method is that the <input type="submit"
/> element is unaffected, and the color applied only to the
desired text fields. There are following rules applied to
attribute selector.
a[target="_blank"]
{ background-color:
yellow;
}
4.13.4 CSS [attribute~="value"] Selector
Example
[class$="test"]
{ background:
yellow;
}
[class*="te"]
{ background:
yellow;
}
with combinatory,pseudo-class,pseudo-
elements,creating a navigation bar, working with
images,attribute selectors.