Professional Documents
Culture Documents
L2 - CSS PDF
L2 - CSS PDF
Claudine Narciso
where:
selector = HTML element/tag to be defined
property = element’s attribute to be midified
value = new value assigned to the element’s attribute
‘background-images’ [background-images]
body {
background-color:#ffcc66;
background-image:url(“sunset.gif”);
}
To avoid repetition of background image the code could look like this:
body {
background-color:#ffcc66;
background-image:url(“sunset.gif”);
background-repeat:no-repeat;
}
The code example below positions the background image in the bottom right corner:
body {
background-color: #FFCC66;
background-image: url("butterfly.gif");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: right bottom;
}
Lesson 2: Fonts
Font family [font-family]
The property font-family is used to set a prioritized list of fonts to be used to display a given element or web
page.
h1 {font-family: arial, verdana, sans-serif;}
h2 {font-family: "Times New Roman", serif;}
Lesson 3: Text
Text indention [text-indent]
p{
text-indent: 30px;
}
td {
text-align: center;
}
p{
text-align: justify;
}
h2 {
text-decoration: overline;
}
h3 {
Web Prog & Dev 5
Claudine Narciso
text-decoration: line-through;
}
Letter space [letter-spacing]
The spacing between text characters can be specified using the property letter-spacing. The value of the
property is simply the desired width.
h1 {
letter-spacing: 6px;
}
p{
letter-spacing: 3px;
}
li {
text-transform: capitalize;
}
Lesson 4: Links
a:visited {
color: red;
}
This example gives active links a yellow background color:
a:active {
background-color: #FFFF00;
}
Pseudo-class: hover
a:hover {
color: orange;
font-style: italic;
}
Let's say that we have two lists of links of different grapes used for white wine and red wine. Then we want the
white wine links to be yellow, the red wine links to be red and the rest of the existing links on the webpage to
stay blue.
<p>Grapes for white wine:</p>
<ul>
<li><a href="ri.htm" class="whitewine">Riesling</a></li>
<li><a href="ch.htm" class="whitewine">Chardonnay</a></li>
<li><a href="pb.htm" class="whitewine">Pinot Blanc</a></li>
</ul>
a{
color: blue;
}
a.whitewine {
color: #FFBB00;
}
a.redwine {
color: #800000;
}
Let us say that the headline for chapter 1.2 must be in red. This can be done accordingly with CSS:
#c1-2 {
color: red;
}
span.benefit {
color:red;
}
And in our style sheet, we can utilize the grouping in the exact same way as above:
#democrats {
background:blue;
}
#republicans {
background:red;
}
<h1>Article 1:</h1>
<p>All human beings are born free
and equal in dignity and rights.
They are endowed with reason and conscience
and should act towards one another in a
spirit of brotherhood</p>
Web Prog & Dev 9
Claudine Narciso
h1 {
background: yellow;
}
By defining padding for the headlines, you change how much filling there will be around the text in each
headline:
h1 {
background: yellow;
padding: 20px 20px 20px 80px;
}
Lesson 9: Borders
Borders can be used for many things, for example as a decorative element or to underline a separation of two
things.
h1 {
border-width: thick;
border-style: dotted;
border-color: gold;
}
h2 {
border-width: 20px;
border-style: outset;
border-color: red;
}
It is also possible to state special properties for top-, bottom-, right- or left borders. The following example
shows you how:
h1 {
border-top-width: thick;
border-top-style: solid;
border-top-color: red;
border-bottom-width: thick;
border-bottom-style: solid;
border-bottom-color: blue;
border-right-width: thick;
border-right-style: solid;
border-right-color: green;
Web Prog & Dev 12
Claudine Narciso
border-left-width: thick;
border-left-style: solid;
border-left-color: orange;
}
<div id="picture">
<img src="bill.jpg" alt="Bill Gates">
</div>
To get the picture floating to the left and the text to surround it, you only have to define the width of the box
which surrounds the picture and thereafter set the property float to left:
#picture {
float:left;
width: 100px;
}
Another example: columns
Floats can also be used for columns in a document. To create the columns, you simply have to structure the
desired columns in the HTML-code with <div> as follows:
<div id="column1">
<p>Haec disserens qua de re agatur
et in quo causa consistat non videt...</p>
</div>
<div id="column2">
<p>causas naturales et antecedentes,
Web Prog & Dev 13
Claudine Narciso
idciro etiam nostrarum voluntatum...</p>
</div>
<div id="column3">
<p>nam nihil esset in nostra
potestate si res ita se haberet...</p>
</div>
Now the desired width of the columns is set to e.g. 33%, and then you simply float each column to the left by
defining the property float:
#column1 {
float:left;
width: 33%;
}
#column2 {
float:left;
width: 33%;
}
#column3 {
float:left;
width: 33%;
}
float can be set as either left, right or none
<div id="picture">
<img src="bill.jpg" alt="Bill Gates">
</div>
<h1>Bill Gates</h1>
To avoid the text from floating up next to the picture, we can add the following to our CSS:
#picture {
float:left;
width: 100px;
}
.floatstop {
clear:both;
}
Web Prog & Dev 14
Claudine Narciso
Lesson 11: Positioning of elements
With CSS positioning, you can place an element exactly where you want it on your page.
If we want this headline positioned 100px from the top of the document and 200px from the left of the
document, we could type the following in our CSS:
h1 {
position:absolute;
top: 100px;
left: 200px;
}
As you can see, positioning with CSS is a very precise technique to place elements. It is much easier than
trying to use tables, transparent images or anything else.
Absolute positioning
An element which is positioned absolute does not obtain any space in the document. This means that it does
not leave an empty space after being positioned.
To position an element absolutely, the position property is set as absolute. You can subsequently use the
properties left, right, top, and bottom to place the box.
As an example of absolute positioning, we choose to place 4 boxes in each corner of the document:
Web Prog & Dev 15
Claudine Narciso
#box1 {
position:absolute;
top: 50px;
left: 50px;
}
#box2 {
position:absolute;
top: 50px;
right: 50px;
}
#box3 {
position:absolute;
bottom: 50px;
right: 50px;
}
#box4 {
position:absolute;
bottom: 50px;
left: 50px;
}
Relative positioning
To position an element relatively, the property position is set as relative. The difference between absolute and
relative positioning is how the position is being calculated.
The position for an element which is relatively positioned is calculated from the original position in the
document. That means that you move the element to the right, to the left, up or down. This way, the element
still obtains a space in the document after it is positioned.
As an example of relative positioning, we can try to position three pictures relatively to their original position on
the page. Notice how the pictures leave empty spaces at their original positions in the document:
#dog1 {
position:relative;
left: 350px;
bottom: 150px;
}
#dog2 {
position:relative;
left: 150px;
bottom: 500px;
}
#dog3 {
position:relative;
left: 50px;
bottom: 700px;
Web Prog & Dev 16
Claudine Narciso
}
Lesson 12: Layer on layer with z-index (Layers)
For that purpose, you can assign each element a number (z-index). The system is that an element with a
higher number overlaps an element with a lower number.
Let us say we are playing poker and have a royal flush. Our hand can be presented in a way where each card
has got a z-index:
In this case, the numbers follow on another (1-5) but the same result can be obtained by using 5 different
numbers. The important thing is the chronological sequence of the numbers (the order).
The code in the card example could look like this:
#ten_of_diamonds {
position: absolute;
left: 100px;
top: 100px;
z-index: 1;
}
#jack_of_diamonds {
position: absolute;
left: 115px;
top: 115px;
z-index: 2;
}
#queen_of_diamonds {
position: absolute;
left: 130px;
top: 130px;
z-index: 3;
}
#king_of_diamonds {
position: absolute;
left: 145px;
top: 145px;
z-index: 4;
}
Web Prog & Dev 17
Claudine Narciso
#ace_of_diamonds {
position: absolute;
left: 160px;
top: 160px;
z-index: 5;
}