Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

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

Jesse,

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.

Erik

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!

Erik