Вы находитесь на странице: 1из 62

What is CSS?

• CSS stands for Cascading Style Sheets


• Styles define how to display HTML elements
• Styles are normally stored in Style Sheets
• Styles were added to HTML 4.0 to solve a problem
• External Style Sheets can save you a lot of work
• External Style Sheets are stored in CSS files
• Multiple style definitions will cascade into one

CSS Syntax

The CSS syntax is made up of three parts: a selector, a property and a value:

selector {

property: value

The selector is normally the HTML element/tag you wish to define,

The property is the attribute you wish to change, and each property can take a value.

The property and value are separated by a colon, and surrounded by curly braces:

body {color: black}

Note: If the value is multiple words, put quotes around the value:

p {font-family: "sans serif"}

Note: If you wish to specify more than one property, you must separate each property with a semicolon.
The example below shows how to define a center aligned paragraph, with a red text color:

p {text-align:center;color:red}

To make the style definitions more readable, you can describe one property on each line, like this:

p
{
text-align: center;
color: black;
font-family: arial
}
Grouping

You can group selectors. Separate each selector with a comma. In the example below we have grouped all
the header elements. All header elements will be displayed in green text color:

h1,h2,h3,h4,h5,h6
{
color: green
}

The class Selector

With the class selector you can define different styles for the same type of HTML element.

Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph,
and one center-aligned paragraph. Here is how you can do it with styles:

p.right {text-align: right}

p.center {text-align: center}

You have to use the class attribute in your HTML document:

<p class="right">
This paragraph will be right-aligned.
</p>
<p class="center">
This paragraph will be center-aligned.

</p>

Note: To apply more than one class per given element, the syntax is:

<p class="center bold">


This is a paragraph.

</p>

The paragraph above will be styled by the class "center" AND the class "bold".
Example:

<HTML>

<HEAD>

<style>

.center

color:red;

.bold

text-align:center;

</style>

</HEAD>

<BODY>

<p class="center bold">

This is a paragraph.

</p>

</BODY>

</HTML>

You can also omit the tag name in the selector to define a style that will be used by all HTML elements
that have a certain class. In the example below, all HTML elements with class="center" will be center-
aligned:
.center

text-align: center

In the code below both the h1 element and the p element have class="center". This means that both
elements will follow the rules in the ".center" selector:

<h1 class="center">
This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p>

Do NOT start a class name with a number! It will not work in Mozilla/Firefox.

Add Styles to Elements with Particular Attributes

You can also apply styles to HTML elements with particular attributes.

The style rule below will match all input elements that have a type attribute with a value of "text":

input[type="text"] {background-color: blue}

The id Selector

You can also define styles for HTML elements with the id selector. The id selector is defined as a #.

The style rule below will match the element that has an id attribute with a value of "green":

#green {color: green}

The style rule below will match the p element that has an id with a value of "para1":

p#para1
{
text-align: center;
color: red;
}

Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.


CSS Comments

Comments are used to explain your code, and may help you when you edit the source code at a later date.
A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this:

/* This is a comment */
p
{
text-align: center;
/* This is another comment */
color: black;
font-family: arial

External Style Sheet

An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you
can change the look of an entire Web site by changing one file. Each page must link to the style sheet
using the <link> tag. The <link> tag goes inside the head section:

<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" /></head>

The browser will read the style definitions from the file mystyle.css, and format the document according
to it.

An external style sheet can be written in any text editor. The file should not contain any html tags. Your
style sheet should be saved with a .css extension. An example of a style sheet file is shown below:

hr {color: sienna}
p {margin-left: 20px}
body { background-image : url("images/back40.gif")}

Do NOT leave spaces between the property value and the units! If you use "margin-left: 20 px" instead of
"margin-left: 20px" it will only work properly in IE6 but it will not work in Mozilla/Firefox or Netscape.

Internal Style Sheet

An internal style sheet should be used when a single document has a unique style. You define internal
styles in the head section by using the <style> tag, like this:

<head>
<style type="text/css">
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
</style>
</head>

The browser will now read the style definitions, and format the document according to it.

Note: A browser normally ignores unknown tags. This means that an old browser that does not support
styles, will ignore the <style> tag, but the content of the <style> tag will be displayed on the page. It is
possible to prevent an old browser from displaying the content by hiding it in the HTML comment
element:

<head>
<style type="text/css">
<!--
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
-->
</style>

</head>

Inline Styles

An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this
method sparingly, such as when a style is to be applied to a single occurrence of an element.

To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS
property. 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>

Multiple Style Sheets

If some properties have been set for the same selector in different style sheets, the values will be inherited
from the more specific style sheet.

For example, an external style sheet has these properties for the h3 selector:

h3
{
color: red;
text-align: left;
font-size: 8pt
}

And an internal style sheet has these properties for the h3 selector:
h3
{
text-align: right;
font-size: 20pt
}

If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:

color: red;
text-align: right;
font-size: 20pt

The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by
the internal style sheet.

CSS Background Properties:

The CSS background properties allow you to control the background color of an element, set an image as
the background, repeat a background image vertically or horizontally, and position an image on a page.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

background A shorthand property for background-color 4 1 6 1


setting all background background-image
properties in one background-repeat
declaration background-
attachment
background-position

background- Sets whether a scroll 4 1 6 1


attachment background image is fixed
fixed or scrolls with the
rest of the page

background-color Sets the background color-rgb 4 1 4 1


color of an element color-hex
color-name
transparent

background-image Sets an image as the url(URL) 4 1 4 1


background none

background-position Sets the starting top left 4 1 6 1


position of a background top center
image top right
center left
center center
center right
bottom left
bottom center
bottom right
x% y%
xpos ypos

background-repeat Sets if/how a repeat 4 1 4 1


background image will repeat-x
be repeated repeat-y
no-repeat

Set the background color


This example demonstrates how to set the background color for an element.<html>

<head>

<style type="text/css">

body {background-color: yellow}

h1 {background-color: #00ff00}

h2 {background-color: transparent}

p {background-color: rgb(250,0,255)}

</style>

</head>

<body>

<h1>This is header 1</h1>


<h2>This is header 2</h2>

<p>This is a paragraph</p>

</body>

</html>

Background images:

<html>

<head>

<style type="text/css">

body

background-image:url('bgdesert.jpg')

</style>

</head>

<body>

</body>

</html>

CSS Text Properties

The CSS text properties allow you to control the appearance of text. It is possible to change the color of a
text, increase or decrease the space between characters in a text, align a text, decorate a text, indent the
first line in a text, and more.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.


W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

color Sets the color of a text color 3 1 4 1

direction Sets the text direction ltr 6 1 6 2


rtl

line-height Sets the distance normal 4 1 4 1


between lines number
length
%

letter-spacing Increase or decrease the normal 4 1 6 1


space between length
characters

text-align Aligns the text in an left 4 1 4 1


element right
center
justify

text-decoration Adds decoration to text none 4 1 4 1


underline
overline
line-through
blink

text-indent Indents the first line of length 4 1 4 1


text in an element %

text-shadow none
color
length

text-transform Controls the letters in an none 4 1 4 1


element capitalize
uppercase
lowercase

white-space Sets how white space normal 5 1 4 1


inside an element is pre
handled nowrap

word-spacing Increase or decrease the normal 6 1 6 1


space between words length

CSS Font Properties

The CSS font properties allow you to change the font family, boldness, size, and the style of a text.

Note: In CSS1 fonts are identified by a font name. If a browser does not support the specified font, it will
use a default font.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

font A shorthand property for font-style 4 1 4 1


setting all of the font-variant
properties for a font in font-weight
one declaration font-size/line-height
font-family
caption
icon
menu
message-box
small-caption
status-bar

font-family A prioritized list of font family-name 3 1 4 1


family names and/or generic-family
generic family names for
an element

font-size Sets the size of a font xx-small 3 1 4 1


x-small
small
medium
large
x-large
xx-large
smaller
larger
length
%

font-size-adjust Specifies an aspect none - - - 2


value for an element number
that will preserve the x-
height of the first-choice
font

font-stretch Condenses or expands normal - - - 2


the current font-family wider
narrower
ultra-condensed
extra-condensed
condensed
semi-condensed
semi-expanded
expanded
extra-expanded
ultra-expanded

font-style Sets the style of the font normal 4 1 4 1


italic
oblique

font-variant Displays text in a small- normal 4 1 6 1


caps font or a normal small-caps
font

font-weight Sets the weight of a font normal 4 1 4 1


bold
bolder
lighter
100
200
300
400
500
600
700
800
900

CSS Border Properties

The CSS border properties allow you to specify the style and color of an element's border. In HTML we
use tables to create borders around a text, but with the CSS border properties we can create borders with
nice effects, and it can be applied to any element.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

border A shorthand property for border-width 4 1 4 1


setting all of the border-style
properties for the four border-color
borders in one
declaration

border-bottom A shorthand property for border-bottom- 4 1 6 1


setting all of the width
properties for the border-style
bottom border in one border-color
declaration

border-bottom-color Sets the color of the border-color 4 1 6 2


bottom border

border-bottom-style Sets the style of the border-style 4 1 6 2


bottom border

border-bottom-width Sets the width of the thin 4 1 4 1


bottom border medium
thick
length

border-color Sets the color of the four color 4 1 6 1


borders, can have from
one to four colors

border-left A shorthand property for border-left-width 4 1 6 1


setting all of the border-style
properties for the left border-color
border in one
declaration

border-left-color Sets the color of the left border-color 4 1 6 2


border

border-left-style Sets the style of the left border-style 4 1 6 2


border

border-left-width Sets the width of the left thin 4 1 4 1


border medium
thick
length

border-right A shorthand property for border-right-width 4 1 6 1


setting all of the border-style
properties for the right border-color
border in one
declaration

border-right-color Sets the color of the border-color 4 1 6 2


right border

border-right-style Sets the style of the border-style 4 1 6 2


right border

border-right-width Sets the width of the thin 4 1 4 1


right border medium
thick
length

border-style Sets the style of the four none 4 1 6 1


borders, can have from hidden
one to four styles dotted
dashed
solid
double
groove
ridge
inset
outset

border-top A shorthand property for border-top-width 4 1 6 1


setting all of the border-style
properties for the top border-color
border in one
declaration

border-top-color Sets the color of the top border-color 4 1 6 2


border

border-top-style Sets the style of the top border-style 4 1 6 2


border

border-top-width Sets the width of the top thin 4 1 4 1


border medium
thick
length

border-width A shorthand property for thin 4 1 4 1


setting the width of the medium
four borders in one thick
declaration, can have length
from one to four values

CSS Outline Properties

An outline is a line that is drawn around elements, outside the border edge, to make the element "stand
out".

The CSS outline properties sets the outlines around elements. You can specify the style, color, and width
of the outline.

Note: Outlines do not take up space, and they do not have to be rectangular.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
Property Description Values IE F N W3C

outline A shorthand property for outline-color - 1. - 2


setting all the outline outline-style 5
properties in one outline-width
declaration

outline-color Sets the color of the color - 1. - 2


outline around an invert 5
element

outline-style Sets the style of the none - 1. - 2


outline around an dotted 5
element dashed
solid
double
groove
ridge
inset
outset

outline-width Sets the width of the thin - 1. - 2


outline around an medium 5
element thick
length

CSS Margin Properties

The CSS margin properties define the space around elements. It is possible to use negative values to
overlap content. 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 of the margins at once.

Note: Netscape and IE give the body tag a default margin of 8px. Opera does not! Instead, Opera applies
a default padding of 8px, so if one wants to adjust the margin for an entire page and have it display
correctly in Opera, the body padding must be set as well!

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C


margin A shorthand property for margin-top 4 1 4 1
setting the margin margin-right
properties in one margin-bottom
declaration margin-left

margin-bottom Sets the bottom margin auto 4 1 4 1


of an element length
%

margin-left Sets the left margin of auto 3 1 4 1


an element length
%

margin-right Sets the right margin of auto 3 1 4 1


an element length
%

margin-top Sets the top margin of auto 3 1 4 1


an element length
%

CSS Padding Properties

The CSS padding properties define the space between the element border and the element content.
Negative values are not allowed. The top, right, bottom, and left padding can be changed independently
using separate properties. A shorthand padding property is also created to control multiple sides at once.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

padding A shorthand property for padding-top 4 1 4 1


setting all of the padding-right
padding properties in padding-bottom
one declaration padding-left

padding-bottom Sets the bottom padding length 4 1 4 1


of an element %
padding-left Sets the left padding of length 4 1 4 1
an element %

padding-right Sets the right padding of length 4 1 4 1


an element %

padding-top Sets the top padding of length 4 1 4 1


an element %

CSS List Properties

The CSS list properties allow you to place the list-item marker, change between different list-item
markers, or set an image as the list-item marker.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

list-style A shorthand property for list-style-type 4 1 6 1


setting all of the list-style-position
properties for a list in list-style-image
one declaration

list-style-image Sets an image as the none 4 1 6 1


list-item marker url

list-style-position Sets where the list-item inside 4 1 6 1


marker is placed in the outside
list

list-style-type Sets the type of the list- none 4 1 4 1


item marker disc
circle
square
decimal
decimal-leading-
zero
lower-roman
upper-roman
lower-alpha
upper-alpha
lower-greek
lower-latin
upper-latin
hebrew
armenian
georgian
cjk-ideographic
hiragana
katakana
hiragana-iroha
katakana-iroha

marker-offset auto 1 7 2
length

CSS Table Properties

The CSS table properties allow you to set the layout of a table.

Browser support: IE: Internet Explorer, M: Mac IE only, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

border-collapse Sets whether the table collapse 5 1 7 2


borders are collapsed separate
into a single border or
detached as in standard
HTML

border-spacing Sets the distance that length length 5M 1 6 2


separates cell borders
(only for the "separated
borders" model)

caption-side Sets the position of the top 5M 1 6 2


table caption bottom
left
right

empty-cells Sets whether or not to show 5M 1 6 2


show empty cells in a hide
table (only for the
"separated borders"
model)

table-layout Sets the algorithm used auto 5 1 6 2


to display the table fixed
cells, rows, and columns

CSS Dimension Properties

The CSS dimension properties allow you to control the height and width of an element. It also allows you
to increase the space between two lines.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

height Sets the height of an auto 4 1 6 1


element length
%

line-height Sets the distance normal 4 1 4 1


between lines number
length
%

max-height Sets the maximum none - 1 6 2


height of an element length
%

max-width Sets the maximum none - 1 6 2


width of an element length
%

min-height Sets the minimum length - 1 6 2


height of an element %

min-width Sets the minimum width length - 1 6 2


of an element %

width Sets the width of an auto 4 1 4 1


element %
length

CSS Classification Properties

The CSS classification properties allow you to control how to display an element, set where an image will
appear in another element, position an element relative to its normal position, position an element using
an absolute value, and how to control the visibility of an element.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

clear Sets the sides of an left 4 1 4 1


element where other right
floating elements are both
not allowed none

cursor Specifies the type of url 4 1 6 2


cursor to be displayed auto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help

display Sets how/if an element none 4 1 4 1


is displayed inline
block
list-item
run-in
compact
marker
table
inline-table
table-row-group
table-header-group
table-footer-group
table-row
table-column-group
table-column
table-cell
table-caption

float Sets where an image or left 4 1 4 1


a text will appear in right
another element none

position Places an element in a static 4 1 4 2


static, relative, absolute relative
or fixed position absolute
fixed

visibility Sets if an element visible 4 1 6 2


should be visible or hidden
invisible collapse

CSS Positioning Properties

The CSS positioning properties allow you to specify the left, right, top, and bottom position of an
element. It also allows you to set the shape of an element, place an element behind another, and to specify
what should happen when an element's content is too big to fit in a specified area.

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.


W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).

Property Description Values IE F N W3C

bottom Sets how far the bottom auto 5 1 6 2


edge of an element is %
above/below the bottom length
edge of the parent
element

clip Sets the shape of an shape 4 1 6 2


element. The element is auto
clipped into this shape,
and displayed

left Sets how far the left auto 4 1 4 2


edge of an element is to %
the right/left of the left length
edge of the parent
element

overflow Sets what happens if the visible 4 1 6 2


content of an element hidden
overflow its area scroll
auto

position Places an element in a static 4 1 4 2


static, relative, absolute relative
or fixed position absolute
fixed

right Sets how far the right auto 5 1 6 2


edge of an element is to %
the left/right of the right length
edge of the parent
element

top Sets how far the top auto 4 1 4 2


edge of an element is %
above/below the top length
edge of the parent
element

vertical-align Sets the vertical baseline 4 1 4 1


alignment of an element sub
super
top
text-top
middle
bottom
text-bottom
length
%

z-index Sets the stack order of auto 4 1 6 2


an element number

pseudo-classes:

Syntax

The syntax of pseudo-classes:

selector:pseudo-class {property: value}

CSS classes can also be used with pseudo-classes:

selector.class:pseudo-class {property: value}

Anchor Pseudo-classes

A link that is active, visited, unvisited, or when you mouse over a link can all be displayed in different
ways in a CSS-supporting browser:

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 */

Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective!!
Note: a:active MUST come after a:hover in the CSS definition in order to be effective!!

Note: Pseudo-class names are not case-sensitive.

Pseudo-elements Syntax

The syntax of pseudo-elements:

selector:pseudo-element {property: value}

CSS classes can also be used with pseudo-elements:

selector.class:pseudo-element {property: value}

The :first-line Pseudo-element

The "first-line" pseudo-element is used to add special styles to the first line of the text in a selector:

p:first-line {color:#0000ff;font-variant:small-caps}
<p>Some text that ends up on two or more lines</p>

The output could be something like this:

Some text that ends


up on two or more lines

In the example above the browser displays the first line formatted according to the "first-line" pseudo
element. Where the browser breaks the line depends on the size of the browser window.

Note: The "first-line" pseudo-element can only be used with block-level elements.

Note: The following properties apply to the "first-line" pseudo-element:

• font properties
• color properties
• background properties
• word-spacing
• letter-spacing
• text-decoration
• vertical-align
• text-transform
• line-height
• clear
The :first-letter Pseudo-element

The "first-letter" pseudo-element is used to add special style to the first letter of the text in a selector:

p:first-letter {color:#ff0000;font-size:xx-large}
<p>The first words of an article...</p>

The output could be something like this:

The first words of an article...

Note: The "first-letter" pseudo-element can only be used with block-level elements.

Note: The following properties apply to the "first-letter" pseudo- element:

• font properties
• color properties
• background properties
• margin properties
• padding properties
• border properties
• text-decoration
• vertical-align (only if 'float' is 'none')
• text-transform
• line-height
• float
• clear

Pseudo-elements and CSS Classes

Pseudo-elements can be combined with CSS classes:

p.article:first-letter {color:#ff0000}
<p class="article">A paragraph in an article</p>

The example above will make the first letter of all paragraphs with class="article" red.

Multiple Pseudo-elements

Several pseudo-elements can be combined:

p:first-letter {color:#ff0000;font-size:xx-large}
p:first-line {color:#0000ff}
<p>The first words of an article...</p>

The output could be something like this:

The first
words of an
article...

In the example above the first letter of the paragraph will be red with a font size of 24pt. The rest of the
first line would be blue while the rest of the paragraph would be the default color.

CSS2 - The :before Pseudo-element

The ":before" pseudo-element can be used to insert some content before the content of an element.

The style below will play a sound before each occurrence of an <h1> element:

h1:before
{
content: url(beep.wav)
}

CSS2 - The :after Pseudo-element

The ":after" pseudo-element can be used to insert some content the content of an element.

The style below will play a sound after each occurrence of an <h1> element:

h1:after
{
content: url(beep.wav)
}

Pseudo-elements

Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.

W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
Pseudo-element Purpose IE F N W3C

:first-letter Adds special style to the first letter of a text 5 1 8 1

:first-line Adds special style to the first line of a text 5 1 8 1

:before Inserts some content before the content of an 1. 8 2


element 5

:after Inserts some content after the content of an 1. 8 2


element 5

Outline

Draw a line around an element, outside the border edge (outline) (Does not work in IE)

<html>

<head>

<style type="text/css">

border: red solid thin;

outline: green dotted thick

</style>

</head>

<body>

<p>Some text.</p>
</body>

</html>
Set the style of an outline (Does not work in IE)

<html>

<head>

<style type="text/css">

border: red solid thin;

p.dotted {outline-style: dotted}

p.dashed {outline-style: dashed}

p.solid {outline-style: solid}

p.double {outline-style: double}

p.groove {outline-style: groove}

p.ridge {outline-style: ridge}

p.inset {outline-style: inset}

p.outset {outline-style: outset}

</style>

</head>

<body>

<p class="dotted">A dotted outline</p>


<p class="dashed">A dashed outline</p>

<p class="solid">A solid outline</p>

<p class="double">A double outline</p>

<p class="groove">A groove outline</p>

<p class="ridge">A ridge outline</p>

<p class="inset">An inset outline</p>

<p class="outset">An outset outline</p>

</body>

</html>

Set the color of an outline (Does not work in IE)

<html>

<head>

<style type="text/css">

border: red solid thin;

outline-style: solid;
outline-color: #00ff00

</style>

</head>

<body>

<p>Some text.</p>

</body>

</html>
Set the width of an outline (Does not work in IE)

<html>

<head>

<style type="text/css">

p.one

border: red solid thin;

outline-style: solid;

outline-width: thick

p.two

border: red solid thin;

outline-style: solid;
outline-width: 2px

</style>

</head>

<body>

<p class="one">Some text.</p>

<p class="two">Some text.</p>

</body>

</html>

Margin

All the margin properties in one declaration

<html>

<head>

<style type="text/css">

p.margin {margin: 2cm 4cm 3cm 4cm}

</style>

</head>

<body>

<p>This is a paragraph with no specified margins</p>


<p class="margin">This is a paragraph with specified margins</p>

<p>This is a paragraph with no specified margins</p>

</body>

</html>

Set the top margin of a text using a cm value


Set the top margin of a text using a percent value

<html>

<head>

<style type="text/css">

p.topmargin

margin-top: 25%

</style>

</head>

<body>

<p>This is a paragraph with no margin specified</p>

<p class="topmargin">This is a paragraph with a specified top margin</p>

</body>
</html>

All the padding properties in one declaration

<html>

<head>

<style type="text/css">

td.test1 {padding: 1.5cm}

td.test2 {padding: 0.5cm 2.5cm}

</style>

</head>

<body>

<table border="1">

<tr>

<td class="test1">

This is a tablecell with equal padding on each side.

</td>

</tr>

</table>

<br />

<table border="1">

<tr>

<td class="test2">
This tablecell has a top and bottom padding of 0.5cm and a left and right padding of 2.5cm.

</td>

</tr>

</table>

</body>

Set the left padding of a tablecell

</html>

<html>

<head>

<style type="text/css">

td {padding-left: 2cm}

</style>

</head>

<body>

<table border="1">

<tr>

<td>

This is a tablecell with a left padding. This is a tablecell with a left padding.

</td>

</tr>

</table>

</body>
</html>

Table

Set the layout of a table

<html>

<head>

<style type="text/css">

table.one

table-layout: automatic

table.two

table-layout: fixed

</style>

</head>

<body>

<table class="one" border="1" width="100%">

<tr>

<td width="20%">1000000000000000000000000000</td>

<td width="40%">10000000</td>
<td width="40%">100</td>

</tr>

</table>

<br />

<table class="two" border="1" width="100%">

<tr>

<td width="20%">1000000000000000000000000000</td>

<td width="40%">10000000</td>

<td width="40%">100</td>

</tr>

</table>

</body>

</html>

Show empty cells in a table

<html>

<head>

<style type="text/css">

table

border-collapse: separate;
empty-cells: show

</style>

</head>

<body>

<table border="1">

<tr>

<td>Peter</td>

<td>Griffin</td>

</tr>

<tr>

<td>Lois</td>

<td></td>

</tr>

</table>

</body>

</html>

Collapse a table border

<html>

<head>

<style type="text/css">
table.coll

border-collapse: collapse

table.sep

border-collapse: separate

</style>

</head>

<body>

<table class="coll" border="1">

<tr>

<td>Peter</td>

<td>Griffin</td>

</tr>

<tr>

<td>Lois</td>

<td>Griffin</td>

</tr>

</table>
<br />

<table class="sep" border="1">

<tr>

<td>Peter</td>

<td>Griffin</td>

</tr>

<tr>

<td>Lois</td>

<td>Griffin</td>

</tr>

</table>

</body>

</html>

Set the space between table borders

<html>

<head>

<style type="text/css">

table.one

border-collapse: separate;

border-spacing: 10px
}

table.two

border-collapse: separate;

border-spacing: 10px 50px

</style>

</head>

<body>

<table class="one" border="1">

<tr>

<td>Peter</td>

<td>Griffin</td>

</tr>

<tr>

<td>Lois</td>

<td>Griffin</td>

</tr>

</table>

<br />
<table class="two" border="1">

<tr>

<td>Cleveland</td>

<td>Brown</td>

</tr>

<tr>

<td>Glenn</td>

<td>Quagmire</td>

</tr>

</table>

</body>

</html>

Set the position of the table caption

<html>

<head>

<style type="text/css">

caption

caption-side:bottom

</style>

</head>
<body>

<table border="1">

<caption>This is a caption</caption>

<tr>

<td>Peter</td>

<td>Griffin</td>

</tr>

<tr>

<td>Lois</td>

<td>Griffin</td>

</tr>

</table>

</body>

</html>

Dimension

Set the height of an image using a pixel value

<html>

<head>

<style type="text/css">

img.normal

{
height: auto

img.big

height: 160px

img.small

height: 30px

</style>

</head>

<body>

<img class="normal" src="logocss.gif" width="95" height="84" />

<br />

<img class="big" src="logocss.gif" width="95" height="84" />

<br />

<img class="small" src="logocss.gif" width="95" height="84" />

</body>
</html>

Set the width of an element using a pixel value

<html>

<head>

<style type="text/css">

img

width: 200px

</style>

</head>

<body>

<img src="logocss.gif" width="95" height="84" />

</body>

</html>

Set the maximum height of an element

<html>

<head>

<style type="text/css">

p
{

max-height: 10px

</style>

</head>

<body>

<p>This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.</p>

<img src="logocss.gif" width="95" height="84" />

</body>

</html>
Set the maximum width of an element using a pixel value

<html>

<head>

<style type="text/css">

max-width: 600px

</style>
</head>

<body>

<p>This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.</p>

</body>

</html>

Classification

How to display an element as an inline element

<html>

<head>

<style type="text/css">

p {display: inline}

div {display: none}

</style>

</head>

<body>

<p>A display property with a value of "inline" results in</p>


<p>no distance between two elements.</p>

<div>And this div section is not displayed!</div>

</body>

</html>
How to display an element as a block element

<html>

<head>

<style type="text/css">

span

display: block

</style>

</head>

<body>

<span>A display property with a value of "block" results in</span>

<span>distance between two elements.</span>

</body>

</html>
A simple use of the float property

<html>
<head>

<style type="text/css">

img

float:right

</style>

</head>

<body>

<p>In the paragraph below, we have added an image with style <b>float:right</b>. The result is
that the image will float to the right in the paragraph.</p>

<p>

<img src="logocss.gif" width="95" height="84" />

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.


</p>

</body>

</html>

Let the first letter of a paragraph float to the left

<html>

<head>

<style type="text/css">

span

float:left;

width:0.7em;

font-size:400%;

font-family:algerian,courier;

line-height:80%;

</style>

</head>

<body>

<p>

<span>T</span>his is some text.

This is some text. This is some text.


This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

This is some text. This is some text. This is some text.

</p>

<p>

In the paragraph above, the first letter of the text is embedded in a span element.

The span element has a width that is 0.7 times the size of the current font.

The font-size of the span element is 400% (quite large) and the line-height is 80%.

The font of the letter in the span will be in "Algerian".

</p>

</body>

</html>

Cursor properties:

<html>

<body>

<p>Move the mouse over the words to see the cursor change:</p>

<span style="cursor:auto">
Auto</span><br />

<span style="cursor:crosshair">

Crosshair</span><br />

<span style="cursor:default">

Default</span><br />

<span style="cursor:pointer">

Pointer</span><br />

<span style="cursor:move">

Move</span><br />

<span style="cursor:e-resize">

e-resize</span><br />

<span style="cursor:ne-resize">

ne-resize</span><br />

<span style="cursor:nw-resize">

nw-resize</span><br />

<span style="cursor:n-resize">

n-resize</span><br />

<span style="cursor:se-resize">

se-resize</span><br />

<span style="cursor:sw-resize">

sw-resize</span><br />

<span style="cursor:s-resize">

s-resize</span><br />
<span style="cursor:w-resize">

w-resize</span><br />

<span style="cursor:text">

text</span><br />

<span style="cursor:wait">

wait</span><br />

<span style="cursor:help">

help</span>

</body>

</html>

Clear the sides of an element

<html>

<head>

<style type="text/css">

img

float:left;

clear:both;

</style>

<img src="logocss.gif" width="95" height="84" />


<img src="logocss.gif" width="95" height="84" />

</body>

</html>

Positioning

How to show overflow in an element using scroll

<html>

<head>

<style type="text/css">

div

background-color:#00FFFF;

width:150px;

height:150px;

overflow: scroll

</style>

</head>

<body>

<p>The overflow property decides what to do if the content inside an element exceeds the given
width and height properties.</p>

<div>
You can use the overflow property when you want to have better control of the layout. Try to
change the overflow property to: visible, hidden, auto, or inherit and see what happens. The
default value is visible.

</div>

</body>

</html>

How to hide overflow in an element

<html>

<head>

<style type="text/css">

div

background-color:#00FFFF;

width:150px;

height:150px;

overflow: hidden

</style>

</head>

<body>

<p>The overflow property decides what to do if the content inside an element exceeds the given
width and height properties.</p>
<div>

You can use the overflow property when you want to have better control of the layout. Try to
change the overflow property to: visible, scroll, auto, or inherit and see what happens. The
default value is visible.

</div>

</body>

</html>

How to show set the browser to automatically handle overflow

<html>

<head>

<style type="text/css">

img.top {vertical-align:text-top}

img.bottom {vertical-align:text-bottom}

</style>

</head>

<body>

<p>

This is an

<img class="top" border="0"

src="logocss.gif" width="95" height="84" />

image inside a paragraph.


</p>

<p>

This is an

<img class="bottom" border="0"

src="logocss.gif" width="95" height="84" />

image inside a paragraph.

</p>

</body>

</html>

Vertical alignment of an image

Place an element "behind" another element

<html>

<head>

<style type="text/css">

img.x

position:absolute;

left:0px;

top:0px;

z-index:-1
}

</style>

</head>

<body>

<h1>This is a Heading</h1>

<img class="x" src="bulbon.gif" width="100" height="180">

<p>Default z-index is 0. Z-index -1 has lower priority.</p>

</body>

</html>

Place an element "behind" another element 2

<html>

<head>

<style type="text/css">

img.x

position:absolute;

left:0px;

top:0px;

z-index:1

</style>
</head>

<body>

<h1>This is a Heading</h1>

<img class="x" src="bulbon.gif" width="100" height="180">

<p>Default z-index is 0. Z-index 1 has higher priority.</p>

</body>

</html>

Pseudo-classes

Add different colors to a hyperlink

<html>

<head>

<style type="text/css">

a.one:link {color: #ff0000}

a.one:visited {color: #0000ff}

a.one:hover {color: #ffcc00}

a.two:link {color: #ff0000}

a.two:visited {color: #0000ff}

a.two:hover {font-size: 150%}

a.three:link {color: #ff0000}


a.three:visited {color: #0000ff}

a.three:hover {background: #66ff66}

a.four:link {color: #ff0000}

a.four:visited {color: #0000ff}

a.four:hover {font-family: monospace}

a.five:link {color: #ff0000; text-decoration: none}

a.five:visited {color: #0000ff; text-decoration: none}

a.five:hover {text-decoration: underline}

</style>

</head>

<body>

<p>Mouse over the links to see them change layout.</p>

<p><b><a class="one" href="default.html" target="_blank">This link changes


color</a></b></p>

<p><b><a class="two" href="default.html" target="_blank">This link changes font-


size</a></b></p>

<p><b><a class="three" href="default.html" target="_blank">This link changes background-


color</a></b></p>

<p><b><a class="four" href="default.html" target="_blank">This link changes font-


family</a></b></p>

<p><b><a class="five" href="default.html" target="_blank">This link changes text-


decoration</a></b></p>
</body>

</html>

Add other styles to hyperlinks

<html>

<head>

<style type="text/css">

a:link {color: #FF0000}

a:visited {color: #00FF00}

a:hover {color: #FF00FF}

a:active {color: #0000FF}

</style>

</head>

<body>

<p><b><a href="default.asp" target="_blank">This is a link</a></b></p>

<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order
to be effective!!</p>

<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be
effective!!</p>
</body>

</html>

Вам также может понравиться