All CSS Background Properties
All CSS Background Properties
Example
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:right top;
}
Example
h1 {background-color:#6495ed;}
p {background-color:#e0ffff;}
div {background-color:#b0c4de;}
CSS Font :-
CSS font properties define the font family, boldness, size, and the style of a text.
On computer screens, sans-serif fonts are considered easier to read than serif fonts.
CSS Font Families
In CSS, there are two types of font family names:
generic family - a group of font families with a similar look (like "Serif" or
"Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")
Monospace
Courier New All monospace characters have the same
Lucida Console width
<head>
<style type="text/css">
p.sansserif{font-family:Arial,Helvetica,sans-serif;}
</style>
</head>
<body>
<h1>CSS font-family</h1>
</body>
</html>
O/P:
CSS font-family
CS
Property Description Values
S
font-style
font-variant
font-weight
font-size/line-height
font-family
Sets all the font properties in one caption
font 1
declaration icon
menu
message-box
small-caption
status-bar
inherit
family-name
font-family Specifies the font family for text generic-family 1
inherit
xx-small
x-small
small
medium
large
x-large
font-size Specifies the font size of text 1
xx-large
smaller
larger
length
%
inherit
normal
italic
font-style Specifies the font style for text 1
oblique
inherit
normal
Specifies whether or not a text should
font-variant small-caps 1
be displayed in a small-caps font
inherit
font-weight Specifies the weight of a font normal 1
bold
bolder
lighter
100
200
300
400
500
600
700
800
900
inherit
Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
Special for links are that they can be styled differently depending on what state they are in.
Example
a:link {color:#FF0000;} /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;} /* mouse over link */
a:active {color:#0000FF;} /* selected link */
Text Decoration
The text-decoration property is mostly used to remove underlines from links:
Example
a:link {text-decoration:none;}
a:visited {text-decoration:none;}
a:hover {text-decoration:underline;}
a:active {text-decoration:underline;}
CSS Lists
The CSS list properties allow you to:
List
In HTML, there are two types of lists:
With CSS, lists can be styled further, and images can be used as the list item marker.
Example
ul.a {list-style-type: circle;}
ul.b {list-style-type: square;}
ome of the property values are for unordered lists, and some for ordered lists.
Example
ul
{
list-style-image: url('sqpurple.gif');
}
The example above does not display equally in all browsers. IE and Opera will display the image-
marker a little bit higher than Firefox, Chrome, and Safari.
If you want the image-marker to be placed equally in all browsers, a crossbrowser solution is
explained below.
Crossbrowser Solution
The following example displays the image-marker equally in all browsers:
Example
ul
{
list-style-type: none;
padding: 0px;
margin: 0px;
}
li
{
background-image: url(sqpurple.gif);
background-repeat: no-repeat;
background-position: 0px 5px;
padding-left: 14px;
}
Example
ul
{
list-style: square url("sqpurple.gif");
}
When using the shorthand property, the order of the values are:
list-style-type
list-style-position (for a description, see the CSS properties table below)
list-style-image
It does not matter if one of the values above are missing, as long as the rest are in the
specified order.
CSS Tables:-
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for table, th, and td elements:
Example
table, th, td
{
border: 1px solid black;
}
Collapse Borders
The border-collapse property sets whether the table borders are collapsed into a single border
or separated:
Example
table
{
border-collapse:collapse;
}
table,th, td
{
border: 1px solid black;
}
The example below sets the width of the table to 100%, and the height of the th elements to
50px:
Example
table
{
width:100%;
}
th
{
height:50px;
}
The text-align property sets the horizontal alignment, like left, right, or center:
Example
td
{
text-align:right;
}
The vertical-align property sets the vertical alignment, like top, bottom, or middle:
Example
td
{
height:50px;
vertical-align:bottom;
}
Table Padding
To control the space between the border and content in a table, use the padding property on td
and th elements:
Example
td
{
padding:15px;
}
Table Color
The example below specifies the color of the borders, and the text and background color of th
elements:
Example
table, td, th
{
border:1px solid green;
}
th
{
background-color:green;
color:white;
}
The CSS Box Model
All HTML elements can be considered as boxes. In CSS, the term "box model" is used when
talking about design and layout.
The CSS box model is essentially a box that wraps around HTML elements, and it consists
of: margins, borders, padding, and the actual content.
The box model allows us to place a border around elements and space elements in relation to
other elements.
Margin - Clears an area around the border. The margin does not have a background color, it
is completely transparent
Border - A border that goes around the padding and content. The border is affected by the
background color of the box
Padding - Clears an area around the content. The padding is affected by the background
color of the box
Content - The content of the box, where text and images appear
In order to set the width and height of an element correctly in all browsers, you need to know
how the box model works.
Width and Height of an Element
Important: When you specify the width and height properties of an element with CSS, you
are just setting the width and height of the content area. To know the full size of the element,
you must also add the padding, border and margin.
width:250px;
padding:10px;
border:5px solid gray;
margin:10px;
Imagine that you only had 250px of space. Let's make an element with a total width of
250px:
Example
width:220px;
padding:10px;
border:5px solid gray;
margin:0px;
Total element width = width + left padding + right padding + left border + right border + left
margin + right margin
Total element height = height + top padding + bottom padding + top border + bottom border
+ top margin + bottom margin
Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
div.ex
{
width:220px;
padding:10px;
border:5px solid gray;
margin:0px;
}
</style>
</head>
CSS Border
Border Style
The border-style property specifies what kind of border to display.
None of the border properties will have ANY effect unless the border-style property is set!
Border-style values:
none: Defines no border
dotted: Defines a dotted border
double: Defines two borders. The width of the two borders are the same as the border-width
value
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
Border Width
The border-width property is used to set the width of the border.
The width is set in pixels, or by using one of the three pre-defined values: thin, medium, or
thick.
Note: The "border-width" property does not work if it is used alone. Use the "border-style"
property to set the borders first.
Example
p.one
{
border-style:solid;
border-width:5px;
}
p.two
{
border-style:solid;
border-width:medium;
}
Border Color
The border-color property is used to set the color of the border. The color can be set by:
Note: The "border-color" property does not work if it is used alone. Use the "border-style"
property to set the borders first.
Example
p.one
{
border-style:solid;
border-color:red;
}
p.two
{
border-style:solid;
border-color:#98bf21;
}
Border - Individual sides
In CSS it is possible to specify different borders for different sides:
Example
p
{
border-top-style:dotted;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:solid;
}
Example
border-style:dotted solid;
The border-style property is used in the example above. However, it also works with border-
width and border-color.
Border - Shorthand property
As you can see from the examples above, there are many properties to consider when dealing
with borders.
To shorten the code, it is also possible to specify all the border properties in one property.
This is called a shorthand property.
Example
border:5px solid red;
When using the border property, the order of the values are:
border-width
border-style
border-color
It does not matter if one of the values above are missing (although, border-style is required),
as long as the rest are in the specified order.
More Examples
All the top border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the top
border in one declaration.
border-width
Sets all the border properties in one
border border-style 1
declaration
border-color
border-bottom-width
Sets all the bottom border properties in one
border-bottom border-bottom-style 1
declaration
border-bottom-color
color_name
hex_number
border-color Sets the color of the four borders rgb_number 1
transparent
inherit
border-left-width
Sets all the left border properties in one
border-left border-left-style 1
declaration
border-left-color
border-right-width
Sets all the right border properties in one
border-right border-right-style 1
declaration
border-right-color
border-top-width
Sets all the top border properties in one
border-top border-top-style 1
declaration
border-top-color
thin
medium
border-width Sets the width of the four borders thick 1
length
inherit
CSS Outlines
An outline is a line that is drawn around elements, outside the border edge, to make the
element "stand out".
The outline properties specifies the style, color, and width of an outline.
outline-color
outline-style
outline Sets all the outline properties in one declaration 2
outline-width
inherit
none
dotted
dashed
solid
double
outline-style Sets the style of an outline 2
groove
ridge
inset
outset
inherit
thin
medium
outline-width Sets the width of an outline thick 2
length
inherit
Margin
The margin clears an area around an element (outside the border). The margin does not have
a background color, and is completely transparent.
The top, right, bottom, and left margin can be changed independently using separate
properties. A shorthand margin property can also be used, to change all margins at once.
Possible Values
Value Description
Example
margin:100px 50px;
margin:25px 50px;
o top and bottom margins are 25px
o right and left margins are 50px
margin:25px;
o all four margins are 25px
More Examples
Set the top margin of a text using a cm value
This example demonstrates how to set the top margin of a text using a cm value.
margin-top
A shorthand property for setting the margin margin-right
margin 1
properties in one declaration margin-bottom
margin-left
auto
margin-bottom Sets the bottom margin of an element length 1
%
auto
margin-left Sets the left margin of an element length 1
%
auto
margin-right Sets the right margin of an element length 1
%
auto
margin-top Sets the top margin of an element length 1
%
CSS Padding
The CSS padding properties define the space between the element border and the element
content.
Padding
The padding clears an area around the content (inside the border) of an element. The padding
is affected by the background color of the element.
The top, right, bottom, and left padding can be changed independently using separate
properties. A shorthand padding property can also be used, to change all paddings at once.
Possible Values
Value Description
Example
padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
Example
padding:25px 50px;
padding-top
A shorthand property for setting all the padding padding-right
padding 1
properties in one declaration padding-bottom
padding-left
length
padding-bottom Sets the bottom padding of an element 1
%
length
padding-left Sets the left padding of an element 1
%
length
padding-right Sets the right padding of an element 1
%
length
padding-top Sets the top padding of an element 1
%