CSSCASCADING STYLE SHEETS
Mukesh N. TekwaniComputer Science & [email protected]. Y. College, Mumbai, India
2
Prof. Mukesh N. Tekwani
WHAT IS CSS? CSS stands for Cascading Style Sheets CSS defines HOW to display HTML elements Styles are normally stored in Style Sheets External Style Sheets can save you a lot of work External Style Sheets are stored in CSS files Multiple style definitions will cascade into one CSS selects an element and sets the rules for
that element. These set of rules are known as style sheets and can be in the HEAD section of an HTML document or in an external style sheet.
January 2012
3
Prof. Mukesh N. Tekwani
CSS RULES A CSS rule has 2 parts: a selector, and one or more
declarations:
Selector DeclarationH1 {color:blue; font-size:12px;)Here, color is the property and blue is the value of that property.
The selector is the HTML element that we want to style. The property is the attribute we want to change. Each attribute has a value.
CSS property names are separated by dashes when they are multiple words—for example, font-face, font-size, line-height, and so on.
January 2012
4
Prof. Mukesh N. Tekwani
WHAT IS THE NEED FOR CSS? (OR WHAT ARE THE ADVANTAGES OF CSS?) HTML pages use a lot of markup to style the
pages.
There can be very complex structures of tables, nested frames, invisible pixel images for layout, etc.
This makes HTML page difficult to render for the browser.
January 2012
5
Prof. Mukesh N. Tekwani
WHAT IS THE NEED FOR CSS? (OR WHAT ARE THE ADVANTAGES OF CSS?) Code: CSS is the standard for coding in HTML. CSS is compatible
with most browsers. CSS reduces the length of the codes of web page, which decreases the page size, making it easy and fast to load in browsers
Design: Use of CSS makes the design simple. CSS makes the management of the entire website easy to maintain by just changing the CSS file which contains the style details.
Bandwidth: CSS reduces the HTML coding and page size. This reduces the bandwidth usage.
Consistency: It is easy to maintain, handle and control the whole website made on CSS based HTML. Ex: Suppose we want to change the background of the entire website, we just need to change the background of the single page in the style sheet and the background of the whole website will change.
January 2012
6
Prof. Mukesh N. Tekwani
WHAT IS MEANT BY STYLE RULES? A style rule is used to change the default
behavior of an HTML element. All style rules are contained in the <STYLE> element and this is put in the HEAD section of an HTML document.
A style rule is made up of 2 parts: a selector and a declaration. The selector determines the element to which the style is to be applied. The declaration gives the exact property values.
January 2012
7
Prof. Mukesh N. Tekwani
WHAT IS MEANT BY STYLE RULES? Consider the <P> element. We can create a style rule for
this <P> element so that all paragraphs are in blue color and have a font size of 24px. The style rule is as follows:
<STYLE>P {COLOR:BLUE; FONT-SIZE:24px}
</STYLE> Consider the <H1> element. We can create a style for
this element so that all H1 headings are in red color. <STYLE TYPE = “TEXT/CSS”>
H1 {COLOR:RED} </STYLE>
January 2012
8
Prof. Mukesh N. Tekwani
DEFINING THE STYLE FOR A SINGLE ELEMENT: We can define the style for a single element
as follows:
<H1 STYLE =”COLOR:BLUE”>This is a heading</H1>
This direct use of CSS is called inline style and is not recommended due to the tight coupling between the HTML document and the style.
January 2012
9
Prof. Mukesh N. Tekwani
TRY… Write a style rule so that every <H1>
element on your web site is in green color and centered.
H1 {COLOR:GREEN; TEXT-ALIGN:CENTER}
January 2012
10
Prof. Mukesh N. Tekwani
EXTERNAL STYLE SHEET: Placing style sheets in an external document
lets you specify rules for different HTML documents. An external style sheet is a text document with the file name extension of .CSS. This external style sheet contains the style rules for various elements.
January 2012
11
Prof. Mukesh N. Tekwani
EXTERNAL STYLE SHEET:/* stylesheet 1 */H1 {COLOR:GREEN}H2 {COLOR:GREEN; BORDER:SOLID BLUE}(Other option for SOLID is DOTTED)The CSS line begins with a comment. The CSS style sheet does not contain any HTML code.
To link this external style sheet to an HTML document, we add the <LINK> element in the HEAD section of an HTML document:
<HEAD><TITLE>Sample document</TITLE><LINK HREF = “style1.css” REL = “stylesheet”></HEAD>
January 2012
12
Prof. Mukesh N. Tekwani
EXTERNAL STYLE SHEET: The HTML file containing this code displays with
the characteristics given in the style sheet.
The HREF attribute gives the URL of the stylesheet. The REL attribute specifies the relationship between the linked and the current document.
The major advantage of external style sheets is that the styles can apply to all the web pages on a site. In order to make global changes, we just have to modify the external style sheet.
January 2012
13
Prof. Mukesh N. Tekwani
DIFFERENT CSS SELECTION TECHNIQUES Selecting Multiple Elements:
By using multiple selectors, we can use less code. E.g., to make both <H1> and <H2> headings green, we can use the following rules:
<STYLE TYPE = “TEXT/CSS”><H1> {COLOR:GREEN}<H2> {COLOR:GREEN}</STYLE>
These two rules can be expressed in a single rule statement using multiple selectors for the same property as follows:
<STYLE TYPE = “TEXT/CSS”>H1, H2 {COLOR:GREEN}</STYLE>
January 2012
14
Prof. Mukesh N. Tekwani
DIFFERENT CSS SELECTION TECHNIQUES Selecting by Context:
A context-based selector lets you specify the exact context in which a style is applied.
For example, to specify that the <I> elements appear in blue color only within the <H1> elements, we create the style rule as follows:<STYLE TYPE = “TEXT/CSS”>H1 I {COLOR:BLUE}</STYLE>
Note: We should not place a comma between the element H1 and I in the above example. Otherwise it will turn a contextual selection into a multiple element selection and blue color text will apply to both H1 headings and Italic text.
So don’t write this: H1 , I {COLOR:BLUE}
January 2012
15
Prof. Mukesh N. Tekwani
DIFFERENT CSS SELECTION TECHNIQUES Selecting with the CLASS attribute:
The CLASS attribute lets you write rules and then apply them to groups of elements. Basically the CLASS attribute lets you define your own tags and apply them wherever you want.
To create a class, we first declare it within the <STYLE> element. The period (.) flag character indicates that the selector is a class selector.
<STYLE TYPE = “TEXT/CSS”>
.QUOTE {COLOR:RED}</STYLE>
Applying this style to a paragraph:<P CLASS=”QUOTE”> This is a paragraph </P>
January 2012
16
Prof. Mukesh N. Tekwani
WHAT ARE THE CSS FONT PROPERTIES?
The following font properties can be controlled with CSS:
Font families and alternatesFont sizeFont weightLine heightLetter spacingText indentColor
January 2012
17
Prof. Mukesh N. Tekwani
WHAT ARE THE CSS FONT PROPERTIES?
Example 1: Create a style rule that specifies Arial as the font for the <P> element.
<STYLE TYPE=”TEXT/CSS”>P {FONT-FAMILY:ARIAL}
</STYLE>
January 2012
18
Prof. Mukesh N. Tekwani
WHAT ARE THE CSS FONT PROPERTIES?
Example 2: Create a style rule that specifies Arial as the font for the <P> element. In case Arial font is not available, use the Helvetica font.
<STYLE TYPE=”TEXT/CSS”>P {FONT-FAMILY:ARIAL, HELVETICA}</STYLE>
January 2012
19
Prof. Mukesh N. Tekwani
WHAT ARE THE CSS FONT PROPERTIES?
Example 3: Create a style rule that specifies Arial as the font for the <P> element. In case Arial font is not available, use the Helvetica font. In case this too is not available, use a generic font like sans-serif.
<STYLE TYPE=”TEXT/CSS”>P {FONT-FAMILY:ARIAL, HELVETICA, SANS-SERIF}</STYLE>
The generic names we can use are: Monospace, Serif and Sans-serif.
January 2012
20
Prof. Mukesh N. Tekwani
WHAT ARE THE CSS FONT PROPERTIES?Specifying Text Background Color:
Example 4: We can set the text background color (i.e., the color behind the text) for any element, by using the BACKGROUND-COLOR property, as follows:
<STYLE TYPE=”TEXT/CSS”>H2 {COLOR:WHITE; BACKGROUND-COLOR:BLUE}</STYLE>
January 2012
21
Prof. Mukesh N. Tekwani
WHAT IS THE CLASS SELECTOR IN CSS? The CLASS attribute lets you write rules and then apply them to
groups of elements. The CLASS attribute lets you define your own tags and apply
them wherever you want.
To create a class, we first declare it within the <STYLE> element. The period (.) flag character indicates that the selector is a class selector.
<STYLE TYPE = “TEXT/CSS”>.QUOTE {COLOR:RED}</STYLE>
Applying this style to a paragraph:<P CLASS=”QUOTE”> This is a paragraph </P>
January 2012
22
Prof. Mukesh N. Tekwani
WHAT IS THE CLASS SELECTOR IN CSS?
Example 2: Create a class rule called “veryimportant” that sets the background color to yellow. Apply this to a H1 heading and two paragraphs. Also show how a para is rendered if the class is not applied to it.
<HTML><HEAD><STYLE TYPE="TEXT/CSS">.veryimportant {background-color: yellow;}</STYLE></HEAD><BODY><H1 CLASS="veryimportant">Example</h1><P CLASS="veryimportant">This is the first paragraph.</P><P>This is the second paragraph.</P><P Class="veryimportant">This is the third paragraph.</P></BODY></HTML>
January 2012
23
Prof. Mukesh N. Tekwani
HOW TO SPECIFY TEXT MARGINS N CSS?
The MARGIN attribute is used to set the text margin on all four sides. We can also set the margins on individual sides with following settings:
MARGIN-TOPMARGIN-BOTTOMMARGIN-LEFTMARGIN-RIGHT
Example: Set the margin on all sides to 30 px<STYLE TYPE = “TEXT/CSS”>P {margin:30px}</STYLE>
January 2012
24
Prof. Mukesh N. Tekwani
HOW TO SPECIFY THE TEXT BORDERS?
The BORDER property can be used to set the border style, color and width.
Syntax: {BORDER BORDER-STYLE BORDER-WIDTH BORDER-
COLOR}
Example<STYLE TYPE = “TEXT/CSS”>
P {BORDER: SOLID 2pt BLUE}</STYLE>
January 2012
25
Prof. Mukesh N. Tekwani
CHANGING BACKGROUND COLOR WITH CSS<html><head><style type="text/css">body{
background-color:#b0c45e;}</style></head><body>
<h1>My CSS web page!</h1><p>Hello world!</p>
</body></html>
January 2012
26
Prof. Mukesh N. Tekwani
SET AN IMAGE AS THE BACKGROUND OF A PAGE<html><head><style type="text/css">
body {background-image:url('paper.gif');}</style></head><body><h1>Hello World!</h1></body></html>
January 2012
27
Prof. Mukesh N. Tekwani
EXAMPLECreate a style rule as follows: To display the date in
a right-aligned paragraph. To display the main text in a justified paragraph.
<html><head><style type="text/css">
h1 {text-align:center;}p.date {text-align:right;}p.main {text-align:justify;}
</style></head>
January 2012
28
Prof. Mukesh N. Tekwani
EXAMPLE (CONTD)<body>
<h1>CSS text-align Example</h1><p class="date">Nov 2010</p><p class="main">This is the main paragraph in which text alignment is “justified”</p>
</body></html>
January 2012
29
Prof. Mukesh N. Tekwani
EXAMPLECreate style rules for the four states of a link. <html><head><style type="text/css">
a:link {color:#FF0000;} /* unvisited link */a:visited {color:#00FF00;} /* visited link */a:hover {color:#FF00FF;} /* mouse over link */a:active {color:#0000FF;} /* selected link */
</style></head>
January 2012
30
Prof. Mukesh N. Tekwani
EXAMPLE (CONTD)<body>
<p><b><a href="default.asp" target="_blank">This is a link</a></b></p>
</body></html>
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.
January 2012
31
Prof. Mukesh N. Tekwani
EXAMPLE Create a style rule for H1 element with
following specifications:H1 tag with Background image: swirlbkg.jpgColor—greenLetter-spacing-7ptText-align- centerText-decoration-underlineText-transform--uppercase;Word-spacing: 2pt
January 2012
32
Prof. Mukesh N. Tekwani
EXAMPLE(CONTD)H1 {
color:green;letter-spacing:7pt;text-align: center; text-decoration:underline; text-transform:uppercase; word-spacing: 2pt
}
January 2012
33
Prof. Mukesh N. Tekwani
EXAMPLE Create a style rule for P tag with
following specifications:Color – magentafont-family – Algerianfont-size – mediumfont-style—italic font-weight—lighter
January 2012
34
Prof. Mukesh N. Tekwani
EXAMPLEP{
color: magenta;font-family:Algerian;font-size: medium;font-style:italic;font-weight:-lighter;
}
January 2012
35
Prof. Mukesh N. Tekwani
EXAMPLE TO ILLUSTRATE THE VARIOUS STYLES APPLIED TO LISTS:<html><head><style type="text/css">
ul.a {list-style-type:circle;} /* open circle */ul.b {list-style-type:square;} /* filled square */ul.e {list-style-type:disc;} /* filled circle */ol.c {list-style-type:upper-roman;} /* upper roman I */ol.d {list-style-type:lower-alpha;} /* lower alpha: a */
</style></head>
January 2012
36
Prof. Mukesh N. Tekwani
LISTS<body><p>Example of unordered lists:</p>
<ul class="a"> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li>
</ul> <ul class="b">
<li>Coffee</li> <li>Tea</li> <li>Coca Cola</li>
</ul>
January 2012
37
Prof. Mukesh N. Tekwani
LISTS<ul class="e"> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li></ul>
<p>Example of ordered lists:</p><ol class="c"> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li></ol>
</body></html>
January 2012
38
Prof. Mukesh N. Tekwani
HANGING INDENTWrite a style rule for a <P> element with a 24 pt hanging indent and a 30 pixel margin on left and right sides.
<STYLE TYPE = "TEXT/CSS">P {
text-indent:-24pt;margin-left:30px;margin-right:30px
}</STYLE>
January 2012
39
Prof. Mukesh N. Tekwani
ALIGNING TEXT WITH CSS<html><head><style>h1 {text-align:center;}p.date {text-align:right;}p.main {text-align:justify;}</style></head><body><h1>CSS text-align Example</h1><p class="date">May, 2009</p><p class="main">In my younger and more vulnerable years my father
gave me some advice that I've been turning over in my mind ever since. 'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'</p>
<p><b>Note:</b> Resize the browser window to see how the value "justify" works.</p></body></html>
January 2012
40
Prof. Mukesh N. Tekwani
QUESTIONS ? Read. Practice. Read. Practice. : : :
January 2012