Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I skip attribute "type" in "style" tag in HTML5? [duplicate]

Tags:

According to W3Schools, I can skip attribute type for tag script in HTML5.

Evidence:

Differences Between HTML 4.01 and HTML5

The "type" attribute is required in HTML 4, but optional in HTML5.

Can I do exactly the same with style tag? I was unable to find similar note. It only says

type text/css Specifies the media type of the tag

but no info about omitting it. I don't care about IE, Edge or that sort of crap from Microsoft. I would like to write as less unnecessary code as possible. Ideally I would like to use just:

<style>.foo { color: red; }</style>
like image 425
Matt Komarnicki Avatar asked Oct 19 '15 09:10

Matt Komarnicki


People also ask

Does style tag need type?

In theory, other types of styling could also be used between style tags–thus the need for a type attribute. However, in practice, the only type of styling supported by modern browsers at this time (and for the foreseeable future) is CSS. So this attribute is not necessary.

When using the style tag what should be the value of type?

The type attribute identifies the content between the <style> and </style> tags. The default value is "text/css", which indicates that the content is CSS.

Is type text CSS necessary?

When used in either an inline stylesheet or an external stylesheet, the attribute type="text/css" is optional as of HTML5. In the HTML4 spec it was needed, though browsers were forgiving. If omitted, browsers will default to text/css. I tested this all the way back to Internet Explorer 6.

How do you write multiple attributes in style tag?

You can add multiple styling properties at once when using the HTML style attribute - just make sure to separate the name-value pairs with commas. Using a separate stylesheet is very convenient for styling multiple pages, as it's easier to apply changes to one document than to each page separately.


2 Answers

According to the documentation:

type

This attribute defines the styling language as a MIME type (charset should not be specified). This attribute is optional and default to text/css if it's missing.

Also comparing html 4.01 and html 5:

html 4.01

type = content-type [CI]

This attribute specifies the style sheet language of the element's contents and overrides the default style sheet language. The style sheet language is specified as a content type (e.g., "text/css"). Authors must supply a value for this attribute; there is no default value for this attribute.

html 4.01 - The style element w3c

html 5

The type attribute gives the styling language. If the attribute is present, its value must be a valid MIME type that designates a styling language. The charset parameter must not be specified. The default value for the type attribute, which is used if the attribute is absent, is "text/css".

html 5 - The style element w3c

like image 101
Alex Char Avatar answered Oct 24 '22 15:10

Alex Char


Yes, you can, according to the specification:

The type attribute gives the styling language. If the attribute is present, its value must be a valid MIME type that designates a styling language. The charset parameter must not be specified. The default value for the type attribute, which is used if the attribute is absent, is "text/css". [RFC2318]

The default value already is text/css.

like image 42
KittMedia Avatar answered Oct 24 '22 17:10

KittMedia