Sweet alert 2 - position swal in middle of a div

12,699

The target param needs to be used with document.getElementById('myDiv') rather than ".myClass"

swal({
    title: 'Hello',
    target: document.getElementById('myDiv')
});

All the target param does is let you determine which div or element the sweetalert2 div gets appended to. The default is <body> but if you specify a div, you will still get a full screen overlay, the code is simply appended into the div of your choice.

As a work around you could provide some override CSS in your style sheet:

#myDiv .swal2-container {
    position:inherit;
    height:500px;
    width:500px;
}

<div class="myClass" id="myDiv"></div>

But this will only work if your div is empty, as soon as you put anything in the div, the alert will be offset underneath any content.

Share:
12,699

Related videos on Youtube

Shay
Author by

Shay

Updated on June 04, 2022

Comments