Professional Documents
Culture Documents
Introduction to CSS
Whay is Stylesheet?
Stylesheet is a collection of formatting styles which can be applied to a web page.
A style rule is a set of HTML tags specified for the formatting element.
A style rule can be divided into two parts : selector and declarator.
Each declaration includes a CSS property name and a value, separated by a colon.
A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by
curly braces.
CSS selector
CSS selectors are used to "find" (or select) HTML elements based on their
element name, id, class, attribute, and more.
Element Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be unique within a page, so the id selector is used to select one
unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element.
Class Selector
To select elements with a specific class, write a period (.) character, followed by the name of
the class.
Types of stylesheets
External Style Sheet :
With an external style sheet, you can change the look of an entire website by changing just
one file!
Each page must include a reference to the external style sheet file inside the <link> element.
The <link> element goes inside the <head> section:
An external style sheet can be written in any text editor. The file should not contain any
html tags. The style sheet file must be saved with a .css extension.
An internal style sheet may be used if one single page has a unique style.
Internal styles are defined within the <style> element, inside the <head> section of an HTML
page:
Inline Styles
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can
contain any CSS property.
CSS Units
CSS has several different units for expressing a length.
Many CSS properties take "length" values, such as width, margin, padding, font-size, border-
width, etc.
A whitespace cannot appear between the number and the unit. However, if the value is 0,
the unit can be omitted.
Unit Description
% Percentage
cm centimeters
mm millimeters
Relative to the font-size of the element (2em means 2 times the size of the
Em
current font)
CSS background
CSS background property defines the background effects on any document.
1. Background-color : The background-color property specifies the background color of an
element.
Syntax : background-color: { colorname | #RRGGBB | rgb(value1,value2,value3)}
The background of an element is the total size of the element, including padding and
border (but not the margin).
6. Background : The background shorthand property sets all the background properties in
one declaration.
1. Color : The color property is used to set the color of the text.
Syntax : color : { colorname | #RRGGBB | rgb(value1,value2,value3)}
8. Text-indent : The text-indent property specifies the indentation of the first line in a text-
block. Negative values are allowed. The first line will be indented to the left if the value is
negative.
Syntax : text-indent: {length}
9. Line-height : The line-height property specifies the line height. Negative values are not
allowed.
Syntax : line-height : {normal | length }
CSS font
The CSS font properties define the font family, boldness, size, and the style of a text.
1. Font-family : The name of a text is set with the font-family property. If the name of a font
family is more than one word, it must be in quotation marks, like: "Times New Roman".
Syntax : font-family : fontname
In a small-caps font, all lowercase letters are converted to uppercase letters. However,
the converted uppercase letters appears in a smaller font size than the original uppercase
letters in the text.
5. Font-weight: The font-weight property sets how thick or thin characters in text should be
displayed
Syntax : font-weight: {normal|bold|bolder|lighter|light |100 – 900 }
6. Font : The font shorthand property sets all the font properties in one declaration.
The properties that can be set, are (in order): "font-style font-variant font-weight font-
size/line-height font-family"
The font-size and font-family values are required. If one of the other values are missing,
the default values will be inserted, if any.
Syntax font: {font-style font-variant font-weight font-size/line-height font-family}
CSS border
The CSS border properties allow you to specify the style, width, and color of an element's
border.
Value Description
dotted Defines a dotted border
dashed Defines a dashed border
solid Defines a solid border
double Defines a double border
groove Defines a 3D grooved border. The effect
depends on the border-color value
ridge Defines a 3D ridged border. The effect
depends on the border-color value
Inset Defines a 3D inset border. The effect
depends on the border-color value
Outset Defines a 3D outset border. The effect
depends on the border-color value
none Defines no border
Hidden Defines a hidden border
The border-style property can have from one to four values (for the top border, right
border, bottom border, and the left border).
6. border-width : The border-width property specifies the width of the four borders.
The width can be set as a specific size (in px, pt, cm, em, etc) or by using one of the three
pre-defined values: thin, medium, or thick.
The border-width property can have from one to four values (for the top border, right
border, bottom border, and the left border).
10. border-left-width : The border-left-width property sets the width of an element's left
border.
Syntax : border-left-width : {thin | medium | thick | length}
11. border-color : The border-color property is used to set the color of the four borders. The
border-color property can have from one to four values (for the top border, right border,
bottom border, and the left border).
Syntax : border-color : { colorname | #RRGGBB | rgb(value1,value2,value3)}
12. Border-top-color : The border-top-color property sets the color of an element's top
border.
Syntax : border-top-color : { colorname | #RRGGBB | rgb(value1,value2,value3)}
13. Border-right-color : The border-right-color property sets the color of an element's right
border.
Syntax : border-right-color : { colorname | #RRGGBB | rgb(value1,value2,value3)}
15. Border-left-color : The border-left-color property sets the color of an element's left
border.
Syntax : border-left-color : { colorname | #RRGGBB | rgb(value1,value2,value3)}
16. Border : The border shorthand property sets all the border properties in one declaration.
The properties that can be set, are (in order): border-width, border-style, and border-
color.It does not matter if one of the values above are missing
CSS outline
The CSS outline properties specify the style, color, and width of an outline.
An outline is a line that is drawn around elements (outside the borders) to make the
element "stand out".
However, the outline property is different from the border property - The outline is NOT a
part of an element's dimensions; the element's total width and height is not affected by the
width of the outline.
2. Outline-width : The outline-width property specifies the width of the four outlines.
The outline can be set as a specific size (in px, pt, cm, em, etc) or by using one of the
three pre-defined values: thin, medium, or thick.Syntax : outline-width : {thin | medium
| thick | length}
3. Outline-color : An outline is a line that is drawn around elements (outside the borders) to
make the element "stand out".
4. Outline : An outline is a line that is drawn around elements (outside the borders) to make
the element "stand out".
The outline shorthand property sets all the outline properties in one declaration.
The properties that can be set, are (in order): outline-color, outline-style, outline-width.
If one of the values above are missing, e.g. "outline:solid #ff0000;", the default value for
the missing property will be inserted, if any.
The CSS margin properties are used to generate space around elements.
The margin properties set the size of the white space outside the border.
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).
5. Margin : The margin shorthand property sets all the margin properties in one
declaration. This property can have from one to four values.
Syntax : margin : length {1,4}
CSS padding
The CSS padding properties are used to generate space around content.
The padding clears an area around the content (inside the border) of an element.
With CSS, you have full control over the padding. There are CSS properties for setting the
padding for each side of an element (top, right, bottom, and left).
CSS list
1. List-style-type : The list-style-type property specifies the type of list item marker.
4. List-style : The list-style shorthand property sets all the list properties in one declaration.
The properties that can be set, are (in order): list-style-type, list-style-position, list-style-
image.
If one of the values above are missing, e.g. "list-style:circle inside;", the default value for
the missing property will be inserted, if any.
CSS table
1. Table-layout : The table-layout property sets the table layout algorithm to be used for a
table.
Syntax table-layout: {auto|fixed}
2. Border-collapse: The border-collapse property sets whether the table borders are
collapsed into a single border or detached as in standard HTML.
Syntax : border-collapse : { separate | collapse }
3. Border-spacing : The border-spacing property sets the distance between the borders of
adjacent cells (only for the "separated borders" model).
Syntax : border-spacing: length
4. Empty-cells : The empty-cells property sets whether or not to display borders and
background on empty cells in a table (only for the "separated borders" model).
Syntax : empty-cells : {show | hide }
CSS links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
In addition, links can be styled differently depending on what state they are in.
<font>…</font> :
The <font> tag specifies the font face, font size, and color of text. The <font> tag is not
supported in HTML5. Use CSS instead.
1. face : The face attribute specifies the font of the text inside a <font> element.
Syntax : face= {fontname}
2. color : The color attribute specifies the color of the text inside a <font> element.
Syntax : color ={ colorname | #RRGGBB | rgb(value1,value2,value3)}
3. size : The size attribute specifies the size of the text inside a <font> element. A number
from 1 to 7 that defines the size of the text. Browser default is 3. You can specify the font
size with either absolute number or relative number. You can specify how many sizes
larger or how many sizes smaller than the preset font size should be. You can specify it
like <font size="+n"> or <font size="-n">
Syntax : size = number
<basefont > :
The <basefont> element is supposed to set a default font size, color, and typeface for any
parts of the document that are not otherwise contained within a <font> tag. You can use
the <font> elements to override the <basefont> settings.
The <basefont> tag also takes color, size and face attributes and it will support relative font
setting by giving size a value of +1 for a size larger or -2 for two sizes smaller.