CSS - The styles of HTML
Translation of the original in Spanish. Read it in: CatalàCastellano
After a few days struggling with the HTML format of the web I decided to build a CSS structure that gives the whole portal its formatting. In order to be able to do this I found a mini-manual that I suppose will be enough to create these .css files.
Cascading style sheets.
Introduction
Style sheets are a great step forward in Web page design, since they allow better control of appearance.
Until now it was necessary to resort to various tricks to make up for the limitations of HTML and to improve the presentation of the pages. These tricks, commonly used, have had side effects, since they usually work only for some browsers or platforms, they increase the complexity of the page, they offer little flexibility and they tend to increase the weight of the page (more Kbs).
Some of these tricks are:
- Use of non-standard tags.
- Turning text into an image.
- Using transparent images to create blank spaces.
- Using tables to force certain presentations.
- Using programs or languages outside HTML to achieve certain effects. (JavaScript, JAVA, CGIs, etc.)
Cascading style sheets give us back control of presentation. For example, they allow us to determine margins, line spacing, font type and size, text and background colours, as well as the redefinition of any tag at will.
There is also another very important aspect of style sheets and that is that they separate completely the information for the presentation of a page from its content, which makes the design and revision of the pages much easier, because the presentation of a page, or of all of them together, can be varied without changing a single line of the HTML code.
As we will see later, design variations can be defined by means of "classes". For example, if you need five different paragraph styles, you can define five sub-tags of <P>: P.normal, P.indentado, P.subrayado, P.rojo and P.dobleespacio.
NOTE: Cascading style sheets are only supported by Explorer 3.0 and Netscape 4.0 onwards. Other browsers, or earlier versions, will simply ignore them.
How do they work?
Cascading style sheets allow us to
redefine
the rules that the browser uses to present a Web page.
All browsers have their rules. Thus, for example, H1 is defined as text aligned to the left, a couple of points bigger than the rest and in bold.
Each tag defines its own rules for displaying the text it encloses. With style sheets we can change each one of these rules if we wish. That is, we can modify at will the behaviour of each tag.
There are three ways of adding styles to our Web pages.
- Adding style instructions only to specific tags or to a group of them.
- Including the style instructions in the HTML document of a specific page, which allows changing the appearance of a whole sheet.
- Linking several pages with a style file, so that the appearance of multiple pages can be changed by touching that .CSS file.
We can use in our pages one, two or all three of the methods described, as will be seen later.
1. Style for specific tags or groups of tags
This method is the appropriate one if you only want to change the style in certain sections of a page. With it we can manipulate the properties of some tag or of some group of tags.
It is done by adding the STYLE parameter, which contains a series of properties, inside the corresponding tag. Let us look at the following example: <P>
"This is a normal paragraph, with no margins on the left or on the right, with the colour of its text in black".
<P STYLE="margin-left: 0.5in; margin-right: 0.5in; color:green">
"I want this other paragraph to have a left margin of half an inch, just like the right margin. I also want its text to be green".
Which will look (in browsers that support style sheets) like this:
"This is a normal paragraph, with no margins on the left or on the right, with the colour of its text in black".
"I want this other paragraph to have a left margin of half an inch, just like the right margin. I also want its text to be green".
As you can see in this example, we have varied the way the tag <P> usually behaves. We could have achieved many other things, as will be seen later.
Note the syntax of the STYLE parameter. A series of properties are placed in quotes (for example, margin-left), and after a colon comes the value of that property (in this case, 0.5in, that is, 0.5 inches). The different properties must be separated from one another by a semicolon.
If we want to change the appearance of an entire section (that groups a set of tags), we can use the <DIV> tag, with which we define the style globally for that section.
In the following example we change the colour (to red) and the size of the font (to 16 points) of a block of tags by means of the <DIV> tag, which has the same effect as if we had assigned this style separately to each of the enclosed tags (<P>, <UL> and <LI>):
<DIV STYLE="font-size: 16pt; color: red">
<P>The style definition (red and 16 points) affects the whole block of tags.
<P>This text is also red and is 16 points high.
<P>Just like this line.
</DIV>
Which looks as follows:
The style definition (red and 16 points) affects the whole block of tags.
This text is also red and is 16 points high.
Just like this line.
What happens if we introduce a different style into one of the tags enclosed by DIV? Let us check it by introducing a style of its own into the second <P> tag:
<DIV STYLE="font-size: 16pt; color: red">
<P>
Let us see whether the style definition (red and 16 points) affects the whole block of tags, up to the DIV closing tag.
<P STYLE="color: blue">This line is blue, in spite of being enclosed within DIV, because it has its own style.
<P>This one complies with the DIV style, because it has no style of its own.
</DIV>
This is the result:
Let us see whether the style definition (red and 16 points) affects the whole block of tags.
This line is blue, in spite of being enclosed within DIV, because it has its own style.
This one complies with the DIV style, because it has no style of its own.
As can be verified in this case, the style inside a specific tag has preference over the style of the block, imposed by DIV.
The preference of some methods over others is due to the order in which they are activated by the browser; for this reason they are called Cascading style sheets, since there is a cascade of methods for applying styles, with a strict order of preference of some over the others.
This method of introducing the style into specific tags or into sets of tags is simple to apply and is the right one if you only want to make some specific change on a page. However, it is not the real power of style sheets.
2. Global inclusion of the style in an HTML document
It is done by putting a block of instructions inside the <STYLE> and </STYLE> tags, which must be inside the document header, after the title, between the </TITLE> and </HEAD> tags.
This <STYLE> tag has a parameter, TYPE, which specifies the type of medium in which it is going to be published on the Internet; in our case it will be "text/css", which will allow browsers that do not support styles to ignore them. Therefore, the structure will be the following:
<HTML>
<HEAD>
<TITLE> </TITLE>
**<STYLE TYPE="text/css">
[style instruction block]
</STYLE>**
</HEAD>
<BODY>
[set of all the tags that make up the page]
</BODY>
</HTML>
Let us now look, with an example, at how the block of style instructions is written, included inside the <STYLE> tag (in the following chapter each of the attributes is seen in detail):
<STYLE TYPE="text/css">
BODY {background: yellow ;font-size: 10pt; font-family: Arial;
margin-left: 0.5in; margin-right: 0.5in}
H1 {background: blue; font-size: 14pt;font-weight: bold; color: red}
H2 {font-size: 12pt;font-weight: bold; color: red}
DIV {background: URL(nubes.jpg)}
</STYLE>
As can be seen in this example, the different tags to which a style is to be attributed are placed there (in this case BODY, H1, H2 and DIV, but they could have been any others); after the name of each tag, and enclosed by the signs "{" and "}", go the different properties with their respective values, separated from one another by a semicolon.
If this block is placed in the header of the HTML document, where it has been indicated before, we will see that every time one of the tags that have a style definition is used, it will not behave in the usual way, but as we have defined it. The rest of the tags, which are not included in the style definition block, will behave in the usual way.
3. Linking different documents to a style sheet
If our Web site is made up of many pages, and we want to give a uniform style to a few of them, instead of including a block of style definition repeated in each one of them (as has been seen in the previous section), we can establish a link to a text file that contains the style definition block.
Let us suppose that we want to apply to a few pages the style seen in the previous example. We would have to create a text file like this:
| BODY {background: yellow ;font-size: 10pt; font-family: Arial; margin-left:
0.5in; margin-right: 0.5in} H1 {background: blue; font-size: 14pt;font-weight: bold; color: red} H2 {font-size: 12pt;font-weight: bold; color: red} DIV {background: URL(nubes.jpg)} |
Note that it has no tags at all, since it is not a normal HTML document, but a text file that only contains the style definition block.
It is saved with whatever name you like, but it must necessarily have the .css extension. Let us suppose that we save it with the name hoja.css
In all the pages that we want to have this specific style, we will only have to add (in the same place in the header as for the previous case, that is, between </TITLE> and </HEAD>, the following tag:
<LINK REL=stylesheet HREF=hoja.css TYPE="text/css">
Here it has been assumed that the hoja.css file has been left in the same directory where the HTML documents are. If this is not the case, that is, if it is in another subdirectory on the server, the path would have to be indicated (for example: HREF=ruta/hoja.css).
An example of this method is this manual, in which all the chapters are linked to a file that contains the style sheet for all of them, called hoja.css.
Mixing the methods
As has been seen above, there are three methods of applying cascading style sheets.
Can more than one be applied at the same time on the same page?
Yes. For example, in the making of this manual some of them are used simultaneously. On the index and cover pages the second method is used (inclusion of the style sheet in the HTML document) and also the first (inclusion of the style in specific tags). And in all of the chapters the third one (link to the style sheet) and the first one are used.
What happens if there is contradictory information between them?
To avoid conflicts between the different methods used simultaneously, there is an order of preference, that is to say, which one will prevail over the other if they give contradictory orders about a specific aspect.
The order of preference is the following (from highest to lowest):
- Style inside a tag.
- Style block in the document header.
- Link to a file that contains the .CSS style sheet
This means that a style order, for example the colour of the text, placed inside a tag, will prevail over the one indicated in the other two methods, if they are used on that page.
The most practical thing is to use the third method (link to a style sheet), to give a consistent appearance to all the pages, and if it is necessary to modify a specific aspect in one of them, use the first or the second method.
Cascading style sheets.
Attributes
Below is a table with the summary of the parameters that can be included in style sheets. Clicking on the link with the name of the attribute takes us to the section where it is discussed in more detail.
| Attribute | Description | Values | Example |
| Font-Size | Sets the text size. | Points (pt) Pixels (px) centimetres (cm) inches (in) |
{font-size: 12pt} {font-size: 24px} {font-size: 5cm} {font-size: 1in} |
| Font-Family | Sets the type of font. | Name of the font or font family | {font-family: "Comic Sans MS",Sans-Serif} |
| Font-Weight | Sets the weight of the font, but they depend on it. | Extra-Light Light Demi-Light Medium Demi-Bold Bold Extra-Bold |
{font-weight: bold} |
| Font-Style | Sets the text as italic letters. | Normal Italic Minusculas oblique |
{font-style: italic} |
| Line-Height | Sets the distance between lines. | points (pt) pixels (px) centimetres (cm) inches (in) percentage (%) |
{Line-Height: 24pt} |
| Color | Sets the colour of the text. | Name of the colour RGB values |
{color: blue} |
| Text-Decoration | Underlines or highlights the text. | None (none) Underline (underlined) Italic (italics) Line-Through (struck through) |
{text-decoration: underline} |
| Margin-left | Sets the left margin of the page | Points (pt) pixels (px) centimetres (cm) inches (in) |
{margin-left: 3cm} |
| Margin-right | Sets the right margin of the page. | Points (pt) pixels (px) centimetres (cm) inches (in) |
{margin-right: 3cm} |
| Margin-top | Sets the top margin of the page. | Points (pt) pixels (px) centimetres (cm) inches (in) |
{margin-top: -20px} |
| Text-align | Sets the alignment of the text. | Left Center Right |
{text-align: right} |
| Text-Indent | Sets the indentation of the text. | Points (pt) pixels (px) centimetres (cm) inches (in) |
{text-indent: 1.5cm} |
| Background | Sets the image or the colour of the background. | URL, name of the colour RGB value |
{background:#33CC00} {background: red} {background: URL(isla.gif)} |
Cascading style sheets.
Usage tips
This chapter expands on the concepts seen in the two previous chapters, and introduces some new ones about cascading style sheets.
Grouping different attributes
Let us suppose that we want to attribute the same attributes to different tags, as for example:
H1 {font-size: 15pt;
font-weight: bold;
color: maroon}
H2 {font-size: 15pt;
font-weight: bold;
color: maroon}
H3 {font-size: 15pt;
font-weight: bold;
color: maroon}
They can be grouped in this way:
H1, H2, H3 {font-size: 15pt;
font-weight: bold; color: maroon}
Grouping the text attributes
In the previous chapter a series of attributes related to the appearance of the text has been seen. They can be simplified by grouping them in a specific way. Thus, for example, instead of:
P {font-weight: bold; font-style: italic; font-size: 12pt;
line-height: 20pt; font-family: Times, serif; }
They can be grouped into a single attribute called font:
P {font: bold italic 12pt/20pt Times, serif}
Note: The order of the attributes is significant. The Font-Weight and Font-Style attributes must be specified before the others.
Grouping the margin attributes
The three different attributes for the margins (top, right and left) can also be grouped into a single attribute called margin.
Thus, for example, instead of:
BODY {margin-top: 20px; margin-right: -10px; margin-left: -10px}
You can put:
BODY {margin: 20px -10px-10px}
The order of placement is significant. It must be: top, right and left. If a single value is given, it will be applied to all three margins.
Variations by means of classes
In chapter 23 we saw that one of the methods was the global inclusion of the style, in which the styles of a block of different tags were defined. We saw there this example:
<STYLE TYPE="text/css"> BODY {background: yellow ; font-size: 10pt;
font-family: Arial; margin-left: 0.5in; margin-right: 0.5in} H1
{background: blue; font-size: 14pt; font-weight: bold; color: red}
H2 {font-size: 12pt; font-weight: bold; color: red} DIV {background:
URL(nubes.jpg)} </STYLE>
As can be seen, it is defined for the H2 tag, for example, that its text be red. But this makes, obligatorily, all H2 level headers be of this colour on the whole page.
But we might be interested, for whatever reason, that sometimes it has the red colour and other times other colours. To achieve this, some classes can be used (variants of this tag). To do so, the H2 tag is defined separately, followed by a dot and whatever name we want, as for example:
H2.rojo {font-size: 12pt; font-weight: bold; color:
red} H2.verde {font-size: 12pt; font-weight: bold; color: green} H2.
azul {font-size: 12pt; font-weight: bold; color: blue}
and on the page, we will be able to use, as it suits us, one or the other of these variants in the following way:
<H2 CLASS=rojo>Esta cabecera sera de color rojo</H2>
<H2 CLASS=verde>Esta cabecera sera de color verde</H2>
<H2 CLASS=azul>Esta cabecera sera de color
azul</H2>
As can be seen, this gives us an even greater flexibility to obtain the appearance we want on our pages.
Applying style to links
Style sheets also allow modifying at will the appearance of links, assigning any of the attributes seen (text colour, font size, whether the underline exists or not, etc.).
There are two types of links that can be modified:
A:link links that have not yet been visited
(clicked)
A:visited links that have already been visited (clicked)
For example:
A:link {color: red} A:visited {color: green}
makes unvisited links red, and once visited they become green.
If the text-decoration attribute seen in the previous chapter is applied to them, set to the none parameter, it makes the links not underlined. Example:
A:visited {color: fuchsia; text-decoration: none}
makes visited links fuchsia and not underlined.
Comments
Own comments can be added to style sheets, which can serve as a later reminder. They can be placed anywhere in the specification, as long as they are enclosed between the characters /* and */. Example:
H1 {font: 20pt/22pt bold; color=#00FF00} /*Color verde para las
cabeceras de nivel 1 */
Designing for all browsers
If the method of including a style block in the header is used, which consists of putting the different style instructions between the <STYLE> and </STYLE> tags, as in the example that will be displayed:
<STYLE TYPE="text/css"> BODY
{background: yellow ; font-size: 10pt; font-family: Arial;
margin-left: 2cm; margin-right: 2cm} H1 {background: blue;
font-size: 14pt; font-weight: bold; color: red} H2 {font-size: 12pt;
font-weight: bold; color: red} DIV {background: URL(isla.gif)}
</STYLE>
in browsers that do not implement style sheets the <STYLE> and </STYLE> tags will be ignored, but it may happen that the style definition block appears as text.
To avoid this, it is advisable to enclose that block of information between the symbols <!-- and -->. Therefore, it is advisable to put the style definition block in this way:
<STYLE TYPE="text/css"> <!-- BODY {background: yellow
; font-size: 10pt; font-family: Arial; margin-left: 2cm;
margin-right: 2cm} H1 {background: blue; font-size: 14pt;
font-weight: bold; color: red} H2 {font-size: 12pt; font-weight:
bold; color: red} DIV {background: URL(nubes.jpg)} -->
</STYLE>
Taking advantage of inheritance between tags
As we know, the tags of an HTML document have a defined structure, which in a very summarised way can be put like this:
<HTML> <BODY> (conjunto de
comandos que conforman la pagina) </BODY> </HTML>
As can be seen, the <BODY> tag encloses all the others. If a specific style is assigned to this tag, all the elements that are inside the page (tables, lists, paragraphs, etc.) will inherit this style.
Therefore, to establish a global style for the whole page, the most appropriate thing is to attribute it to the <BODY> tag. For example:
BODY {font: 10pt/11pt
Arial, Helvetica, sans-serif; background: url(isla.gif);
margin-left: 2cm; margin-right: 2cm}
Sets for the whole page the font, line spacing, background image and margin thickness. If certain specific elements inside the page need to have other characteristics different from the general one, then they must be defined separately.
Manual of HTML v2.1
Ing Enrique Garrido-Lecca Risco
Universidad ALAS Peruanas
1 comment
Comments from the old blog, as they were written. Comments are closed.
Oriol Rius
He estat aplicant els estils al portal i he descovert un manual en castella
sobre els estils molt millor, ja q l'anterior malgrat esta molt bé com a
introducció té moltes mancances, la url del nou manual
és:
http://www.TejedoresDelWeb.com/avanzado/css/