The link element allows authors to link external documents into the HTML document.
Usually it is used to link external .css (style) files into the document.
Another common use of the link tag is to identify an own or a specific search provider, as shown in Example 2.
If you want to import a CSS file into another CSS file, use the @import at-rule.
If you need to modify the contents of an external style sheet dynamically, use the styleSheet object and the cssRules and rules collections.
For further details, see the examples below.
To install a search provider from JavaScript, use the AddSearchProvider method.
Specifies or retrieves the value of the bottom style property as a floating-point number that specifies the value in the current unit type of the bottom property.
Specifies or retrieves the value of the height style property as a floating-point number that specifies the value in the current unit type of the height property.
Specifies or retrieves the value of the left style property as a floating-point number that specifies the value in the current unit type of the left property.
Specifies or retrieves the value of the right style property as a floating-point number that specifies the value in the current unit type of the right property.
Specifies or retrieves the value of the top style property as a floating-point number that specifies the value in the current unit type of the top property.
Specifies or retrieves the value of the width style property as a floating-point number that specifies the value in the current unit type of the width property.
Example HTML code 1:
This example illustrates the use of the link element:
This example shows how to tell the client browser that you offer a search plugin from HTML. If the search provider is currently installed, it will be selected search engine. Note that the AddSearchProvider method provides similar functionality in JavaScript.
Code
searchProvider.xml
<head><linkrel="search"type="application/opensearchdescription+xml"href="searchProvider.xml"title="Dottoro Search"/></head><body>
Please open the drop-down menu of your browser's Search Box.
</body>
<?xmlversion="1.0"encoding="UTF-8"?><OpenSearchDescriptionxmlns="http://a9.com/-/spec/opensearch/1.1/"xmlns:moz="http://www.mozilla.org/2006/browser/search/"><!-- a brief human-readable title that identifies the search engine --><ShortName>Dottoro</ShortName><!-- a brief human-readable title that identifies the search engine --><LongName>Dottoro Search Engine</LongName><!-- description of the search engine --><Description>Use Dottoro to search on dottoro.com</Description><!-- an URL where the search requests can be handled --><Urltype="text/html"method="get"template="http://www.dottoro.com/search.php?query={searchTerms}"/><!-- the image that is associated with the search engine --><Imageheight="16"width="16"type="image/x-icon">http://www.dottoro.com/favicon.ico</Image><!-- character encoding of the search --><InputEncoding>UTF-8</InputEncoding><OutputEncoding>UTF-8</OutputEncoding><!-- the URL of the Search Home Page --><moz:SearchForm>http://www.dottoro.com/search.php</moz:SearchForm></OpenSearchDescription>
This example shows how to change a style rule in an external style sheet:
Code
red.css
<head><linkrel="stylesheet"type="text/css"href="red.css"/><scripttype="text/javascript">function ChangeRedColor () {
// the imported style sheet
var importedSheet = document.styleSheets[0];
// the first rule in the style sheet
var rules = importedSheet.cssRules ? importedSheet.cssRules : importedSheet.rules;
var firstRule = rules[0];
// change the color
firstRule.style.color = "#00ff00";
}
</script></head><body><buttononclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button><br/><br/><divclass="red">red division</div><div>non-red division</div><spanclass="red">red span</span></body>
This example is the same as the previous one, but it uses the sheet and styleSheet properties of the link element to get the style sheet:
Code
red.css
<head><linkid="myLink"rel="stylesheet"type="text/css"href="red.css"/><scripttype="text/javascript">function ChangeRedColor () {
var linkTag = document.getElementById ("myLink");
// the imported style sheet
var importedSheet = linkTag.sheet ? linkTag.sheet : linkTag.styleSheet;
// the first rule in the style sheet
var rules = importedSheet.cssRules ? importedSheet.cssRules : importedSheet.rules;
var firstRule = rules[0];
// change the color
firstRule.style.color = "#00ff00";
}
</script></head><body><buttononclick="ChangeRedColor ()">Change the color of the '.red' style rule!</button><br/><br/><divclass="red">red division</div><div>non-red division</div><spanclass="red">red span</span></body>