Cascading Style Sheet
Cascading Style Sheet
<html>
<head>
<title> Example </title>
</head>
<body style="background-color:#FF0000;">
<p> This is a red page </p>
</body>
</html>
<html>
<head>
<title> Example </title>
<style type ="text/css">
body {background-color: red;}
</style>
</head>
<body>
<p> This is a red page </p>
</body>
</html>
<html>
<head>
<title> My documents </title>
<link rel="stylesheet" type="text/css" href="style/main.css"/>
</head>
<body>
</html>
h1 {
color: #ff0000;
}
body {
background-color: #FFCC^^;
background-image: url("sunset.gif");
}
Notice the location of the image as url("sunset.gif"). This means that the image is located in
the same folder as the style sheet. We can also refer to imager in other folders using
url("../images/sunset.gif") or even on the Internet indicating the full address of the file:
url("http://www.html.net/sunset.gif").
Value Description
background-repeat: repeat-x The image is repeated horizontally
background-repeat: repeat-y The image is repeated vertically
background-repeat: repeat-y The image is repeated both horizontally and vertically
background-repeat: no The image is not repeated
repeat
to avoid repetition of a background image the code should look like this:
body {
background-color: #FFCC66;
background-image: url;("sunset.gif");
background-repeat: no-repeat;
}
Value Description
background-attachment: The image scrolls with the page - unlocked
scroll
background-attachment: The image is locked
fixed
body {
background-color: #FFCC^^;
background-image: url("sunset.gif");
background-repeat: no-repeat;
background-attachment: fixed;
}
There are numerous ways to set the values of background-position. However, all of them are
formatted as a set of coordinates.
Value Description
background-position: 2cm The image is positioned 2cm from the left and 2cm down
2cm the page
background-position: 50% The image is centrally positioned and one fourth down the
25% page
background-position: top The image is positioned in the top-right corner of the page
right
The code example below positions the background image in the bottom right corner:
body {
background-color: #FFCC^^;
background-image: url("sunset.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;}
The property font-style defines the chosen font either in normal, italic or obllique
h1{font-family: arial,verdana,sans-serif;}
h2{font-family: "Times New Roman", serif; font-style: Italic;}
Font Variant [font-variant]
The property font-variant is used to choose between normal or small-caps variants of a font. A
small-caps font is a font that uses smaller sized capitalized letters (upper case) instead of
lower case letters.
h1{font-variant: small-caps;}
h2{font-variant: normal;}
The property font-weight describes how bold or "heavy" a font should be represented. A font can either
be normal or bold.
h1 {font-size: 30px;}
h2 {font-size: 12pt;}
h3 {font-size: 120%;}
p {font-size: 1em;}
Lesson 3: Text
Text Indention [text-indent]
p {
text-indent: 30px;
}
Text alignment[text-align]
th {
text-align: right;
}
td {
text-align: center;
}
p {
text-align: justify;
}
h1{
text-decoration: underline;
}
h2{
text-decoration: overline;
}
h3{
text-decoration: line-through;
}
h1{
letter-spacing: 6px;
}
p{
letter-spacing: 3px;
}
You can choose to capitalize, uppercase or lowercase regardless of how the original text is
looks in the HTML
h1{
text-transform: uppercase;
}
li {
text-transform: capitalize;
}
Lesson 4: Links
a: visited {
color: red;
}
a: active {
background-color: #FFFF00;
}
Pseudo-class: hover
a: hover {
color: orange;
font-style: italic;
}
a: hover {
letter-spacing: 10px;
font-weight: bold;
color: red;
}
a: hover {
text-transform: uppercase;
font-weight: bold;
color: blue;
background-color: yellow;
}
a{
text-decoration: none;
}
We can hereafter define special properties for links belonging to pasta and drinks, respectively.
a{
color: blue;
}
a.pasta {
color: yellow;
}
a.drinks {
color: red;
}
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;
}
Lesson 6: Grouping of Elements (span and div)
Grouping with <span>
The element <span> is what we could call a neutral element which does not add anything to
the document itself. But with CSS, <span> can be used to add visual features to specific parts of the
text in your documents.
span.benefit {
color: red;
}
<div id="students">
<ul>
<li> 1st year </li>
<li> 2nd year </li>
<li> 3rd year </li>
<li> 4th year </li>
</ul>
</div>
<div id="courses">
<ul>
<li> IT </li>
<li> Nursing </li>
<li> Engineerinf </li>
<li> HRM </li>
</ul>
</div>
We can utilize the grouping in the exact same way as above:
#students {
background: blue;
}
#courses {
background: red;
}
<body> {
margin-top: 100 px;
margin-right: 50 px;
margin-bottom: 10 px;
margin-left: 70 px;
}
body {
margin : 100px 40px 10px 70px
}
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;
}
h1 {
` border-top-width: thick;
border-top-style: solid;
border-top-color: red;
border-right-width: thick;
border-right-style: solid;
border-right-color: green;
border-left-width: thick;
border-left-style: solid;
border-left-color: orange;
}
Lesson 10:Align
Center Aligning Using the margin Property
Block elements can be aligned by setting the left and right margins to "auto"
Setting the left and right margins to auto specifies that they should split the available margin
equally. The result is a centered element
.center
{
margin-left: auto;
margin-right: auto;
width: 70%
background-color: #b0e0e6;
}
In IE (filter:alpha(opacity=x)) x can be a value from 0-100. A lower value makes the element
more transparent.
<html>
<head>
<style type="text/css">
div.background
{
width:500px;
height:250px;
background:url(webdev.jpg) repeat;
border: 2px solid black;
}
div.transbox
{
width: 400px;
height: 180px;
margin: 30px 50px;
background-color: #fffffff;
border: 1px solid black;
}
div.transbox p
{
margin: 30px 40px;
font-weight:bold;
color: #0000000;
}
</style>
</head>
<body>
<div class="background">
<div calss="transbox">
<p>
HelloHello HelloHello HelloHello HelloHello HelloHello HelloHello
HelloHello HelloHello HelloHello HelloHello HelloHello HelloHello
HelloHello HelloHello HelloHello HelloHello HelloHello HelloHello
HelloHello HelloHello HelloHello HelloHello HelloHello HelloHello
</p>
</div>
</div>
</body>
</html>
We create a div element (class="background") with a fixed height and width, a background
image, and a border. Then we create a smaller div(calss="transbox") inside the first div
element. The "transbox" div have a fixed width, a background color and a border and it is
transparent. Inside the transparent div, we add some text inside a p element.
To minimize the code, you can group selectors. Separate each selector with a comma.
h1, h2, p
{
color:green;
}
It is possible to apply a style for a selector within a selector. In the example below, one style is
specified for all p elements. one style is specified for all elements with class="marked", and a
third style is specified only for elements with class="marked":
p
{
color:clue;
text-align:center
}
.marked
{
background-color:red;
}
.markedp
{
color: white;
}
With the width- property, you can define a certain width of an element.
div.box {
width: 200px;
border: 1px solid black;
background: orange;
}
</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>
<head>
<style type= " text/css">
#caption
{
line-height: 25px;
}
div {
background: red;
max-width: 25%;
}
</style>
</head>
<body>
<div>
<p id="caption">
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
</p>
<div>
</div>
</body>
</html>
Hiding an element can be done by setting the display property to "none" or the visibility to
"hidden"
However, these two methods produce different results:
visibility:hidden hides an element, but it will still take up the same space as before. The
element will be hidden, but still affect the layout.
h1.hidden (visibility:hidden;}
display:none hides an element and it will not take up any space. The element will be hidden,
and the page will be displayed as the element is not there:
<html>
<head>
<style type="text/css">
h1.hidden {display: none;}
</style>
</head>
<body>
<h1>This is a visible heading</h1>
<h1 class="hidden"> This is a hidden heading </h1>
<p> Notice that the hidden heading does not take up space </p>
</body>
</html>
If we would like to have a text wrapping around a picture, the result would be
like this:
<div id="picture">
<img src=" Hydrangeas.jpg" alt="flower">
</div>
<p> causas naturales et antecedentes, idciro etiam nostrarum voluntatum... </p>
To get the picture floating to the left and the text surround it, you only have to define the width
of the box which surrounds the picture thereafter set the property to left:
#picture {
Float:left;
width: 100px; }
<div id="column1>
<p> Haec disserens qua de agatur et in quo causa consistat non videt... </p>
</div>
<div id="column2>
<p> causas naturales et antecedentesm idciro etiam nostrarum voluntatum... </p>
</div>
<div id="column3">
<p> nam nihil esset in nostra potestate si res ita sehaberet... </p>
</div>
Now the desired width of the columns is set to e,g, 22% and then you simpy 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%
}
The cleat property is used to control how the subsequent elements of floated elements in a document
shall behave.
<div id="picture">
<img src="bill.jpg" alt="bill gates">
</div>
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;
}
Absolute positioning
An element which is positioned absolute does not obtain any space in the document.
They 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 properties left, right, top and bottom to the box