Agenda
- Quiz #2
- Stylesheet management
- Simplifying CSS
- Advanced selectors
- Rule specificity
When linking to external style sheets, there are a number of things to know. A standard LINK element:
<link rel="stylesheet"
type="text/css"
href="http://www.mysite.com/css/styles.css"
title="Main Style"
media="screen" />
This features a number of things to keep in mind:
link
tagrel
determines how the HTML will use the file linked totype
attribute is optional (dropped in HTML5), but including it increases browser backward compatibilityhref
defines the path, relative or absolute, to the css file title
attribute allows the user to select different styles if requiredmedia
attribute allows you to specify what medium this should be viewed inmedia
attributemedia="type"
can be a powerful toolall
, aural
, braille
, embossed
, handheld
, print
, projection
, screen
, tty
, tv
all
, screen
, print
with handheld becoming more popularprint
sheet to render text and elements for when the user selects a page to printrel
attributeYou can also have alternate stylesheets supported
rel="stylesheet"
defines the default stylesheets to userel="alternate stylesheet"
allows different styles to be definedtitle
title
attributerel="stylesheet"
and has no title
attribute. All persistent stylesheets are always used when renderingrel="stylesheet"
and has a title
attribute. these styles will be used as the default. preferred stylesheets with the same title are grouped togetherrel="alternate stylesheet"
and has a title
. These stylesheets are supposed to allow the user to choose stylesheets, they are grouped together by title
and show up in the browser's stylesheet selector if it has one (View > Page Style
in Firefox). Each group (by title
) is mutually exclusive.The @import
directive can be used embedded or externally to easily manage the loading of stylesheets
Embedding the @import
works well when dealing with older browsers, as they don't read the command. Simply reference a stylesheet that imports another sheet, so you protect yourself from older browsers. Should also be used in conjunction with an older-style page (lo-fi) for older browsers.
In your document, use the following within the <head> tag:
<link rel="stylesheet" type="text/css" href="/css/basic_styles.css" media="all" />
<style type="text/css" media="all">
@import url("/css/rich_styles.css");
</style>
This allows you to include basic styles in basic_styles.css
(for older, or CSS-less-supported browsers) and have current styles for modern browsers in rich_styles.css
.
You can use the @import
method to import multiple stylesheets from a basic stylesheet that you link to. This will reduce the clutter that can sometimes occur within the <head>
section.
It is also a great place to be able to manage your CSS. From your main document, create a <link>
to your styles.css
page.
In the body of the styles.css
file, import as many files as you need:
@import url("/css/type.css");
@import url("/css/nav.css");
@import url("/css/template.css");
NOTE: when using the @import
directive in an external CSS file, ensure it is the first lines of code in the .css
file. It is also best to not include other CSS rules in a page that uses @import