Академический Документы
Профессиональный Документы
Культура Документы
CSS Syntax
The CSS syntax is made up of three parts: a selector, a property and a value:
selector {
property: value
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:
Note: If the value is multiple words, put quotes around the value:
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
}
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 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>
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>
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.
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":
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":
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;
}
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
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.
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:
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.
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.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
<head>
<style type="text/css">
h1 {background-color: #00ff00}
h2 {background-color: transparent}
p {background-color: rgb(250,0,255)}
</style>
</head>
<body>
<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>
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.
text-shadow none
color
length
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.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
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.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
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.
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
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!
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
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.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
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.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
marker-offset auto 1 7 2
length
The CSS table properties allow you to set the layout of a table.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
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.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
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.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined
(CSS1 or CSS2).
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.
pseudo-classes:
Syntax
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:
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!!
Pseudo-elements Syntax
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>
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.
• 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>
Note: The "first-letter" pseudo-element can only be used with block-level elements.
• 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
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
p:first-letter {color:#ff0000;font-size:xx-large}
p:first-line {color:#0000ff}
<p>The first words of an article...</p>
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.
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)
}
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
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
Outline
Draw a line around an element, outside the border edge (outline) (Does not work in IE)
<html>
<head>
<style type="text/css">
</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">
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style type="text/css">
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
outline-style: solid;
outline-width: thick
p.two
outline-style: solid;
outline-width: 2px
</style>
</head>
<body>
</body>
</html>
Margin
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style type="text/css">
p.topmargin
margin-top: 25%
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table border="1">
<tr>
<td class="test1">
</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>
</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
<html>
<head>
<style type="text/css">
table.one
table-layout: automatic
table.two
table-layout: fixed
</style>
</head>
<body>
<tr>
<td width="20%">1000000000000000000000000000</td>
<td width="40%">10000000</td>
<td width="40%">100</td>
</tr>
</table>
<br />
<tr>
<td width="20%">1000000000000000000000000000</td>
<td width="40%">10000000</td>
<td width="40%">100</td>
</tr>
</table>
</body>
</html>
<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>
<html>
<head>
<style type="text/css">
table.coll
border-collapse: collapse
table.sep
border-collapse: separate
</style>
</head>
<body>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
<br />
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
<html>
<head>
<style type="text/css">
table.one
border-collapse: separate;
border-spacing: 10px
}
table.two
border-collapse: separate;
</style>
</head>
<body>
<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>
<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
<html>
<head>
<style type="text/css">
img.normal
{
height: auto
img.big
height: 160px
img.small
height: 30px
</style>
</head>
<body>
<br />
<br />
</body>
</html>
<html>
<head>
<style type="text/css">
img
width: 200px
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style type="text/css">
p
{
max-height: 10px
</style>
</head>
<body>
</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>
</body>
</html>
Classification
<html>
<head>
<style type="text/css">
p {display: inline}
</style>
</head>
<body>
</body>
</html>
How to display an element as a block element
<html>
<head>
<style type="text/css">
span
display: block
</style>
</head>
<body>
</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>
</body>
</html>
<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>
</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%.
</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>
<html>
<head>
<style type="text/css">
img
float:left;
clear:both;
</style>
</body>
</html>
Positioning
<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>
<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>
<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
<p>
This is an
</p>
</body>
</html>
<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>
</body>
</html>
<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>
</body>
</html>
Pseudo-classes
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
</html>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<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>