7

Depending on where I look, I see different way to include css.

Examples

<link rel="stylesheet" type="text/css" media="screen, projection" href=""/>
<link rel="stylesheet" type="text/css" media="all"                href=""/>
<link rel="stylesheet" type="text/css" media="screen"             href=""/>
<link rel="stylesheet"                                            href=""/>

Do they all do the same?

Is one of them the correct way?

1
  • 1
    Have a look through w3.org/TR/CSS2/media.html#media-types. Specific times you may wish to define a media different than all is for example when you want to create printer friendly page. Commented Jun 16, 2011 at 13:04

6 Answers 6

9

All are correct. The type attribute is not required - it is just a hint for browsers but can be omitted. The media attribute tells the browser when the CSS file should be used. For example, if you specify media="print" the CSS file will only get used when printing the page (try to print a Wikipedia page, for example).

Generally this variant is fine in most situations:

<link rel="stylesheet" type="text/css" href="..."/>
Sign up to request clarification or add additional context in comments.

Comments

4

At minimum you need the rel and href attributes. The type attribute is often used, but not required.

The media attribute is used to target specific devices.

For Example:

<link href="print.css" rel="stylesheet" type="text/css" media="print" /> 

tells the browser to apply the print.css file only if the user is trying to print the webpage.

1 Comment

Thanks, updated my answer. I thought it was only optional in HTML5, like the script type. I guess I was wrong.
1

They are all correct syntax.

Maybe this will help you : css media

The css will be picked depending on the media tag

  • all

    Suitable for all devices.

  • braille

    Intended for braille tactile feedback devices.

  • embossed

    Intended for paged braille printers.

  • handheld

    Intended for handheld devices (typically small screen, limited bandwidth).

  • print

    Intended for paged material and for documents viewed on screen in print preview mode. Please consult the section on paged media for information about formatting issues that are specific to paged media.

  • projection

    Intended for projected presentations, for example projectors. Please consult the section on paged media for information about formatting issues that are specific to paged media.

  • screen

    Intended primarily for color computer screens.

  • speech

    Intended for speech synthesizers. Note: CSS2 had a similar media type called 'aural' for this purpose. See the appendix on aural style sheets for details.

  • tty

    Intended for media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities). Authors should not use pixel units with the "tty" media type.

  • tv

    Intended for television-type devices (low resolution, color, limited-scrollability screens, sound available).

Comments

1

IMHO the 4th is the least good as it does not declare the stylesheet file type, although it is optionally.

<link rel="stylesheet" type="text/css" media="screen, projection" href=""/>
<link rel="stylesheet" type="text/css" media="all"                href=""/>
<link rel="stylesheet" type="text/css" media="screen"             href=""/>

These three differ in the media, as you probably noticed. The media tells to which media the stylesheet should be applied to. See Section 7 (Media Types) of the CSS 2 standard.

I personally prefer this way:

<style type="text/css">
  @import url("import1.css");
  @import url "import2.css";
</style>

For more information on @import have a look at this article by about.com .

Comments

1

Media attribuute specifies when the css file should be loaded

it should be working in all major browsers

Valid values: for media attribute

  • screen - Computer screens (this is default)
  • tty - Teletypes and similar media using a fixed-pitch character grid
  • tv - Television type devices (low resolution, limited scroll ability)
  • projection - Projectors
  • handheld - Handheld devices (small screen, limited bandwidth)
  • print - Print preview mode/printed pages
  • braille - Braille feedback devices
  • aural - Speech synthesizers
  • all - Suitable for all devices

reference w3 schools

Comments

0
<LINK href="special.css" rel="stylesheet" type="text/css">

Reference: http://www.w3.org/TR/html40/present/styles.html

12 Comments

Using CAPITALS for tags is a terrible idea and pretty bad advice.
...and you should also always close the link tag, as the TS had done.
In any case it's terrible practise to not close tags. You never know how a browser can handle it. @WTP
@DarkLightA, w3schools is not affiliated with the w3c. If you haven't seen it already, please review w3fools.com. Browsers will handle link elements just fine, even IE is good about closing stand-alone tags.
I know. I still think it's terrible advice even coming from the W3C.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.