44

I love this library. I'd like to use it to display a moderate response table on one of my web pages, but the text gets a little wide. I was hoping there was an option to change the width of the overall alert for the page that I'm displaying the table within SweetAlert, but I'm not having any luck. Table & HTML look great, just too much info for the modal.

I can't even find where the width is set in the overall sweetalert.css.

I thought maybe the customClass configuation key might help and I tried:

swal({
       title: priorityLevel +' Issues for '+appName,
       html: appHTML,
       type: "info",
       customClass: 'swal-wide',
       showCancelButton: true,
       showConfirmButton:false
   });

The css was just a simple:

.swal-wide{
    width:850px;
}

This didn't do anything. Anyone have an idea of how to do this or perhaps have played around with the width element?

Thank you!

Watercayman
  • 7,970
  • 10
  • 31
  • 49

14 Answers14

63

try to put !important like this in the css:

.swal-wide{
    width:850px !important;
}

See it working in the snippet.

function TestSweetAlert(){
    swal({
       title: 1 +' Issues for test',
       text: "A custom <span style='color:red;'>html</span> message.",
       html: true,
       type: "info",
       customClass: 'swal-wide',
       showCancelButton: true,
       showConfirmButton:false
   });
};

$('#testeSWAL').on("click",TestSweetAlert);
.swal-wide{
    width:850px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet"/>


<button id="testeSWAL"> Test SWAL </button>

Note: maybe you will have problems with the position.

Joel R Michaliszen
  • 4,164
  • 1
  • 21
  • 28
  • 2
    Many thanks @Joel Ramos Michaliszen. This didn't work for me, but it got me to the answer - my version of SWAL is not the 'real' one - LOL the sweetalert2 doesn't actually have the Customclass function i don't think -so it won't work. I think we were both right in our attempts for the real SWAL, so I'm going to mark the answer as correct. I think I'm going to use plain ol Bootstrap modals for the tables anyway as they are a little better suited. Thanks again. – Watercayman Dec 30 '15 at 03:45
  • 1
    Use `className` instead of `customClass` in Swal 2.0 – shajji Mar 18 '20 at 11:26
  • for sweetalert2 there is the 'grow' option: https://sweetalert2.github.io/v7.html which allows for 'row', 'column', 'fullscreen', or false (the default). – David Burson Aug 20 '20 at 16:32
  • 1
    clarification: there are two different packages: sweetalert and sweetalert2. sweetalert is now on version 2, and for that, use className instead of customClass. For sweetalert2, you can use 'width' or 'grow'. – David Burson Aug 20 '20 at 18:32
28

On the most recent versions, you can use:

width: '800px'

i.e.:

swal({
    title: "Some Title",
    html: "<b>Your HTML</b>",
    width: '800px'
})

Sweet Alert2 Docs

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
7

Rather than overwriting default .sweet-alert css class you can define your own

.sweet-alert.sweetalert-lg { width: 600px; }

Than simply use sweetalert options to set your class only to those alerts you want to be wide:

swal({
  title: "test",
  text: "Am I wide?",
  customClass: 'sweetalert-lg'
});

PS (update): You may need to change sweetalert class a little to center it properly:

.sweet-alert { margin: auto; transform: translateX(-50%); }

Here is jsbin to try it out

Buksy
  • 11,571
  • 9
  • 62
  • 69
6

Better use SweetAlert2 which is a successor of SweetAlert. Just add "width" property, like:

swal({
   title: priorityLevel +' Issues for '+appName,
   html: appHTML,
   type: "info",
   customClass: 'swal-wide',
   showCancelButton: true,
   showConfirmButton:false,
   width: '850px'
});
infografnet
  • 3,749
  • 1
  • 35
  • 35
3

A simple solution is to add the below CSS. reference: Sweet alert 2 modal shows too small

.swal2-popup { font-size: 1.6rem !important; }
icynets
  • 337
  • 2
  • 5
3

If you are using SweetAlert2 available here at https://sweetalert2.github.io/

then for custom width you can use width property of Swal.fire() function like

Swal.fire({
  title: 'Custom width',
  width: 600,
})
Umair Ijaz
  • 191
  • 1
  • 7
2

None of the answers here worked for me. All I had to do was add the width value as an integer (without strings) and remove the 'px' suffix.

swal({
       width: 400,
       html: data,
       showCancelButton: true,
       showConfirmButton:false,
       cancelButtonText: "Close",
});
Jnr
  • 1,504
  • 2
  • 21
  • 36
1
Tried Everything , none Worked. The Only Thing which worked for me was to add a classname instead of custom class

In your js

swal({
       title : "More Data",
       className: "myClass"
     });

In Your Css

.myClass{
            width:600px;
            height:auto;
        }
Sheri
  • 1,383
  • 3
  • 10
  • 26
1

While most answers here work but wanted to add my two cents. (for: Sweetalert2)

We should NOT use a fix size say width: 500/600/800 because it may look ugly on smaller devices. I would rather suggest using the percentage so that no matter which device screen size you use it on, it will behave consistently.

Say we want to display a pop up which should not be more than 40% of the screen size then we can use it like below:

Swal.fire({
  title: 'Consistent width',
  width: '40%',
}) 

and if we use the same convention in all our pop-ups across the project, we will see consistent Sweet alerts.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
0

The class name for SweetAlert modal is sweet-alert. So, if you want to change its width, the correct CSS code is:

.sweet-alert { 
       width:  850px;   
}

This is working with SweetAlert 1.1.3

Lurai
  • 171
  • 2
  • 7
0

Also you can add to the HTML head the Style:

        .swal-modal {
        width: 1000px;
        }

This work for me. Hope this helps

0

swal({
  title: 'Title',
  type: 'question',
  width: '650px', /*set width container*/
  showCancelButton: true,
  showConfirmButton: true
 });
0

not the answer, but I hope i helps someone. in my case modal was appearing small due to my theming styles. so adding this fixed mine to default size.

.swal2-popup {
  font-size: 1.6rem !important;
}
Ali Aref
  • 1,893
  • 12
  • 31
0

None of this worked for me. "sweetalert2": "^11.2.0"

  1. Tried custom class
  2. direct width property (worked but needed maxwidth that wasn't available)
  3. Also classname doesn't works for me

Finally had to use the dev tools, inspect the elements and provide necessary changes to the classes. Here are some changes for the recent alert box.

.swal2-popup {
  max-height: 300px !important;
  max-width: 300px !important;
}
.swal2-icon {
  font-size: 13px !important;
}

.swal2-text {
  max-height: 70px;
}

.swal2-confirm {
  background: #3aa9a9 !important;
}

Open in new tab if it doesn't works

blablabla
  • 39
  • 1
  • 7