How do I change text color in SweetAlert2?
Asked Answered
B

3

7

I want to change the 'title' color in SweetAlert2. How can I do that? Thank you in advance

function CustomConfirm(title, message, type) {
    return new Promise((resolve) => {
        Swal.fire({
            title: title,
            text: message,
            icon: type,
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#6e7d88',
            confirmButtonText: 'Yes',
            cancelButtonText: "No"
        }).then((result) => {
            if (result.isConfirmed) {
                resolve(true);
            } else {
                resolve(false);
            }
        });
    });
}
Baran answered 3/8, 2021 at 15:44 Comment(0)
B
10

Here is my final function:

        Swal.fire({
        title: "<h5 style='color:red'>" + title + "</h5>",
        text: message,
        icon: type,
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#6e7d88',
        confirmButtonText: 'Yes',
        cancelButtonText: "No"
    })
Baran answered 3/8, 2021 at 16:13 Comment(0)
D
3

You can use customClass or showClass to get the CSS class for the popup and change the colors in a CSS file from there.

For example:

function CustomConfirm(title, message, type) {
    return new Promise((resolve) => {
        Swal.fire({
            customClass : {
              title: 'swal2-title'
            }
            title: title,
            text: message,
            ...

Then in your CSS

.swal2-title {
  color: red;
}
Decastere answered 5/9, 2021 at 4:47 Comment(0)
M
0

You can try add this code in a global CSS:

Swal.fire({
  title: 'Testing Custom Styles',
  html: 'Good Job',
  icon: 'success'
})
.swal2-container.swal2-center>.swal2-popup{
  background-color: #121212 !important;
}
.swal2-html-container, .swal2-title{
  color: white !important;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Momism answered 29/3, 2022 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.