Lecture 3 - Css - Bn109 - R
Lecture 3 - Css - Bn109 - R
06/21/24 T1 2022
Learning Outcomes
• In this chapter, you will learn how to . . .
– Describe the evolution of style sheets from print media to
the Web
– List advantages of using Cascading Style Sheets
– Use color on web pages
– Create style sheets Apply inline styles
– Use embedded style sheets, external style sheets
– Configure element, class, id, and contextual selectors
– Utilize the “cascade” in CSS
– Validate CSS
06/21/24 T1 2022
Overview of
Cascading Style Sheets (CSS)
• See what is possible with CSS:
– Visit http://www.csszengarden.com
• Style Sheets
– used for years in Desktop Publishing
– apply typographical styles
and spacing to printed media
• CSS
– provides the functionality of style sheets (and much more) for
web developers
– a flexible, cross-platform, standards-based language
developed by the W3C.
06/21/24 T1 2022
CSS
Advantages
• Greater typography and page layout control
• Style is separate from structure
• Styles can be stored in a separate document
and associated with the web page
• Potentially smaller documents
• Easier site maintenance
06/21/24 T1 2022
Types of
Cascading Style Sheets
• Inline Styles
• Embedded Styles
• External Styles
• Imported Styles
06/21/24 T1 2022
Cascading
Style Sheets
• Inline Styles
– body section
– HTML style attribute
– apply only to the specific element
• Embedded Styles
– head section
– HTML style element
– apply to the entire web page document
06/21/24 T1 2022
Cascading
Style Sheets
• External Styles
– Separate text file with .css file extension
– Associate with a HTML link element in the head section of a
web page
• Imported Styles
– Similar to External Styles
– We’ll concentrate on the other three types of styles.
06/21/24 T1 2022
CSS Syntax
06/21/24 T1 2022
CSS Syntax Sample
06/21/24 T1 2022
Common Formatting
CSS Properties
• See Table 3.1 Common
CSS Properties,
including:
– background-color - line-height
– color - margin
– font-family - text-align
- text-decoration
– font-size
- width
– font-style
– font-weight
06/21/24 T1 2022
Using Color on Web Pages
06/21/24 T1 2022
Hexadecimal
Color Values
• # indicates a hexadecimal value
• Hex value pairs range from 00 to FF
• Three hex value pairs describe an RGB color
06/21/24 T1 2022
Web Color Palette
06/21/24 T1 2022
Making Color Choices
06/21/24 T1 2022
Support Web Accessiblity
Verify Sufficient Contrast
• When you choose colors for text and background,
sufficient contrast is needed so that the text is
easy to read.
• Use one of the following online tools to verify
contrast:
– http://webaim.org/resources/contrastchecker
– http://snook.ca/technical/colour_contrast/colour.html
– http://juicystudio.com/services/
luminositycontrastratio.php
06/21/24 T1 2022
Configuring Color with Inline
CSS
• Inline CSS
– Configured in the body of the web page
– Use the style attribute of an HTML tag
– Apply only to the specific element
06/21/24 T1 2022
Configuring Color with Inline
CSS (2)
• Example : configure the red text in the
heading and configure a gray background in
the heading
• Separate style rule declarations with ;
<h1 style="color:#FF0000;background-
color:#cccccc">This is displayed as a red
heading with gray background</h1>
06/21/24 T1 2022
CSS Embedded (Internal)
Styles
• Configured in the head section of a web page.
• Use the HTML <style> element
• Apply to the entire web page document
• Style declarations are contained between the
opening and closing <style> tags
• Example: Configure a web page with white
text on a black background
<style>
body { background-color: #000000;
color: #FFFFFF;
}
06/21/24 T1 2022
</style>
CSS Embedded Styles
06/21/24 T1 2022
Configuring Text with CSS
06/21/24 T1 2022
The font-size Property
• Accessibility Recommendation: Use em or percentage font
sizes – these can be easily enlarged in all browsers by users
06/21/24 T1 2022
The font-family Property
06/21/24 T1 2022
Embedded Styles Example
<style>
body { background-color: #E6E6FA;
color: #191970;
font-family: Arial, Verdana, sans-serif; }
h1 { background-color: #191970;
color: #E6E6FA;
line-height: 200%;
font-family: Georgia, "Times New Roman",
serif; }
h2 { background-color: #AEAED4;
color: #191970; text-align: center;
font-family: Georgia, "Times New Roman",
serif; }
p {font-size: .90em; text-indent: 3em; }
ul {font-weight: bold; }
06/21/24 </style>T1 2022
CSS Selectors
06/21/24 T1 2022
Using CSS with “class”
06/21/24 T1 2022
Using CSS with “id”
<style>
• id Selector #new { color: #FF0000;
– Apply a CSS font-size:2em;
rule to ONE element font-style: italic;
on a web page. }
</style>
• Configure with #idname
– Code CSS to create an id called “new”
with red, large, italic text.
• Apply the id:
<p id=“new”>This is text is red, large, and in
italics</p>
06/21/24 T1 2022
External Style Sheets - 1
06/21/24 T1 2022
External Style Sheets - 2
06/21/24 T1 2022
Using an External Style Sheet
External Style Sheet color.css
body { background-color: #0000FF;
color: #FFFFFF;
}
06/21/24 T1 2022
Centering Page Content with
CSS
#container { margin-left: auto;
margin-right: auto;
width:80%; }
06/21/24 T1 2022
The “Cascade”
06/21/24 T1 2022
Summary
06/21/24 T1 2022
Checkpoint 3.1
06/21/24 T1 2022
Checkpoint 3.2
06/21/24 T1 2022