Overview Of CSS
CSS (Cascading Style Sheets) do simplify dramatically the creation of your pages at the price of a small initial learning. The sheet is a file that is included in each web page, which contains the attributes of position and style for the content. The CSS format exists since 1994 but has become more widespread in 2000 with a complete support in Internet Explorer 5 (I.E. is now replaced by Edge). It is possible to associate a stylesheet to an XML document. In this case the properties are attributed to the tags of the document.
- Why style sheets?
- The bases
- Selectors and properties
- Rules
- Inserting a stylesheet
- Layout
- A rule: cascading properties
Why style sheets?
The main interest
is to define the layout of the page by the creation of several columns of
text.
CSS has the following
advantages too:
- Defining a type of presentation once, reusing it in all pages.
- Separating text and presentation which in turn has several advantages:
- Ease reading the page by search engine robots, they ignore the style.
- Changing the style for the whole site in the stylesheet onlt.
- If necessary changing the style of a single page, with a clean display to print the text for example.
The bases
Stylesheets are mainly used:
- To position the
parts of the page.
The header, a menu to the left or right, the content and possibly a footer. - To define fonts and sizes of titles and subtitles and other texts.
- To position images.
Note that you can not put text in the stylesheet file, but it is better anyway they are in the page to be taken into consideration by search engines.
Selectors and properties
CSS definitions are made of two things: the selectors and their properties. Selectors are HTML tags or user-defined classes.
To divide and position parts of the page, a tag called "div" is used.
The general format
of a style rule is as follows:
name { ...list of properties... }
The name may have
three forms, as detailed below.
The properties have
the following form:
attribute : value ;
Some attributes as "border" for example, may include a list of values separated by a space (not a comma).
Rules
A style sheet is made of rules. There are three types of rules in a CSS file:
- The HTML tags
whose style is redefined.
Example:
table { } - Elements with
an unique identifier:
#menu { } - Reusable classes
.item { }
Identifiers and class require an attribute in the HTML code. This will have the following form with the div tag for example:
- <div
id="menu">
For a unique identifier in the page (you can reuse it in different pages). - <div
class="item">
For a name reused several times in a same page.
Inserting a stylesheet
To insert a stylesheet into an HTML page, the following line is added into the <head> section:
<link type="text/css" href="mysheet.css" rel="stylesheet">
- The type
designates the type of display.
- href set
to file containing definitions of style.
- rel="stylesheet"
specifies the type of link.
Layout
The parts of the page are cut with a div tag at which is assigned an identifier. Examples:
<div id="logo"> </div> <div id="menu"> </div> <div id="content"> </div>
These elements are reflected in the style sheet in the following format:
#logo { ... } #menu { top: 60px; } #content { left: 120px; }
Then attributes of
positions have to be defined.
For the menu top:
60px;
- to be placed 60 pixels under the logo.
For the content: left:
120px;
- to spare 120 pixel left to the menu.
A rule: properties in cascades
Never forget that
CSS are hierarchical: all the properties you set are related to the element
where they are defined.
For example, a police decreased to 80% within one element in which the police
is increased to 120% returns to the standard font size.
Indeed, it is possible to target precisely an element within another element with the following syntax:
#item inner-item {
...properties...
}
Example:
#table a { color:green;
}
The links will be
green, but only in tables.
To get CSS properties
in details, see the references below.
Resources
Some tutorials to learn how to use CSS with examples, and tools.
- CSS Tutorial. Provides a complete example of CSS page: menu, header, footer, logo, etc. ... This example is a model that you can use to create your pages with permission of the author. Do not forget to download the stylesheet, and the logo (links are on the menu).
- CSS validation service. Check your style sheet for errors.
See also