HTML: Style Sheets
Copyright © Ellis Horowitz 1999-2022 1
Style Sheets
Copyright By PowCoder代写 加微信 powcoder
• Starting with HTML version 4.x there was an effort to separate the specification of style (presentation) from the specification of content
• Style sheets are the mechanism HTML assumes will be used to specify
– the amount of white space between text or between lines,
– the amount lines are indented,
– the colors used for the text and the backgrounds
– the font size and style of text
– the precise position of text and graphics
– How front matter (preface, figure list, title page, and so forth) should look
– How all or individual sections should be laid out in terms of space (for example, two newspaper columns, one column with headings having hanging heads, and so forth)
Copyright © Ellis Horowitz 1999-2022 2
• CSS stands for Cascading Style Sheets
– It is not a markup language. It is a list of Rules
• CSS is like JSON Objects, with keyword: value pairs – CSS keywords are already defined though
CSS 3 modules
Copyright © Ellis Horowitz 1999-2022 3
Style Sheet Languages
• These notes use the style language “Cascading Style Sheets” ([CSS1] and [CSS2]), abbreviated CSS, and described in:
http://www.w3.org/TR/REC-CSS1
http://www.w3.org/TR/REC-CSS2
http://www.w3.org/TR/CSS21/ http://www.w3.org/Style/CSS/current-work (Level 3, in process) http://www.w3.org/Style/CSS/Overview.en.html (see Status)
• There are other style sheet languages, e.g., XSL defined at http://www.w3.org/Style/XSL
(level 2 Revision 1)
Copyright © Ellis Horowitz 1999-2022 4
Expressing Style Within HTML
• CSS can be included in three ways
1. Inline in an HTML element through
the style attribute
2. In the
To be or not to be, that is the questionand here is a quote from G. Marx
I would never belong to an organization that would have me as a memberCopyright © Ellis Horowitz 1999-2022 11
Browser Output Firefox Internet Explorer Chrome Copyright © Ellis Horowitz 1999-2022
Example - Center Entire Document
Copyright © Ellis Horowitz 1999-2022 13
Browser Output
Firefox Internet Explorer Chrome
Copyright © Ellis Horowitz 1999-2022
Option 3: Selectors (. #)
• The CLASS attribute assigns a name to one or
more elements
• The ID attribute also can be use in a similar way.
• CLASS and ID can be used as a selectors of style properties
Ex1: P.redtext {color: #FF0000}
would be applied when
contains the CLASS name
some text
Ex2: .redtext{color: #FF0000}
applies to all elements that use CLASS=redtext
• Context sensitive selectors
STRONG { color: #00FF00}
defines all occurrences of as green, but
Copyright © Ellis Horowitz 1999-2022 15
Class Attribute
• The class attribute is used for optional styles
.Warning { font-weight: bold; color: red;
dot character
A paragraph
border:2px solid red;}
A paragraph with
red text and a red border
A paragraph
Copyright © Ellis Horowitz 1999-2022 16
Id Attribute
• The id attribute is also used for optional styles
– but it can only be used ONCE in the entire document
• Normally used for major document sections – Header, Footer, TopNav, LeftNav,
– Content, etc.
#Disclaimer { font-size:medium;
color: #ff9900;
border:1px dotted red;}
# character
A paragraph
A paragraph
Your home is at risk if you
do not keep up repayments on a mortgage
or other loan secured on it.
Copyright © Ellis Horowitz 1999-2022 17
Specific Styles
• Styles aimed at specific tags
ul { color: red; }
ul ul { color: gray; }
ul ul ul { color: black }
- Sub-list item in gray
- Sub-sub-list item in black
- List item in red
Copyright © Ellis Horowitz 1999-2022 18
More