Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:44:28+00:00 2026-06-15T17:44:28+00:00

I’m having trouble with realizing how I would code a div’s background which requires

  • 0

I’m having trouble with realizing how I would code a div’s background which requires a dynamic background. The div has a specific height but the width is the size of the browser.

I have a div which has a default repeat-all background pattern but changes if a button is hovered over or clicked on. The backgrounds do NOT repeat so I’m trying to figure out how to have them expand/resize based on the browser size which is the div size.

Here is what it should look like… Default: https://i.stack.imgur.com/Ogxrh.png Category 1 (Hover/Pressed): https://i.stack.imgur.com/260i8.png

Notice how default is repeating pattern and the other is a full-bg.

My code is currently:

$("#select_all").click(function(e) {
        $("#selected-game").removeClass('game_bg_minecraft').removeClass('game_bg_terraria').removeClass('game_bg_dayz');
     });
     $("#select_minecraft").click(function(e) {
        $("#selected-game").addClass('game_bg_minecraft').removeClass('game_bg_terraria').removeClass('game_bg_dayz');
     });
     $("#select_terraria").click(function(e) {
        $("#selected-game").addClass('game_bg_terraria');
     });
     $("#select_dayz").click(function(e) {
        $("#selected-game").addClass('game_bg_dayz');
     });

HTML Code

<div id="selected-game">
<div class="row">
    <div class="ten columns">
    <h1>TopTitle</h1>
        <div>
        <h4>Selected Game Title</h4>
        </div>
    </div>
</div>
</div>
<div id="game-selection">   
    <div class="select_game">
        <button id="select_all" class="demo-change btn btn-info" style="display: inline-block;">Default</button>
        <button id="select_minecraft" class="demo-change btn btn-info" style="display: inline-block;">Category1</button>
        <button id="select_terraria" class="demo-change btn btn-info" style="display: inline-block;">Category2</button>
        <button id="select_dayz" class="demo-change btn btn-info" style="display: inline-block;">Category3</button>
    </div>
    <div class="game-options">
        More stuff here, yo!
    </div>
</div>

But the problem here is that for every new button i add, i need to remove all other classes or risk the new background not showing up. I’m also lost as to how to have one background repeat while having the other dynamically resize based on div’s width.

I’m also not sure how to cleanly and optimally change for both click and hover while keeping js code to a minimum.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:44:28+00:00Added an answer on June 15, 2026 at 5:44 pm

    I would make general background class that repeats but centers:

    .game-bg{
        background: url("default-bg.jpg) repeat center;
    }
    

    and just change the background property:

    $("#select_item1").click(function(e) {
        $("#selected-game").css('background-image', 'url("myimage.jpg")');
    });
    

    Just make sure you use images that are large enough to display nicely on big monitors. Because the background image centers it expands and crops on the left and right side.

    Another way to go would be to look at the background-size property (but this isn’t supported in all browsers):

    http://www.css3.info/preview/background-size/

    Specificly auto or cover.

    Follow up question:

    If you use jQuery to remove the CSS again it should convert back to the default .game-bg class background:

    $("#selected-game").css('background-image', ''); //leave the background empty
    

    However if you i.e. click on one of the items…. and then hover over the other items. It will revert to the default background, and not of the “active” item. If you also want that it becomes a bit more complex. Then you should add an active class to the item that is active. And on mouseout look for the item with the active class, lookup the ID and based on the ID apply the right background. I hope this makes any sense 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.