If the type attribute is not declared, it defaults to text/css.


Is there a performance cost (however small) for not declaring type="text/css" in the <style> element?


12 个解决方案



From MDN Style docs on the type attribute:


This attribute is optional and defaults to text/css if it's missing.


Since it defaults to the correct one, there is no impact.




You might as well ask if there is a performance cost for making an HTTP request in the first place. It is impossible to completely eliminate the notion of performance itself. At some point you're either at 100% performance efficiency for your page, standards compliant or not. The complimentary aspect to performance is standards in this case. Another example would be entrepreneurialism and execution. All the great ideas in the world are utterly useless unless you can execute to make said business fit into an economic model that works in the real world.


Now it is possible to make great leaps in performance. Back in the 80s and 90s, you could either buy a cheap Honda (non-standards here) or you could improve performance (Porsche, Ferrari or McLaren). Then Tesla came along and completely blew away even the best in acceleration. Disclaimer: I really don't care about cars but it's an easy analogy for most to comprehend.


As someone who has left very few rocks untouched, I can attest to you personally that eventually, it is not you though your environment in which performance increases should be sought. So while I commend you for trying to think outside the box I would make other considerations as to where to improve performance.


  • Be wary of too much utilization of the * selector.
  • 要小心使用*選擇器。
  • Avoid CSS animation of box-shadow except on ::after and ::before pseudo-elements.
  • 避免框陰影的CSS動畫,除了::after和:before偽元素。
  • Be aware of which selector/rules aren't used.
  • 注意哪些選擇器/規則沒有被使用。
  • Be aware to use a single compiled stylesheet for an entire domain you should have very well-honed CSS skills.
  • 請注意,對於整個域使用一個編譯過的樣式表,您應該具有非常完善的CSS技能。
  • Avoid CSS class puking (class attributes on frigin everything).
  • 避免CSS類puking(所有類的類屬性)。
  • Avoid CSS position absolute and fixed on moderate or minor layout aspects; reserve for only major layout elements.
  • 避免CSS位置的絕對和固定在中度或小布局方面;只保留主要布局元素。
  • Minify your CSS when and wherever possible.
  • 盡可能的縮小CSS。
  • Reduce the number of HTTP requests.
  • 減少HTTP請求的數量。
  • Use third-party tools like Yahoo's YSlow and Google PageSpeed Insights for further insight.
  • 使用第三方工具,如雅虎的YSlow和谷歌頁面顯示的深入見解。



It won't have an impact on performance, however older browsers and non standard browsers may misinterpret the content within the elements when the type is not set. I have quite often used embedded blocks with a type element, and haven't seen an issue when using common browsers, IE, Chrome, Firefox or Safari.




As you already said in the your question the default type is 'text/css', which means if the type attribute isn't present the browser uses 'text/css'.


For browsers based on the webkit engine, it's actually faster to omit the type attribute because it first checks for the type to be empty. Therefore if you don't omit the type the browser have to at least do 1 more check. (The check if the provided type is matching text/css).


I could imagine other browsers (not based on webkit) are implementing the css check likely the same. Because for checking an empty string is "easier" from the CPU's point of view (in the case of a webkit based browser it's an AtomicString from the webkit engine).


You can find the css check inside the webkit engine implementation. Can also be found inside the chromium implementation.




According to HTML5 the default type for style is text/css and the default type for the script tag is text/javascript,Even if you dint specify, they will consider the defaults. so that there wont be any performance issue for not mentioning the type considering most modern browsers




The main purpose of the type="text/css" is Set the value of the type attribute to indicate the language of the linked (style sheet) resource. This allows the user agent to avoid downloading a style sheet for an unsupported style sheet language. (www.w3.org)


so basically, it can only reduce the page load by avoiding render blocking files to be downloaded. so its safe to say that, using type attribute to specify the stylesheet type could sometimes increase the pagespeed.




I think it should have an impact, even though it's optional.


Let us consider both cases:


  1. Case 1: type is present
    While parsing html document browser will parse style tag and read type attribute and will know the format of the file. It will check the support for type and start download.
  2. 案例1:在解析html文檔瀏覽器時,類型是存在的,它將解析樣式標簽和讀取類型屬性,並知道文件的格式。它將檢查對類型的支持並開始下載。
  3. Case 2: type is not present
    While parsing html document browser will parse style tag and check type attribute.Since it doesn't have a type attribute it will check default type saved in browser settings and try to download the file based on the default settings.
  4. 案例2:解析html文檔瀏覽器將解析樣式標記並檢查類型屬性時不存在類型。由於它沒有類型屬性,它將檢查保存在瀏覽器設置中的默認類型,並嘗試根據默認設置下載文件。

Now the additional step of checking the default settings and applying those should take additional time in case 2. So I would say there's a performance impact but it's really negligible. Specifying type should increase performance.




Since the introduction of HTML 5.x, it's no longer required, if your clients browser have already support HTML5, then you can just omit that one.

自從HTML 5的引入。x,它不再是必需的,如果你的客戶端瀏覽器已經支持HTML5,那么你可以省略它。



This is optional in latest versions, including or excluding the tag does not matter because it is default but including this Tag is not a bad idea because it will help the browsers to identify the script easily- especially old versioned.




Simple answer: Nope :)


It's like a human saying "Hey. How you doing?" instead of "Hey. How are you doing?"... Your brain knows what you trying to say and it doesn't take extra time for you to understand the sentence.




Normally it has no performance cost and with html 5 there should be no problem either its better to not declare it then to set the wrong type

通常它沒有性能成本,使用html 5也應該沒有問題,最好不要聲明它,然后設置錯誤的類型



As per MDN


type attribute is optional and defaults to text/css.


So need not to worry about the performance cost, if not declaring type="text/css" in the <style> element?




Can I skip attribute “type” in “style” tag in HTML5? [duplicate] Use rowspan attribute in
粤ICP备14056181号  © 2014-2021 ITdaan.com