Multiple Style Rules:
You may need to define multiple style rules for a single element. You can define these rules to combine multiple properties and corresponding values into a single block as defined in the following example:
h1 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Here all the property and value pairs are separated by a semi colon (;). You can keep them in a ingle line or multiple lines. For better readability we keep them into separate lines.
Grouping Selectors:
You can apply a style to many selectors if you like. Just separate the selectors with a comma as given in the following example:h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
This define style rule will be applicable to h1, h2 and h3 element as well. The order of the list is irrelevant. All the elements in the selector will have the corresponding declarations applied to them.
Embedded CSS - The <style> Element:
You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside <head>...</head> tags. Rules defined using this syntax will be applied to all the elements available in the document. Here is the generic syntax:<head>
<style type="text/css" media="..."> Style Rules ............ </style>
</head>
Attributes:
Attributes associated with <style> elements are:
Example:
Following is the example of embed CSS based on above syntax:
<head>
<style type="text/css" media="all">
h1{
color: #36C;
}
</style>
</head>
Inline CSS - The style Attribute:
You can use style attribute of any HTML element to define style rules. These rules will be applied to that element only. Here is the generic syntax:<element style="...style rules....">
Attributes:
Example:
Following is the example of inline CSS based on above syntax:
<h1 style ="color:#fff;"> This is inline CSS </h1>
This will produce following result:
This is inline CSS
External CSS - The <link> Element:
The <link> element can be used to include an external stylesheet file in your HTML document.
An external style sheet is a separate text file with .css extension. You define all the Style rules within this text file and then you can include this file in any HTML document using <link> element.
Here is the generic syntax of including external CSS file:
<head>
<link type="text/css" href="..." media="..." />
</head>
Attributes:
Attributes associated with <style> elements are:
Example:
Consider a simple style sheet file with a name mystyle.css having the following rules:
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Now you can include this file mystyle.css in any HTML document as follows:
<head>
<link type="text/css" href="mystyle.css" media="all" />
</head>
Imported CSS - @import Rule:
@import is used to import an external stylesheet in a manner similar to the <link> element. Here is the generic syntax of @import rule.
<head>
<@import "URL";
</head>
Here URL is the URL of the style sheet file having style rules. You can use another syntax as well:
<head>
<@import url("URL");
</head>
Example:
Following is the example showing you how to import a style sheet file into HTML document:
<head>
@import "mystyle.css";
</head>
No comments:
Post a Comment