Web Accessibility Rules in C#

Use the AccessibilityRules repository to access WCAG-related principles, guidelines, success criteria, and techniques in C#. Select rules by code, pass them to CreateValidator(), and validate only the accessibility checks your workflow needs.

Accessibility rules are a set of guidelines, standards, or best practices that define how digital content and interfaces should be designed and developed to ensure they are accessible to people with disabilities. These guidelines are intended to make digital content, such as websites, apps, and multimedia, accessible to a wide range of people, including those with visual, hearing, motor, and cognitive impairments. Accessibility rules are crucial for promoting digital inclusion and ensuring everyone has equal access to online information and services.

The structure of the quick reference can be presented in the form of a diagram, which is indicated below:

Web accessibility rules structure with principles, guidelines, and criteria

The directory contains the rules that implement the IRule interface.

Accessibility Rules

The AccessibilityRules class is a repository of WCAG-related requirements, success criteria, and techniques. Its structure corresponds to the WCAG Quick Reference. In order to view the list of rules, you need to initialize the WebAccessibility container and use the property Rules.

1    // Initialize a webAccessibility container
2    var webAccessibility = new WebAccessibility();
3
4    // Get the accessibilityRules repository
5    AccessibilityRules accessibilityRules = webAccessibility.Rules;

The property returns an object of type AccessibilityRules, which has such methods for accessing the rules:

MethodsDescription
GetPrinciple(string code)Returns Principle object by code from WCAG.
GetPrinciples()Returns a list of principles IList< Principle>.
GetRules(params string[] codes)Returns a list of rules IList< IRule> for given codes from WCAG.

The directory contains rules that inherit from the abstract class Rule. There are several types of rules:

IRule

All rules implement the interface IRule is a public interface that defines the basic properties of the rules:

PropertyDescription
CodeRule code from the quick reference WCAG.
DescriptionDescription of Rule from the quick reference WCAG.

Rule

The base abstract class for all rules, which implements the interface IRule. To get any rule, you can use the GetRules() method:

 1// Retrieve and list accessibility rules by code with their descriptions from the rules repository
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// List of rule codes can contain both technique codes and principles, guidelines and criteria - all rules that implement the interface IRule
 7string[] rulesCodes = new string[] { "H2", "H37", "H30", "1.1", "1.2.1" };
 8
 9// Get a list of IRule objects; if a rule with the specified code is not found, it will not be in the list
10IList<IRule> rules = webAccessibility.Rules.GetRules(rulesCodes);
11
12// Get code and description of rule
13foreach (IRule rule in rules)
14    Console.WriteLine("{0}:{1}", rule.Code, rule.Description);

The program will output to the console:

1    H2:Combining adjacent image and text links for the same resource
2    H37:Using alt attributes on img elements
3    H30:Providing link text that describes the purpose of a link for anchor elements
4    1.1:Text Alternatives
5    1.2.1:Audio-only and Video-only (Prerecorded)

Principle

At the first level of the list of rules are the Principles, they indicate the main direction and purpose of the rules that are in this section. Therefore, work with the directory begins with them.

The Principle class inherited from Rule and also contains a list of guidelines.

MethodDescription
GetGuideline(string code)Get Guideline by code from WCAG, contained in principle. Return Guideline object.
GetGuidelines()Get a list of Guideline from Principle. Return IList<Guideline> object.

An example of how to get a Principle object from a list of rules:

 1// Retrieve and display the code and description of an accessibility principle by its code
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// Get the principle by code
 7Principle rule = webAccessibility.Rules.GetPrinciple("1");
 8
 9// Get code and description of principle
10Console.WriteLine("{0}:{1}", rule.Code, rule.Description);  // output: 1:Perceivable

Guideline

The Guideline class inherited from Rule and contains a criteria list. Guidelines are the next level after principles. They outline frameworks and general goals that help authors understand success criteria and better apply the techniques.

MethodDescription
GetCriterion(string code)Get Guideline by code from WCAG, contained in current Guideline. Return Criterion object.
GetCriterions()Get a list of Criterion from current Guideline. Return IList< Criterion> object.

