Content of the lesson:
CSS (Cascading Style Sheets) is a technology for defining the appearance of a webpage. It was created in 1996 when developers needed to separate the appearance and the content (structure). As we know, HTML is a technology for marking up any part of the document or for defining its structure. The HTML tags have simple meanings:
The structure of a document is described using these tags. CSS has another task, it should describe the problem of appearance:
The file with CSS rules is being usually called a stylesheet. It is a simple text file which contains one or more rules which affects (using their properties and values) how any element of the page should be displayed (font type, font size, layout, position, print recommendations). Using CSS you can also define dynamic properties (appearing and disappearing of objects, drop-down lists, menus).
Nowadays there are different versions of CSS but the last relatively well supported one is CSS 2.1.
Styles are created away from a webpage (they can be written into it but they create an isolated group of rules) because they can be applied to many documents at once without the need to change every of it (for example an e-shop). If you set a rule to change a link color, it will be applied in every document which uses this stylesheet. This saves much time, the amount of transferred data and prevents you from making a mistake. You should create a proper structure of (X)HTML code to be able to apply a stylesheet.
Why can you save the amount of transferred data using a stylesheet? Imagine the following situation. You have a large website which contains many tables and you want every table to have a yellow background and the width of 100 %. You can insert this rule inside the html file using the <table bgcolor="yellow" width="100%"> command. You can also create a CSS rule table{background-color:yellow; width:100%;}. Imagine having 200 tables in your page - you would have to write this 200 times instead of once. Another advantage is that you will be able to change the appearance of all tables at once which is very effective.
Before starting to explain the CSS topic, we take a look at this website: http://www.csszengarden.com/. The CSS potential illustrated here is really huge (CSS styles can DRAMATICALLY change the whole web appearance - all you have to do is to click on the "select a design" item and all changes are done by changing one line of code - the link to a CSS file):
You should realize that the whole HTML code without using CSS styles looks like the following document:
You can view a plain HTML site using the View > Page Style > No Style option in Firefox.
Every CSS rule can be divided to two parts:
h1{color:black;}
h1{
color:black;
}
h1{
color:black;
background:yellow;
}
h1{
color:black;
background:yellow;
font-size:20px;
}
You can view the rules used by this page in this file: ivtstyly.css.
Spaces, empty lines and tabs are ignored in a stylesheedt Every declaration should be divided by a semicolon (you do not have to add a semicolon after the last rule but in case you add another rule and forget to separate it, it will not be applied correctly). Every mistake (spelling, wrong value, ...) is ignored by the browser but it is difficult for you to find it.
You can use comments as well as in HTML, they are very useful to write your notes about the meaning of CSS rules. A comment should be written inside the /* */ marks.
/* Následující pravidlo říká, že pokud najedu myší na odkaz, pak se změní jeho barva a podtrhne se */
a:hover {
color:#6eb300;
text-decoration:underline;
}
Pay attention when writing comments because any user can view this file and read all your comments. Comments also enlarge the size of the CSS file.
The last thing you should know is where to define the stylesheet. You can use one of the following three possibilities:
<link rel="stylesheet" href="css/název souboru se styly.css" type="text/css" />
<link rel="stylesheet" href="název souboru se styly.css" type="text/css" />
<style type="text/css">div#banner{background-image:url(images/design/banner/34.jpg);}</style>
<p style="text-align:right;"><a href="index.php"> Text v odstavci </a></p>
Try to find the CSS 2.1 specification or a later one.