Sheath rule
best-practices-no-script-style-type
Omit unnecessary
type attribute on <script> and <style> elements.
- Package
- Core
- Category
- Best Practices
- Default severity
- info by default
- Auto-fix
- Auto-fix available
#Why
In HTML5, the default type for <script> is text/javascript and for <style> is text/css. Specifying these default values is unnecessary and adds to page weight.
#Examples
#Bad
<!-- Unnecessary type on script -->
<script type="text/javascript">
console.log('Hello');
</script>
<script type="application/javascript" src="app.js"></script>
<!-- Unnecessary type on style -->
<style type="text/css">
body { margin: 0; }
</style>
#Good
<!-- No type needed for JavaScript -->
<script>
console.log('Hello');
</script>
<script src="app.js"></script>
<!-- No type needed for CSS -->
<style>
body { margin: 0; }
</style>
<!-- Keep type="module" - it's required! -->
<script type="module" src="app.mjs"></script>
<!-- Keep non-default types -->
<script type="application/ld+json">
{ "@context": "https://schema.org" }
</script>
#Auto-fix
The fixer removes unnecessary type attributes:
<!-- Before -->
<script type="text/javascript" src="app.js"></script>
<!-- After -->
<script src="app.js"></script>
#Important: Keep type="module"
The type="module" attribute is required for JavaScript modules and is not removed. The following values are reported:
- Any standard JavaScript MIME essence on
<script>, including legacy values such astext/javascript1.5andapplication/x-javascript - Leading and trailing HTML ASCII whitespace is ignored when classifying a script type, matching browser script processing
- Empty and whitespace-only type attributes on
<script>are unnecessary too type="text/css"on<style>
An empty type on <style> is preserved. Unlike omission or text/css, it
does not declare CSS, so removing it could make previously inert content apply.
Script type matching is ASCII case-insensitive but exact. A value with surrounding spaces or MIME parameters is a data-block type in HTML and is not removed.
#References
- HTML Spec: Script Type - Default type behavior for scripts
- MDN: Script Element - Documentation on the type attribute
#Related Rules
- best-practices-no-inline-styles - Avoid inline styles