Unit-Ii/ Chapter - 7 Cascading Style Sheets
Unit-Ii/ Chapter - 7 Cascading Style Sheets
Q. What is CSS?
Cascading Style Sheets are used to take control of the style of web pages, including the colors,
size of fonts, width, color of lines and amount of space between items on the page. The
cascading style sheets specification works by allowing you to specify rules that say how the
content of elements within the document should appear.
CSS works by allowing you to associate rules with the elements that appear in a web page. These
rules govern how the content of those elements should be rendered. To create style we state the
element we wish to define followed by a list of attributes and values in curly braces. A semicolon
separates each property and value pair. A style rule has two parts
(i) A selector
(ii) A set of declarations
The selector is used to create a link between the rule and XHTML tag. The declaration has
two parts.
(i) A property
(ii) A value
Syntax: selector {property: value; property: value …}
This form is used for all style declarations in style sheets.
⬥Ex: h1, h2, h3 {
font-weight:bold;
font-family:arial;
color:#000000;
background-color:#FFFFFF;}
There are three heading elements named in the selector (<h1>, <h2>, and <h3>), and this rule
says that where these headings are used they will be written in a bold Arial font in black with a
white background.
Q. Explain the different types of style sheets.
Style sheets can be applied at four different levels. Cascading Style sheets means the four
methods can be defined together and cascading effect implies that some method takes precedence
over the others. This means that we could create a linked style sheet for the site, but have one
page that must differ from the rest of the site.
Embedding a Stylesheet file: The implication of embedding a style sheet in a web page is that
only a single web page will benefit from the styles we specify. If the web page also links to a
style sheet, the embedded style takes precedence in the case where two tags are defined
differently.Embedded styles are placed in the <head> of the web page,surrounded by
<style></style> tags.
Ex: <html>
<head>
<style type=“text/css”>
a: link{color:red;}
a: visited {color:blue;}
a: active {color:green;}
h1{font-size 36pt;}
p{font-style:italic;}
</style>
</head>
<body>
<p>
<h1>Welcome</h1>
</p>
</body>
</html>
Linked or External style sheets - External style sheets appear in separate files. This means we
can create a single style sheet that each page of our website links to, thus creating a consistent
look to the site.
Linking to a Stylesheet file/External stylesheet: First create the stylesheet to link to, on any
text editor and store in a file with extension .css
Ex: Filename sample.css
body{background: yellow;}
a: link{color:red;}
h1{font-weight: bold; text-align: center; font-family: Arial;}
To link a web page to this style sheet we need to include the <link> element within the <head> of
the element.
⬥Ex: <html>
<head>
<title> External Stylesheet example</title>
<link rel = “stylesheet” type = “text/css” href = “sample.css”>
</head>
<body>
<h1>Welcome</h1>
<a href = “one.html”>clickone</a>
</body>
</html>
To use more than one style sheet the following procedure is adopted.
<style type = “text/css”>
<!-@import URL(URL); -> </style>
The lines are both needed. The first sheet is included as if it were only one. Any further
stylesheets have to be imported @import is enclosed within a comment so that it can be ignored
by older browsers.
Ex: <link rel = “stylesheet”
href = “URL”
type= “text/css”
media= “screen”>
<style type= “text/css”>
<! --@import URL (“style.css”)
</style>
Creating an Inline Stylesheet block: An Inline style sheet block enables to define a look for a
section of a web page. The section or division of page is divided by the <div> </div> tags with a
<style> attribute. The following examples demonstrate how to change the color of a heading and
paragraph section in a document
</div>
Creating an Inline Stylesheet Element: An inline style sheet element enables to define a look
for a specific element of a web page. Adding a style attribute to the relevant element tag does
this
Ex: <p style = “color: red; font-style: italic”>this is an inline element style sheet </p>
The <style> element is used inside the <head> element to contain style sheet rules within a web
page. It is also sometimes used when a single page needs to contain just a few extra rules that do
not apply to the other pages of the site which all share the same style sheet. For example, here is
a style sheet attached to the XHTML document using the <link> element as well as a <style>
element containing an additional rule for <h1> elements:
<head>
<title>
<link rel=”stylesheet” type=”text/css” href=”../styles/mySite.css”/>
<style type=”text/css”>
h1 {color:#FF0000;}
</style>
</head>
dir lang media title type are attributes of <style> element.
Q. What are the advantages of External Style Sheets?
If two or more documents are going to use a style sheet, we should use an external style sheet.
There are several reasons for this, including:
Several properties allow us to control the appearance of text in our documents. These can
be split into two groups:
● Those that directly affect the font and its appearance (including the typeface used,
whether it is regular, bold or italic, and the size of the text)
● Those that would have the same effect on the text irrespective of the font used (these
include color of the text and the spacing between words or letters)
✔ A typeface is a family of fonts, such as the Arial family. A font is a specific member of
that family, such as Arial 12 - point bold.
font property- Allows us to combine several of the following properties into one.
font – family property - Specifies the typeface or family of font that should be used.
Percentage: a percentage is calculated as a proportion of the element that contains the text, 2%
10% 25% 50% 100%
font - weight property -Specifies whether the font should be normal or bold.
The possible values for font - weight are: normal bold bolder lighter 100 200 300 400 500 600
700 800 900
font - style property -Specifies whether the font should be normal, italic, or oblique.
font – stretch property -Allows you to control the width of the actual characters in a font.
ultra-condensed extra-condensed condensed semi-condensed semi-expanded expanded
extra-expanded ultra-expanded are the values.
Eg. p {font-family:arial; font-stretch:condensed;}
font - variant property -Specifies whether the font should be normal or small caps.
font - size - adjust property- Allows you to alter the aspect ratio of the size of the font's
characters
There are several properties to affect the appearance or formatting of your text.
color: Specifies the color of the text
eg. p {color:#ff0000;}
text - align :Specifies the horizontal alignment of the text within its containing element
eg. . leftAlign{text-align:left;}
vertical - align :Specifies the vertical alignment of text within containing element
text – decoration: Specifies whether the text should be underlined, overlined, strikethrough, or
blinking text
text – indent: Specifies an indent from the left border for the text
text – transform: Specifies that the content of the element should all be uppercase, lowercase,
or capitalized
text - shadow :Specifies that the text should have a drop shadow
eg .two {word-spacing:20px;}
white – space: Specifies whether the white space should be collapsed, preserved, or prevented
from wrapping
eg. .pre {white-space:pre;}
.nowrap {white-space:nowrap;}
p.rtl {direction:rtl;}
The first - letter Pseudo -Class: The first - letter pseudo - class allows you to specify a rule just
for the first letter of an element. This is most commonly used on the first character of a new
page, either in some magazine articles or in books. Here is an example of the first - letter pseudo
- class applied to a < p > element that has a class attribute whose value is introduction.
The first - line Pseudo – Class: The first - line pseudo - class should allow you to render the
first line of any paragraph differently from the rest of the paragraph.
Eg. p.introduction:first-line{font-weight:bold;}
Universal Selector
The universal selector is an asterisk; it is like a wildcard and matches all element types in the
document. *{}.If we want a rule to apply to all elements, we can use this selector.
The type selector matches all of the elements specified in the comma - delimited list. It allows us
to apply the same rules to several elements.
Eg. p.BackgroundNote {}
The ID Selector
The id selector works just like a class selector, but works on the value of id attributes.
So an element with an id attribute whose value is abstract can be identified with this selector.
Eg. #abstract
The child selector matches an element that is a direct child of another. In this case it matches any
< b > elements that are direct children of < td > elements. The names of the two elements are
separated by a greater - than symbol to indicate that b is a child of td ( > ) which is referred to as
a combinator.
Eg. td > b {}
The descendant selector matches an element type that is a descendant of another specified
element, not just a direct child. While the greater - than symbol is the combinator for the child
selector, for the descendent selector the combinator is the space.
Eg. table b {}
An adjacent sibling selector matches an element type that is the next sibling of another. For
example, if we want to make the first paragraph after any level 1 heading a different style from
other <p> elements we can use the adjacent sibling selector like so to specify rules for just the
first <p> element to come after any <h1> element.
h1+p {}
The general sibling selector matches an element type that is a sibling of another, although it does
not have to be the directly preceding element. So, if we had two <p> elements that are siblings of
an <h1> element, they would both use the rules of this selector.
h1~p {}
The values of some CSS properties are given as length. Three ways lengths can be specified
in CSS-
● Relative units
● Absolute units
● Percentages
Relative Units: There are three types of relative units: pixels, which relate to the resolution of
the screen, and em’s and ex’s both of which relate to the size of fonts.
o The pixel, referred to in code as px, is by far the most commonly used unit of length in
CSS. A pixel is the smallest unit of resolution on a screen.1 pixel would correspond to
about 0.28 mm or 1 90 of an inch.
o An em is equivalent to the height of the current font, and because the size of fonts can
vary throughout a document, the height of the em unit can be different in different parts
of the document. Furthermore, because users can change the size of text in their browser,
the em unit is capable of varying in relation to the size of the text that the user has
selected. This means that the em unit is most commonly used for measurements of elements that
contain text and for controlling spacing between text. While the em unit is equivalent to the
height of a font, it is often thought to have derived from the width of a lowercase m ; you
may also hear the term en , which equates to half an em .
o The ex should be the height of a lowercase x. Because different fonts have different
proportions, the ex is related to the font size and the type of font.
Absolute Units
Absolute units are used far less than relative unit .The following table shows the absolute units
that are used in some CSS properties-
The box model is a very important concept in CSS because it determines how elements are
positioned within the browser window. It gets its name because CSS treats every element as if it
were in a box. Every box has three properties
border - Even if we cannot see it, every box has a border. This separates the edge of one box
from other surrounding boxes.
margin-The margin is the distance between the border of a box and the box next to it.
padding -This padding is the space between the content of the box and its border
We can use CSS to individually control the border, margin, and padding on each side of a box.
We can specify a different width, line - style and color for each side of the boxes’ border. The
padding and. margin properties are especially important in creating what designers refer to as
white space .this is the space between the various parts of the page.Many items can be encased in
boxes. This can give some very good effects.
o margin: length/percentage/auto{1,4}
o border-width: thin/thick/medium/length{1,4}
o padding: length/percentage{1,4}
Any of the margins of a box can be changed. We can specify 1, 2 or 4 margin values. If 4 are
specified they get applied in the order top, right, bottom and left. If one value is specified it is
applied to all four margins. If two values are specified then first will be then first will be
applied to top and bottom, the second will be applied to left and right margins. Padding and
border width are applied in the same way.
o border-color: value{1,4}
o border-style: none/dashed/solid/dotted/double/groove/ridge{1,4}
Color and style can be applied similarly
o width: length/percentage/auto{1,4}
o height: length/auto
Height and width can be specified to the box
Eg.
<?xml version=”1.0” ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” lang=”en” xml:lang=”en”>
<head>
<title>Understanding the Box Model</title>
<link rel=”stylesheet” type=”text/css” href=”ch07_eg19.css” />
</head>
<body>
<h1>Thinking Inside the Box</h1>
<p class=”description”>When you are styling a web page with CSS you
must start to think in terms of <b>boxes</b>.</p>
<p>Each element is treated as if it generates a new box. Each box can have
new rules associated with it.</p>
<img src=”images/boxmodel.gif” alt=”How CSS treats a box” />
<p>As you can see from the diagram above, each box has a <b>border</b>.
Between the content and the border you can have <b>padding</b>, and
outside of the border you can have a <b>margin</b> to separate this box
from any neighboring boxes.</p>
</body>
</html>
CSS rule
body, h1, p, img, b
{
border-style:solid;
border-width:2px;
border-color:#000000;
padding:2px;
}
--------------------------------------------------------------------------------------------------------------------
h1, b {background-color:#cccccc;}
Q. Write about Box related properties.
The Border Properties: The border properties allow to specify how the border of the box
representing an element should look.
The border - color property allows to change the color of the border surrounding a box. The
value can be a color name or a hex code for the color.
Eg. p {border-color:#ff0000;}
We can individually change the color of the bottom, left, top, and right sides of a box ’ s border
using the following properties:
border - bottom - color
border - right -color
border - top - color
border - left – color
The border - style property allows you to specify the line style of the border:
Eg. p {border-style:solid;}
none No border.
solid Border is a single solid line.
dotted Border is a series of dots.
dashed Border is a series of short lines.
double Border is two solid lines.
groove Border looks as though it is carved into the page.
ridge Border looks the opposite of groove .
Inset Border makes the box look like it is embedded in the page.
outset Border makes the box look like it is coming out of the canvas.
hidden Same as none , except in terms of border - conflict resolution for table elements.
We can individually change the style of the bottom, left, top, and right borders of a box using the
following properties:
border - bottom – style
border - right - style
,border - top – style
border - left - style
The border - width Property allows setting the width of our borders; usually the width is
specified in pixels. The value of the border - width property cannot be given as a percentage,
although we could use any absolute unit or relative unit, or one of the following values:
We can individually change the width of the bottom, top, left, and right borders of a box using
the following properties:
The border property allows you to specify color, style, and width of lines in one property:
Padding property-
The padding property allows you to specify how much space should appear between the
content of an element and its border:
Eg. td {padding:5px;}
The value of this property is most often specified in pixels, although it can use any of the units of
length we met earlier, a percentage, or the word inherit. We can specify different amounts of
padding inside each side of a box using the following properties:
padding – bottom
padding – top
padding – left
padding - right
The margin property controls the gap between boxes, and its value is either a length, a
percentage, or inherit.
p {margin:20px;}
We can also set different values for the margin on each side of the box using the following
properties:
margin-bottom
margin-top
margin-left
margin-right
Dimensions of a Box:
The line - height property is one of the most important properties when laying out text. It allows
to increase the space between lines of text. The value of the line - height property can be a length
or a percentage
The max-width and min-width properties allow us to specify a maximum and a minimum width
for a box. This should be particularly useful if we want to create parts of pages that stretch and
shrink to fit the size of users’ screens. The max - width property will stop a box from being so
wide that it is hard to read and min - width will help prevent boxes from being so narrow that
they are unreadable.
The min - height and max - height properties correspond with the min - width and max - width
properties, but specify a minimum height and maximum height for the box,.These properties are
very useful in creating layouts that can be resized depending upon the size of the user’s browser
window.
When we control the size of a box, the content weu want to fit in the box might require more
space than we have allowed for it. The overflow property was designed to deal with these
situations and can take one of the values
hidden The overflowing content is hidden.
scroll The box is given scrollbars to allow users to scroll to see the content
eg.
div.one {overflow:hidden;}
div.two {overflow:scroll;}
-----------------------------------------------------------------------------------------------------------