I was wondering if there is an easy way to list all available css classes loaded withing a single html page in javascript or through the developer console in chrome or firefox.
Thank you
I was wondering if there is an easy way to list all available css classes loaded withing a single html page in javascript or through the developer console in chrome or firefox.
Thank you
A bit late but ...
for (let link of document.querySelectorAll("link, style")) {
try {
if (!link.sheet) {
console.warn("Warning:", "Property 'sheet' is not set on element", link)
} else
for (let rule of link.sheet.rules) {
console.log(rule.selectorText)
};
} catch (e) {
console.warn("Warning:", e.message, ". Try set crossorigin='anonymous' on element", link)
}
};
or in the Chrome DevTool window (F12), find the "Elements", then "Styles", tab. On the right side of the "filter" text-box there is a ".cls" option. Click it and an "add new class" input should appear. Focus that input and hit Ctrl + Space. A pick list of all class styles in the current opened document should appear.
Yes, basically you would fire up the console and type:
document.querySelectorAll("*[class]");
The querySelectorAll method works just like CSS attribute selectors in this case. Read more about it in MDN https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll
[...[...$$("[class]")].reduce((s, e) =>
(e.classList.forEach(c => s.add(c)), s), new Set())].sort()
Though wrapped just for readability :-)
There're two shorthands used in the above one liner:
$$
is a shorthand for document.querySelectorAll
[...iterable]
is for Array.from(iterable)
, see the spread syntax (...)And here's the expanded code:
Array.from( // 4
Array.from( // 2
document.querySelectorAll("[class]") // 1
).reduce( // 3
(s, e) => (e.classList.forEach(c => s.add(c)), s),
new Set()
)
).sort() // 5
class
attribute, see attribute selectorsArray.from
to turn the NodeList from step 1 into an ArrayArray.reduce
to collect the CSS classes into a Set, to remove duplicatesAdditionally, we can use console.table()
to show the result nicely:
console.table([...[...$$("[class]")].reduce((s, e) =>
(e.classList.forEach(c => s.add(c)), s), new Set())].sort())
Sort of, you can do it per element easily, inside of Chrome Dev tools use the elements tab to select elements, and then go to the "Computed" tab which shows everything attached to each element.
If it was a big page with lots of elements and you needed to look at all of the CSS, I would just go to the elements and look into the head or header html element and go directly to the files. Depending on the architecture of the page some devs put it in the footer element as well as some inline.
You can also CTRL+F in chrome dev tools and write "stylesheet" which should pull up all of the pages attached sheets of CSS.
Here is how Im currently doing this:
function getAllClasses() {
const classSet = new Set();
const allElements = document.getElementsByTagName("*");
for (let i = 0, n = allElements.length; i < n; i++) {
allElements[i].classList.forEach(cls => classSet.add(cls))
}
return Array.from(classSet);
}
console.log(getAllClasses())