SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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;
    }

  2. #2
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    .whatever .td-top {
    different background;
    }
    Maleika E. A. | Rockatee | Twitter | Dribbble



  3. #3
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,167
    Mentioned
    453 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by Nikolette View Post
    if when their department is selected ...
    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?

  5. #5
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    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.
    Last edited by Nikolette; Oct 17, 2012 at 20:51. Reason: added more information

  6. #6
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    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):

    Code CSS:
    .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.
    Maleika E. A. | Rockatee | Twitter | Dribbble



  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,821
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    If and only if you are using PHP then try this script in your header:

    PHP Code:
    <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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •