User:Andrewds1021/CategoryCSS

This solution was created in response to Thread:1507300 and debugged in response to Thread:1559477.

The purpose of this script is to apply a specific stylesheet based on the category membership of the current page. The script uses MediaWiki's api.php to check if the page belongs to one of the specified categories. If so, it imports the specified associated stylesheet.

Configuration
To set the list of categories to search for and the associated stylesheets, copy and paste the following into the appropriate JS page. Replace,  ,  , ... with the actual category page names (case sensitive but without the namespace). Similarly, replace,  ,  , ... with the actual CSS page names using the format required for. This ideally would be done immediately prior to the script import but could possibly be done somewhere else.

Example
The following should apply  from here on Community Central to all pages in the Help category.

Limitations/Additional Notes

 * 1) When creating stylesheets, the customization policy must be followed.
 * 2) The script accepts only one (1) stylesheet per category. If you need/want to apply multiple stylesheets to the same category, use @import.
 * 3) If the page belongs to more than one (1) category for which custom CSS has been assigned, only one (1) of the associated stylesheets will be applied. Based on the ambiguity of the JS specifications, the chosen category could be random. However, it is commonly believed that most modern browsers actually do have predictable output. Based on the expected behavior, the applied stylesheet is likely to be the one associated with the category which, amongst the matched categories, appears first in the category list.