Rules which enforce generally accepted best practices.

AvoidInlineStyles

Since: PMD 6.45.0

Priority: Medium (3)

Don’t mix content and style. Use separate CSS-files for the style and introduce classes. This helps to reuse common styles.

This rule is defined by the following XPath expression:

//*[@style]

Example(s):

<div style="font-size: 12pt;"></div>

Use this rule by referencing it:

<rule ref="category/html/bestpractices.xml/AvoidInlineStyles" />

UnnecessaryTypeAttribute

Since: PMD 6.45.0

Priority: Medium (3)

In HTML5 the explicit type attribute for link and script elements is not needed. Modern browsers expect stylesheets to be in CSS and scripts to use JavaScript.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.html.rule.bestpractices.UnnecessaryTypeAttributeRule

Example(s):

<!-- Instead of -->
<link type="text/css" rel="stylesheet" href="css/styles.css" />
<script type="text/javascript" src="js/scripts.js"></script>

<!-- Just use -->
<link rel="stylesheet" href="css/styles.css" />
<script src="js/scripts.js"></script>

Use this rule by referencing it:

<rule ref="category/html/bestpractices.xml/UnnecessaryTypeAttribute" />

UseAltAttributeForImages

Since: PMD 6.45.0

Priority: Medium (3)

Always use an "alt" attribute for images. This provides an alternative text and is extensively used by screen readers.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.html.rule.bestpractices.UseAltAttributeForImagesRule

Example(s):

<!-- instead of -->
<img src="house.jpeg">

<!-- use this -->
<img src="house.jpeg" alt="A house from the 18th century">

Use this rule by referencing it:

<rule ref="category/html/bestpractices.xml/UseAltAttributeForImages" />