Chapter 3 Cascading Style Sheet CSS
Chapter 3 Cascading Style Sheet CSS
Pages load faster: if we are using CSS, we do not need to write HTML
tag attributes every time. Just write one CSS rule of a tag and apply to all
the occurrences of that tag. So less code means faster download times.
Easy maintenance: to make a global change, simply change the style and
all elements in all web pages will be updated automatically.
✦ CSS1 defines basic style functionality, with limited font and limited positioning
support.
✦ CSS2 adds aural properties, paged media, better font and positioning support. Many
other properties have been refined as well.
Keep in mind that you don’t have to specify the level/version of CSS you are using for
your documents.
The selector is followed by the formatting property definitions, which are enclosed in
braces ({ }).
When a browser reads a style sheet, it will format the HTML document according to the
information in the style sheet.
CSS Comments: are used to explain your code, and may help you when
you edit the source code at a later date. Comments are ignored by browsers.
Type selector: in this case the selectors are HTML tags. Like body, h1..h6,
p, table, ul, li etc.
h1 {
font-family: verdana,sans-serif; }
The id Selector
The id selector is used to specify a style for a single, unique element. It uses
the id attribute of the HTML element, and is defined with a hash sign (#).
Compiled by Dawit U. (M. Tech.) 12
The class Selector
The class selector is used to specify a style for a group of elements. Unlike the id
selector, the class selector is most often used on several elements.
This allows you to set a particular style for any HTML elements with the same
class.
The class selector uses the HTML class attribute, and is defined with a period (.)
You can also specify that only specific HTML elements should be affected by a
class.
eg. h1.center {text-align:center;}
To use inline styles you use the style attribute in the relevant tag. The
style attribute can contain any CSS property.
No selectors are needed in this type.
The example shows how to change the color and the left margin of a
paragraph:
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>
An inline style loses many of the advantages of style sheets by mixing content
with presentation.
Font-type Font-family
1. yellow
2. navy 6. purple 11. black 16. red
3. blue 7. fuchsia 12. silver 17. orange
4. teal 8. green 13. gray
5. aqua 9. lime 14. white
10. olive 15. maroon
direction: used to set the text direction
Values: rtl means right to left
The box model allows us to add a border around elements, and to define
space between elements.
In order to set the width and height of an element correctly in all browsers,
you need to know how the box model works.
Compiled by Dawit U. (M. Tech.) 42
CSS Margin properties
The CSS margin properties are used to create space around elements, outside
of any defined borders.
With CSS, you have full control over the margins. There are CSS properties
for setting the margin for each side of an element (top, right, bottom, and
left).
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
To shorten the code, it is possible to specify all the margin properties in one property. Use the
margin shorthand property with four values: