Archived topic
Load CSS file only if element with class is present on the page
3 replies · Started by Rostyslav on July 12, 2022
Hello,
I use a Customizer to add aditional CSS (id="wp-custom-css"). The problem is all that css is loading in all pages. Is there any way to load it dynamically? Load CSS file only if element with class is present in that particular page.
Thank!
Hi there,
do you want to only load wp-custom-css if there is a specific class?
and if there is no class DO NOT load wp-custom-css
No. The customizer's CSS is loading in all my pages. It is loading all CSS even if some styles are not effecting that specific page.
How can I load just part of that CSS that affects the specific page instead of loading all custom CSS of all pages.
How can I load just part of that CSS that affects the specific page instead of loading all custom CSS of all pages.
I don't think there's an easy way to achieve this.
If you want, you can use the hook element, wrap the CSS specifically for some pages like this <style>your css</style> , and hook it into the wp_head, assign it to those pages.
But in this way, it's not easy to manage all your CSS.
I checked your CSS file, there're not many codes in it, I don't think you need to worry about this.