CSE1041 Web 1: Week 4 Cascading Style Sheets - CSS
CSE1041 Web 1: Week 4 Cascading Style Sheets - CSS
Week 4
Cascading Style Sheets - CSS
1
Agenda
• What is a Style Sheet?
• How to specify a style using Style Sheets
• Types of styles:
– Inline style
– Internal style
– External style
• Multiple style sheets
– Precedence of style sheets
• Id and Class Selectors
2
Introduction
• A web document contains both structural and
presentation (or stylistic) information
3
Structure v/s Style (1)
• HTML:
– Structure: divisions, paragraphs, tables, etc…
– Presentation: fonts, color, indentation, spacing, etc…
4
Structure v/s Style (2)
Structure
+
5
Presentation Formatted Web page
Cascading Style Sheets (CSS)
• CSS evolved from the need to globally control the
look of elements in a web page or multiple web
pages
• Example:
– If your company decides to use Arial font for standard
text on all its web pages, what to do?
– Painful solution: edit all pages individually and change
<font face=”…”> tag
– Easy solution: modify a style sheet which is
referenced by all web pages in your site
6
CSS Syntax
• The CSS syntax is made up of three parts:
1. Selector
2. Property
3. Value
• Example:
p {text-align: center;}
9
Inline Style (2)
• Inline styles:
– Attributes of an element.
– Entered as attribute = value in element tag
• Example:
12
Internal Style (3)
• In this example, all <input> <style>
and <textarea> tags will be input
{
formatted according to the text-transform:uppercase;
styles defined. color:red;
text-decoration:underline;
}
• In other words, we are re-
textarea
defining how HTML tags are {
displayed. text-transform:uppercase;
– This is useful when ALL the color:red;
}
tags need to follow a </style>
standard formatting.
13
External style sheet (1)
• An external style sheet is ideal when the style is
applied to many pages
14
External Style Sheet (2)
• Using a <link> tag in the <head> section of a
document
• Example:
15
Multiple Style Sheets (1)
• There are many ways to apply a style to an element:
– Inline Style
– Internal Style
– External style
17
Exercise 1 top.html
• Consider the
20%
following
screenshot.
80%
left.html
right.html
• An external
15% 85%
CSS style has
been defined Style definition (mystyle.css)
body
to format the {
background-color: Olive;
HTML pages. font-size: 14px;
}
18
Exercise 1(a)
<head>
• A registration page, <link rel="stylesheet" type="text/css"
register.html, has href="css/mystyle.css" />
been created.
<style type="text/css">
body{
• Assuming that the background-color: Yellow;
following styles have font-size: 12px;
}
been applied to </style>
register.html, what </head>
would be the final
background color <body style=" background-color: Blue” >
and font size of the
… HTML codes goes here
registration page?
Justify your answer. </body >
</html>
Register.html 19
Exercise 1(b)
• The left.html and right.html from Question 1
contains the code snippet below inside their body
section. Briefly explain what is wrong with this code
snippet and make the appropriate correction(s).
20
Selectors
• In addition to setting a style for a HTML
element, CSS allows you to specify your own
selectors:
– Id selector
– Class selector
21
Id Selector
• The id selector is used to specify a style for a
single, unique element.
• The id selector uses the id attribute of the
HTML element, and is defined with a "#".
• Example:
– The style rule below will be applied to the
element with id="para1"
#para1 <html>
{ <head><head>
text-align: center; <body>
color: red; <div id=“para1”> </div>
} </body> 22
Class Selector
• The class selector is used to specify a style for
a group of elements
Style 24
Class Selector - Example 2
• In Example 1, the class transform1, transform2 applies only to
<input> tag. It cannot be applied to <textarea> tag or any
other HTML tags
• A general class can be defined to apply to different types of
HTML tags
<input type=text
name="txt_Username"
.transform{
text-transform:uppercase;
class="transform">
color:red;
text-decoration:underline; <textarea rows=4
} cols=50
Style class="transform">
Applying the style
25
Using Id and Class selectors together
26
Exercise 2
• Consider the following HTML page top.html right.html
20%
80%
left.html
center.html
29