rss

CSS - Declaring a style sheet


Declaring a style sheet

Style sheets are not directly integrated into the W3C's HTML recommendations. For this reason, you need to include elements in the HTML code indicating both the type of document, meaning the version of the HTML and CSS recommendations used by the pages, and the styles themselves.

Document Type Declaration

In the HTML page, you need to include a document type declaration, a reference to the HTML standard being used. The declaration is made by adding a line like this one:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>...</HEAD>
<BODY>Page content</BODY>
</HTML>

What's more, a metatag can be used to tell the browser or search engines which language has been used for defining style sheets. This metatag, included in the HTML header of the document, is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
</HEAD>
<BODY>Page content</BODY>
</HTML>

Incorporating styles

The styles may be incorporated within the HTML document in four different ways:

Document style

Style sheets in a web page are declared with the STYLE tag, placed within the tags <HEAD> and </HEAD>.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<STYLE type="text/css">
<!--

Style definitions;

-->
</STYLE>

</HEAD>
<BODY></BODY>
</HTML>
The attribute type="text/css" in the <STYLE> tag is used to specify the stype of style sheet being used. The comment tags <!-- ... --> are used to stop older browsers, which do not support style sheets, from displaying this information on-screen.

Inline style

You can also define a style within a document tag. This sort of declaration is called an inline declaration.

This method for defining style sheets is not recommended, as it runs counter to the very purpose of style sheets, as the style is being embedded within each element separately. Nonetheless, it may be used to define a special style for a particular HTML element, which does not need a universal definition.

To define an inline style, simply add the STYLE attribute to the HTML tag to which you wish to apply a particular style:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
...
</HEAD>
<BODY>
...
<TAG Style="style:value;"> ... </TAG>
...
</BODY>
</HTML>

Note: A style can be applied "in-line" to any HTML tags, except for the following: BASE, BASEFONT, HEAD, HTML, META, PARAM, SCRIPT, STYLE, TITLE

Here is an example of a style applied to an <H1> tag:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
...
</HEAD>
<BODY>
...
<H1 Style="Font: 18px Verdana; font-weight:bold;"> Title </H1>
...
</BODY>
</HTML>

External style

Being able to store style sheet definitions outside of the document is a plus, as it is therefore possible, by editing the file containing the style sheets, to change the look of all the web pages which use that sheet.

First, you need to create a text file containing style sheets, whose extension is .css, such as style.css:

<!--
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
body {background-image: home.gif;}
LI {font: 13px Verdana;}
B {font: 14px Verdana; font-weight: bold;}
A {
font:12px Verdana;
font-weight: bold;
color=black;
text-decoration: none;
}
H1 {font: 16px Arial;font-weight: bold;color=black;}
H2 {font: 14px Arial;font-weight: bold;color=black;}
-->

Next, in every HTML page, add a shortcut to the page containing the style definitions:

<HTML>
<HEAD>
<LINK rel="stylesheet" type="text/css" href="style.css">
</HEAD>
...
  • The <LINK> tag lets the browser know that it has to look for a document located outside the HTML page.
  • The attribute rel="stylesheet" specifies that the document in question is an external style sheet.
  • The attribute type="text/css" specifies the type of style sheet.
  • The attribute href=" URL " shows the URL of the style sheet (its location on the Internet)

Imported style

The W3C's recommendations also offer a final way to include style sheets in a document: by importing style sheets. It is possible to import external style sheets when declaring the document's style by inserting the command @IMPORT immediately after the style tag:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<STYLE type="text/css">
<!--
@IMPORT URL(url of the sheet to import);

Definition of document styles;

-->
</STYLE>
</HEAD>
<BODY></BODY>
</HTML>

Note: If there are multiple imported definitions all affecting the same tag, only the last one will be utilised by the browser.

The <SPAN> and <DIV> tags

Sometimes, within a single paragraph, you need to apply different styles to certain text segments, and this is what the <SPAN> and <DIV> tags are used for.

