Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
<body>
<div class="p1">
<h2 class="q2">Class Times</h2>
<p class="r3"> Classes are timetabled every Friday from <span
class="q2 j4">9.15</span> to <span class="q2 j4">2.05</span> in
MG122</p>
</div>
</body>
</html>
Browser Display
.p1 {background-color:silver;}
.q2 {color:blue;}
.r3 {font-family:"Comic Sans MS", cursive;}
span.q2 {font-size:large;}
p.r3 {color:green;}
.q2.j4 {font-family:Verdana, Geneva, sans-serif;}
HTML
ID Attribute
The HTML attribute id is similar to the class attribute it can be
set on nearly any tag and can be used as a CSS selector but it is
much more restricted.
Only 1 tag on a page can have a given id;
It must be unique within the page and is used to identify just that
element.
<style type="text/css">
<!--
#bigf {font-size:36px;}
-->
</style>
Selectors
Descendant
One of the most useful ways to group
selectors together is to use a descendant
selector.
A descendant, in HTML and XML, is an
element that is completely contained within
another elements content.
Movie Review
<html>
<head> Example
<title>Babe: Best Movie EVER</title>
<style type="text/css"> /* add style rules here */ </style> </head>
<body>
<div class="header"> <h2> is a descendant of the <div>
<h1>Movie Review: <cite>Babe</cite></h1>
<p>A Mini-Review by Joe H. Moviefan</p>
</div>
<div class="opinion">
<h2>The Best Movie <em>EVER</em></h2>
<p>The movie <cite>Babe</cite> was the best family movie<cite>is a descendatn
ever produced! This of the <h1> and
great movie featured talking animals, a cantankerous old<cite>
man, andissubtle-yet-Oscar-
also descendant of the <div> (its
winning special effects -- who could ask for more? The clever writing and humorous
contained
touches make this all-ages movie great for children while within
still very enjoyable by the two)
adults. What a great movie!</p>
</div>
<div class="footer">
<p>What did you think? Mail me at
<a href="mailto:joe@example.com">Joe H. Moviefan.com!</a> All of them are also
</p>
</div> descendants of the
</body>
</html>
<body>
Selectors
Descendants
Descendant selectors define rules based on where a given
tag appears within the page by combining together simple
selectors, separated by spaces.
The order in which the tags are listed in the style rule is
important, the outside tag must be written first. Also note
that the <cite> inside <h1> is not styled by this rule, only
the <cite> inside the <p> element.
Selectors
Descendants
It is also important to keep in mind that a descendant selector
means any descendant, not just an immediate child. This enables
you to make rules that apply to any descendant element, no matter
how deeply its nested.
You can also combine section styles (set via class and <div>) with
element-based type selectors, as well; for example the following
code changes the font face and colours of <p> tags within the
header section, but leaves the rest of the header alone, as well as
the other paragraph tags that arent contained by something with
the .header class:
Example:
<a href=search.html class=nav>Search the site</a>
<a href=http://maps.google.com class=offsite>Google Maps</a>
CSS:
a:link.nav {color:cyan;}
a.offset:link {color:green;}
Example - CSS
#storytitle
{ font-family: Verdana; }
.storybody p
{ font-family: Arial; }
.storybody p:first-line
{background-color:silver;}
.storybody p:first-letter
{ font-size:xx-large; color:white;
background-color:black;
font-family:Verdana; }
Example
Browser
Fonts,
Colours and Effects
Fonts
CSS provides two ways to define colour. The first is to use a color name,
such as green or black; the second is to use a set of three RGB values,
corresponding to the amount of Red, Green and Blue desired.
Aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange,
purple, red, silver, teal, white and yellow.
Most browsers accept other colour names as well, such as pink, cyan and
violet. However, until a future version of CSS adds those colours to the
official specification, its best to avoid them as there is no guarantee that
a browser will support them.
Fonts,
Colours and Effects
To specify a colour in RGB notation, you need to know how much
red, green and blue is contained in that colour. All RGB colours are
measured based on a scale from 0 to 255. They are usually counted
in hexadecimal.
size size place the image at the top center same as top
specified location
percentage percentage place the image top right corresponds to 100% 0%
proportionally
top corresponds to 50% 0% left center same as left
A CSS display box has two widths: The content width and the box
width. The content width, is the width of the boxs content area; It
is the area where the boxs content exists, within the padding, the
border, and the margin; this is what is set by the width property.
The box width is the width of the entire box, including left and right
padding, the left and right border, and the left and right margin, as
well as the content width. Content and box height are determined
in the same way.
Width and Height
The CSS properties width and height are used to control the
size of the content width. To set the boxs width and
height, you need to use the padding, border and margin
values that add to the content width and height.
To stop subsequent text from flowing around a floating element, you can set
the clear property on the first element you dont want to float. This moves
that element down enough so that it doesnt wrap around the floating box.
Relative Positioning
CSS offers other ways to position boxes on the screen. A boxs location on
the screen can be set by using the position property.
The offset property values (top, bottom, left and right) accept both
positive and negative values, percentages, inherit and automatic values.
Positive values will offset the box towards the inside (centre point of the
content box) and negative values move the box away from the middle
(towards the outside).
<html>
Relative Positioning
<head>
<title>Three Boxes in a Row</title>
<style type="text/css">
.demobox {
border: 3px solid black; width: 8em;
font-family: Verdana, sans-serif;
background-color: white;
padding: 1em; margin: 0.5em; }
#mars { position: relative;
left: 5em; top: 2em; }
</style> with
</head>
<body>
<div class="demobox" id="earth">
Earth</div>
<div class="demobox" id="mars">
Mars</div>
<div class="demobox" id="jupiter">
Jupiter</div>
</body>
</html>
Absolute & Fixed
Positioning
Whenever an HTML element is included on a page, it generates a
display box; normally, these boxes are placed one after another or
within another box, based on the structure of the document and
whether the box is an inline or block box.
The Float (clear) values for the alignment property moves the box out
of the normal order to one side or the other, and subsequent content
flows around it.
Relative positioning (position property) preserves the normal content
flow by reserving the appropriate amount of space for the relatively
positioned element, and then moving it relative to that location.
Absolute & Fixed
Positioning
There are two other types of positioning techniques that
can be used: Absolute and fixed positioning. These methods
also use the position property and the 4 offset properties
(top, left, bottom and right).