Search code examples
jqueryhtmltwitter-bootstrapcssbootstrap-modal

How to change 2 different Bootstrap Modals' background colors?


When a Bootstrap modal is created, .modal-backdrop is added to the end of the HTML to create the background color. I can switch the color manually by just changing that class's color, however I'd like to switch the color of that class depending on which of the 2 modals have been triggered.

I'm currently using a sibling selector to try and differentiate which color I'd like, but regardless of which modal is called it now shows #000.

CSS:

.modal-backdrop {
  background-color: #fff;
}
.card-modal ~ .modal-backdrop {
  background-color: #000;
}

enter image description here


Solution

  • $(".modal1").on('shown.bs.modal', function() {
      $('.modal-backdrop').css('background', 'red');
    });
    
    $(".modal1").on('hidden.bs.modal', function() {
      $('.modal-backdrop').css('background', '#000');
    });
    .wrap {
      padding: 15px;
    }
    h1 {
      font-size: 28px;
    }
    h4,
    modal-title {
      font-size: 18px;
      font-weight: bold;
    }
    .no-borders {
      border: 0px;
    }
    .body-message {
      font-size: 18px;
    }
    .centered {
      text-align: center;
    }
    .btn-primary {
      background-color: #2086c1;
      border-color: transparent;
      outline: none;
      border-radius: 8px;
      font-size: 15px;
      padding: 10px 25px;
    }
    .btn-primary:hover {
      background-color: #2086c1;
      border-color: transparent;
    }
    .btn-primary:focus {
      outline: none;
    }
    .model1 .modal-backdrop {
      background: red;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <div class="wrap">
      <h1>Bootstrap Modal Example</h1>
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal1">
        Modal 1
      </button>
    </div>
    
    <div class="modal fade modal1" tabindex="-1" role="dialog" aria-labelledby="modal1" aria-hidden="true">
    
      <div class="modal-dialog modal-lg">
    
        <!-- Modal Content: begins -->
        <div class="modal-content">
    
          <!-- Modal Header -->
          <div class="modal-header no-borders">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="gridSystemModalLabel"></h4>
          </div>
    
          <!-- Modal Body -->
          <div class="modal-body">
            <p class="body-message centered"><strong>Modal 1 here.</strong>
            </p>
          </div>
    
        </div>
        <!-- Modal Content: ends -->
    
      </div>
    
    </div>
    
    <!---------------------->
    <div class="wrap">
      <h1>Bootstrap Modal Example</h1>
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal2">
        Modal 2
      </button>
    </div>
    
    <div class="modal fade modal2" tabindex="-1" role="dialog" aria-labelledby="modal2" aria-hidden="true">
    
      <div class="modal-dialog modal-lg">
    
        <!-- Modal Content: begins -->
        <div class="modal-content">
    
          <!-- Modal Body -->
          <div class="modal-body">
            <div class="body-message">
              <h4>Modal 2 here.</h4>
              <p>How to change this background colour?</p>
            </div>
          </div>
    
        </div>
        <!-- Modal Content: ends -->
    
      </div>
    
    </div>

    Update

    Properties of Bootstrap modals :

    • shown.bs.modal is fired when your modal is opened
    • hidden.bs.modal is fired when your modal is closed

    What I'm doing is, whenever you try to open a bootstrap model a div with class modal-backdrop is added and I'm simply changing that backdrop's color to whatever you like. And whenever you try to remove your model that div will be removed. So you need to manage it every time. And as per your requirement if you have more then two models then every one's color should be different (at least what I interpreted).