Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Using an 'if then' argument to dictate different styles for different pages

Forgive the long winded title guys!

Im doing a fairly simple website for a mate of mine, but something has come up that doesn't really 'gel' with my semantic standards :P.

The index page is super simple with just the navigation and a background image. The problem is that I only want a background image for the index page. Is there some argument I could use to solve my problem? for example if the html has a class of x add a background image, if not then make the background colour equal black.

Right now I'm just pointing the index.html page to a different stylesheet but I feel like thats a dodgy solution.

I've searched the internet for this problem, and there are similar scenarios that people are having but I'm a noob at jquery so some further education into my particular problem would be great

Many thanks

4 Answers

Erik McClintock
Erik McClintock
45,783 Points


One way you could achieve this is just within your HTML and CSS interaction.

As you stated, if you apply a class of X to your HTML (I'd apply it to the body tag), you can then target that class in your CSS and set the background-image property for it. On your other pages, you simply omit that class="x" from the body tag, and the background-image property won't load, thus achieving the affect you're going for.


So simple, I feel like an idiot. Cheers dude

Erik McClintock
Erik McClintock
45,783 Points

I know the feeling; sometimes it's those kinds of solutions that are the most difficult to see, particularly when everything else with all of this stuff can be so complicated and confusing. But, that's where forums and helping one another comes into play!

Glad to have been able to help with this one!