An example of how to get a Guideline object from a list of rules:

 1// Retrieve and display the code and description of an accessibility guideline
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// Get the principle by code 
 7Principle principle = webAccessibility.Rules.GetPrinciple("1");
 8
 9// Get guideline 1.1
10Guideline guideline = principle.GetGuideline("1.1");
11if (guideline != null)
12{
13    Console.WriteLine("{0}:{1}", guideline.Code, guideline.Description, guideline);  // output: 1.1:Text Alternatives
14}

Criterion

The Criterion class describes the WCAG success criteria, inherited from abstract class Rule. Detailed information can be found here – Understanding Techniques for WCAG Success Criteria. The basis for determining conformance to WCAG is the success criteria from the standard – not the techniques.

Verifiable success criteria are provided for each recommendation so WCAG can be applied in areas where compliance testing is required.

PropertyDescription
LevelCompliance levels: A (lowest), AA and AAA (highest).
SufficientTechniquesReturns list of Sufficient techniques that are reliable ways to meet the success criteria.
AdvisoryTechniquesReturns list of Advisory techniques that are suggested ways to improve accessibility. They are often very helpful to some users, and may be the only way that some users can access some types of content.
FailuresReturns list of Failures that are things that cause accessibility barriers and fail specific success criteria.

The criterion contains a list of techniques for meeting WCAG web content accessibility guidelines. If all sufficient methods for a given criterion are supported by accessibility, then the success criterion has passed.

Use the GetCriterion() method to access criteria from guideline:

 1// Retrieve and display the code and description of an accessibility criterion
 2
 3// Initialize a webAccessibility container
 4WebAccessibility webAccessibility = new WebAccessibility();
 5
 6// Get the principle by code 
 7Principle principle = webAccessibility.Rules.GetPrinciple("1");
 8
 9// Get guideline 
10Guideline guideline = principle.GetGuideline("1.1");
11
12// Get criterion by code
13Criterion criterion = guideline.GetCriterion("1.1.1");
14if (criterion != null)
15{
16    Console.WriteLine("{0}:{1} - {2}", criterion.Code, criterion.Description, criterion.Level); // output: 1.1.1:Non-text Content - A
17
18    // Get all Sufficient Techniques and write to console
19    foreach (IRule technique in criterion.SufficientTechniques)
20        Console.WriteLine("{0}:{1}", technique.Code, technique.Description);
21}

Check HTML Against Specific Web Accessibility Rules

This example demonstrates how to validate an HTML document for web accessibility using a specified set of rule codes:

 1// Validate an HTML document using specific rule codes with C#
 2
 3string htmlPath = Path.Combine(DataDir, "input.html");
 4
 5// Initialize a webAccessibility container
 6WebAccessibility webAccessibility = new WebAccessibility();
 7
 8// List of necessary rules for checking (rule code according to the specification) 
 9string[] rulesCode = new string[] { "H2", "H37", "H67", "H86" };
10
11// Get the required IList<Rule> rules from the rules reference
12IList<IRule> rules = webAccessibility.Rules.GetRules(rulesCode);
13
14// Create an accessibility validator, pass the found rules as parameters, and specify the full validation settings
15AccessibilityValidator validator = webAccessibility.CreateValidator(rules, ValidationBuilder.All);
16
17// Initialize an object of the HTMLDocument
18using (HTMLDocument document = new HTMLDocument(htmlPath))
19{
20    // Check the document
21    ValidationResult validationResult = validator.Validate(document);
22
23    // Return the result in string format
24    // SaveToString - return only errors and warnings
25    Console.WriteLine(validationResult.SaveToString());
26}

FAQ

How do I get accessibility rules by code?

Use WebAccessibility.Rules.GetRules() and pass the required rule codes, such as H37, H67, or 1.1.1.

What is the difference between a criterion and a technique?

A criterion defines the WCAG success condition. A technique describes one possible way to satisfy that criterion or detect a failure.

Should I validate all rules or a selected rule list?

Use all rules for broad audits. Use selected rule codes when you need a focused check, faster review, or validation for a specific accessibility requirement.

Related Articles

You can also use the online Web Accessibility Checker for a quick page-level accessibility scan.