Title: Adding a CSS file based on body_class
Last modified: June 14, 2023

---

# Adding a CSS file based on body_class

 *  [thetoolman123](https://wordpress.org/support/users/thetoolman123/)
 * (@thetoolman123)
 * [2 years, 12 months ago](https://wordpress.org/support/topic/adding-a-css-file-based-on-body_class/)
 * Hi there,
 * I am wanting to create separate CSS files for each page of my website and wondered
   if this is possible to do so using the body_class ?
 * For example, if I am on my about page, the page would load a stylesheet called
   about.css or if I’m on a contact page, it would load contact.css
 * Is this possible?
 * Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Ivan Maljukanović](https://wordpress.org/support/users/imoptimal/)
 * (@imoptimal)
 * [2 years, 12 months ago](https://wordpress.org/support/topic/adding-a-css-file-based-on-body_class/#post-16819137)
 * Not sure if you need a different CSS file, because you’ll end up duplicating 
   a lot of styles… but here’s a guide on how to add custom classe to your pages(
   that will be equal to pages slug):
 * [https://www.wpbeginner.com/wp-themes/how-to-add-page-slug-in-body-class-of-your-wordpress-themes/](https://www.wpbeginner.com/wp-themes/how-to-add-page-slug-in-body-class-of-your-wordpress-themes/)
 *  Thread Starter [thetoolman123](https://wordpress.org/support/users/thetoolman123/)
 * (@thetoolman123)
 * [2 years, 12 months ago](https://wordpress.org/support/topic/adding-a-css-file-based-on-body_class/#post-16819181)
 * I would probably have one main CCS file with the main stuff like UI stuff and
   then each page would have CSS relating just to that page rather than loading 
   every single page’s CSS into one file
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 12 months ago](https://wordpress.org/support/topic/adding-a-css-file-based-on-body_class/#post-16819579)
 * You can do so, but body classes are determined rather late. I recommend conditionally
   enqueuing appropriate CSS files earlier based on the queried object ID, which
   would be the post ID in the case of single page requests. You can get it with`
   get_queried_object_id()`.
 * It would simplify enqueuing if the CSS file for a particular page was consistently
   named based on the related ID, for example `style-12345.css` for page ID 12345.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Adding a CSS file based on body_class’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 12 months ago](https://wordpress.org/support/topic/adding-a-css-file-based-on-body_class/#post-16819579)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
