Chapter-4-Cascading Style Sheets
Chapter-4-Cascading Style Sheets
The value however is the exact style you want to set for the
property. Values can include length, percentage, color, url,
and shape.
Declarations tell a browser how to draw any element
on a page that is selected. The end of each
declaration is indicated with a semi colon.
You can use multiple declarations by placing a
semicolon at the end of the declaration.
Examples of Declaration Block:
p {...}
h1 {...}
h2 {...}
p { color: maroon; font-family:arial}
h1 { text-align: center; }
h2 { font-style: italic; }
You may combine selectors when several selectors
share the same declarations.
Each selector must be separated by a comma but, this
prevents the need to write the same rule more than
once.
For example:
B) Class selectors
C) ID selectors
A) HTML element selectors
• The most basic form of CSS selector is an HTML
element. Example: h1, h2, p, ol, ul, table, etc.
• This is the simple or element selector. Example:
p { color: #003366; }
• This will set every occurrence of content marked up the
<p> paragraph tag to be a dark blue color.
• Element selectors can be grouped together if you want to
apply one CSS rule to a range of elements. Example:
h1, h2, h3, h4, h5, h6 { color: #FF0000; }
• Each element is separated by a comma (except for the last
element before the start of the declaration).
• Multiple declarations also possible in element selection.
B) CSS Class Selectors
• The class selector selects elements with a specific
class attribute. To select elements with a specific
class, write a period (.) character, followed by the
name of the class. Example:
h1.special { color: #FF0000; }
The actual definition of the value “special” is defined in a
CSS class selector. So, in body section:
<h1 class=“special”> this is heading </h1>
• This will now make all <h1> elements with the class
“special” display text with a red color. <h1> elements that
don’t have the class attribute “special” will continue to
display in the default <h1> color.
• A CSS class selector can also be defined more generally,
without the element name (just the dot):
.special { color: #FF0000; }
• This class can now be applied to any HTML element that
has the class attribute “special”.
• Actually the full CSS syntax is *.special, where * is a
selector that matches anything. However, CSS shorthand
means we can drop the *.
• HTML elements can also refer to more than one class.
Example : .center {background-color:yellow;}
.large {color:blue;}
• <p class="center large">This paragraph refers to two
classes.</p>
C) CSS ID Selectors
• HTML elements can also have id selectors
assigned to them. Example:
<p id=“summary”>blah, blah, blah.</p>
• In the CSS, the id “summary” can be styled in a
rule, thus:
#summary { font-style: italic; }
Note: An id and class name cannot start with a
number!
CSS How To...
Type of CSS Styles
div.ex2 {
padding: 25px 50px 75px;
}
div.ex3 {
padding: 25px 50px;
}
div.ex4 {
padding: 25px;
}
CSS- font: font style
The following properties can be specified for any element that contains
text, such as <h1> thru <h6>, <p>, <ol>, <ul>, and <a>:
Property Some Possible Values
color: blue, green, yellow, red, white, etc.
font-family: Arial, Verdana, "Times New Roman"
font-size: large, 120%, 20px (pixels)
font-weight: bold, normal
font-style: italic, normal
Note: The font-family property should hold several font names as a
"fallback" system. If the browser does not support the first font, it tries
the next font, and so on. Start with the font you want, and end with a
generic family, to let the browser pick a similar font in the generic family,
if no other fonts are available.
Example: p {
font-family: "Times New Roman", Times, serif; }
CSS- text: text formatting
Text formatting properties are:
• Text Color,
• text-align
• text-decoration with values of overline; line-through,
underline, and none; used to set or remove
decorations from text.
– Example: a { text-decoration: none;}
– h1 {
text-decoration: overline;
}
h2 { text-decoration: line-through;}
h3 { text-decoration: underline; }
• Text Transformation
The text-transform property is used to specify uppercase and
lowercase letters in a text.
• It can be used to turn everything into uppercase or lowercase
letters, or capitalize the first letter of each word:
Example
• p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
• Text Indentation
The text-indent property is used to specify the indentation of the first line of a text:
Example
• p{
text-indent: 50px;
}
• Letter Spacing
The letter-spacing property is used to specify the space between the characters in a
text.
The following example demonstrates how to increase or decrease the space
between characters:
Example
• h1 {
letter-spacing: 3px;
}
h2 {
letter-spacing: -3px;
}
• Line Height
The line-height property is used to specify the space between lines:
Example
• p.small {
line-height: 0.8;
}
p.big {
line-height: 1.8;
}
Word Spacing
The word-spacing property is used to specify the space between the words in a text.
The following example demonstrates how to increase or decrease the space between
words:
Example
• h1 {
word-spacing: 10px;
}
h2 {
word-spacing: -5px;
}
CSS- links
styling links
• Links can be styled with any CSS property
(e.g. color, font-family, background, etc.).
Example: a {
color: hotpink; background-color: yellow; Font-family: arial; }
When setting the style for several link states, there are
some order rules:
– a:hover MUST come after a:link and a:visited
– a:active MUST come after a:hover
– The text-decoration: property with none value is mostly
used to remove underlines from links.
• Example
a:link { color: red; background-color: yellow; text-
decoration: none; }
a:visited { color: green; background-color: cyan; text-
decoration: none; }
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
Overlapping Elements
img:hover {
opacity: 1.0;
}