Use CSS to conditionally call up different background image?

We have a large database that is within a table and covers multiple departments. The other departments are asking if when their department is selected, can their own branding show as the background image, instead of this general one (top_bg.jpg) from the CSS below.

If not CSS, is there any way this can be done?

Thank you very much!

Niki

.td-top {
background-image: url(img/top_bg.jpg);
background-repeat: no-repeat;
background-position: 0 0;
margin: 0;
padding: 0;
margin-bottom:10;
}

You can do that by modifying your markup. You could, for example, add a class to your <body> and then add that class to your CSS rule.


.whatever .td-top {
different background;
}

Hello Nikolette.
I am unsure how to do this concerning CSS. But if you put in a little PHP (if department_selected { //use background image }. That would allow you to do this upon load of the website. If you need to change the background once something is clicked jquery or javascript would be able to help you in regards to this. using the .click() function.

Hopefully someone else can come by and explain this better :slight_smile:

Could you say more about what that means? Is this a special page for their department, or is this the same page that others see, too?

What it actually is, is a search result from metadata. Anyone can see the results, not just, for example, Dallas, Richmond or Chicago. If I have a category named “rd=region_dallas_photos,” one named “rd=region_richmond_photos” and one more named “rd=region_chicago_photos” then I need a different background image based on the different categories of metadata selected.

So, for Dallas it might be (I’m revising my previous code):

.td-top {
background-image: url(img/dallas_bg.jpg);
background-repeat: no-repeat;
background-position: 0 0;
margin: 0;
padding: 0;
margin-bottom:10;
}

Richmond:

.td-top {
background-image: url(img/richmond_bg.jpg);
background-repeat: no-repeat;
background-position: 0 0;
margin: 0;
padding: 0;
margin-bottom:10;
}

Chicago:

.td-top {
background-image: url(img/chicago_bg.jpg);
background-repeat: no-repeat;
background-position: 0 0;
margin: 0;
padding: 0;
margin-bottom:10;
}

Thank you.

One way to do this is like I said in my first post.

In your HTML, add a class to the body of your HTML document and give it a class, e.g. <body class=“chicago”>

In your CSS, place that new rule like so (below your original .td-top):


.chicago .td-top {
background: url(images/chicago.jpg) no-repeat 0 0;
}

Rinse and repeat for the other locations.

To add the class in the body, optimally you’d have a function that assigns the body class to the right page dynamically. That can be done server-side or client-side, but with CSS alone, it won’t be possible.

If and only if you are using PHP then try this script in your header:


<head>
  ...
  ...
  ...
<?php
// input criteria
   $category = 'region_dallas_photos';

// replace 'region_' with 'bg-'
   $category = substr( 'region', 'bg-', $category);

// replace '_photos' with '.jpg'
   $category = str_replace(_photos'', '.jpg', $search);

// new background
   $bg_new = $category;
?>

<style type='text/css'>
   .td-top { background-image: url(img/<?php echo $bg_new;?>);}
</style>
</head>
<body>


Typical background image names will be ‘bg-chicago.jpg’, ‘bg-newyork.jpg’, ‘bg-buffalo.jpg’.
Makes it easier for alphabetic path sorting.