Changing the Overlay background color of sweet alert 2

Eshiett Oto-obong picture Eshiett Oto-obong · Oct 28, 2017 · Viewed 14.5k times · Source

I have recently moved sweet alert 1 to sweet alert 2, but based on the documentation there is no option for theming the overlay background of an alert box like sweet alert 1

.swal-overlay {
  background-color: rgba(43, 165, 137, 0.45);
}.

Please how can I achieve changing the overlay background of sweet alert 2?

Answer

Hayden Passmore picture Hayden Passmore · Oct 30, 2017

To achieve this you can just use jQuery selectors and select the overlay element like so.

<button type="button" onclick="opentheSwal();">OK</button>
<script>
    function opentheSwal() {
        swal(
            'Good job!',
            'You clicked the button!',
            'success'
        );
        $(".swal2-modal").css('background-color', '#000');//Optional changes the color of the sweetalert 
        $(".swal2-container.in").css('background-color', 'rgba(43, 165, 137, 0.45)');//changes the color of the overlay
    }
</script>

You will have to select the .swal2-container.in to change the overlay and apply the css using jqueries .css() function.

Good Luck.