The <SPAN> tag

The <SPAN> tag is used to apply styles to segments of a paragraph.
It can be used with either ID or CLASS.
Its syntax is:

<SPAN class=Name_of_class> Text </SPAN>

The <DIV> tag

Instead of applying to a few words within a paragraph, the DIV tag applies to a block of text, which may be one or several paragraphs.

The DIV tag's syntax is:

<DIV class=major> paragraphs </DIV>

Cascading styles

Multiple styles can be defined by using the different methods of implementing CSS. For this reason, invoking several external style sheets results in what is called a cascading styles, a combination of styles for various HTML elements. If multiple styles affect the same element, only the last one will be kept.

<LINK rel=stylesheet type="text/css" href="style1.css">
<LINK rel=stylesheet type="text/css" href="style2.css">
<LINK rel=stylesheet type="text/css" href="style3.css">

If multiple conflicting styles are found in the external style sheets, CSS recommendations allow users to choose between several alternative style sheets using the rel attribute of the STYLE tag, combined with the TITLE attribute for selecting them by name:

<LINK rel="alternate stylesheet" type="text/css" href="style1.css" title="style1">
<LINK rel="alternate stylesheet" type="text/css" href="style2css" title="style2">
<LINK rel=stylesheet type="text/css" href="style2.css">

What's more, when multiple styles are invoked within a page using the various possible inclusion methods, and some styles conflict, the style closest to the content is the one applied. The priority, in descending order, is as follows:
Inline style > Document style > Imported style > External style
This document entitled « CSS - Declaring a style sheet » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the licence, as this note appears clearly.
 

CSS - Declaring a style sheet Style sheets are not directly integrated into the W3C's HTML recommendations. For this reason, you need to include elements in the HTML code indicating both the type of document, meaning the version of the HTML and CSS recommendations used by the... en.kioskea.net/css/cssimplant.php3
CSS - Colours The CSS standard offers different ways to define colours: by name with hexadecimal notation with decimal notation HTML has names set for a limited number of colours (see HTML colours). The number of colours offered by HTML is not sufficient for... en.kioskea.net/css/css-couleurs.php3
CSS - Style Classes A web designer might want to assign different styles to the same tags. For this reason, the CSS specifications introduced the concept of classes. The definition of classes is as simple as that of styles. It involves specifying a selected tag (as with... en.kioskea.net/css/cssclass.php3
Create CSS easilyCreate CSS easily Easy links CSS is a short term for Cascading Style Sheets. CSS is used to alter the display of the HTML coded environment. For example, using CSS can change the appearance of your scroll bar, the color, headings... en.kioskea.net/faq/sujet-593-create-css-easily
"Trojan.Win32.BHO.agz" / "Csseqch.Hello, I have a big problem: (I have a horse troie "Csseqch.dll" but impossible to turn pourait if someone give me a little hand ... Please I am in my Xp and AV is Kapersky;) ToM Requirements: Windows XP Internet Explorer 7.0 en.kioskea.net/forum/affich-377-trojan-win32-bho-agz-csseqch
Which font it supports ?Hi everybody can someone tell me what fonts does HTML/CSS support? I need fonts like Comic Sans MS, or at least tell me if it supports that. en.kioskea.net/forum/affich-20036-which-font-it-supports
Fonts neededHi everybody can somebody tells me what fonts does HTML/CSS support? I need fonts like Comic Sans MS, or at least tell me if it supports that. en.kioskea.net/forum/affich-20642-fonts-needed
Download DVDSmith Movie BackupDVDSmith Movie Backup is the best manner of copying your DVD on your hard disk in a split second and with the slightest effort.Allow you to abolish all hanging protection copy it such as CSS, RC, RCE, APS, UOPs and Sony ArccOS. Program creates a clone... en.kioskea.net/telecharger/telecharger-615-dvdsmith-movie-backup
Answers for « CSS »