Centralize div content in Alertcontroller

Asked

Viewed 459 times

1

I have the following code below

 let alert = this.alertCtrl.create({
  message: '<div><img height="50" src="assets/imgs/success.png"></div><div>' +
  '<p>Cadastrado com sucesso!!!</p>' +
  '</div>',
   buttons: [{
    text: 'OK',
    handler: data => {
      console.log('Ok clicked');
    }
  }]
});
alert.present();

Where I’m using importing the AlertController in this way:

import { AlertController } from 'ionic-angular';

See the image as it looks:

inserir a descrição da imagem aqui

I would like to centralise the content of AlertController, but I tried in some ways but it doesn’t work, like for example in this way, but does not recognize.

Is there any way to centralize the content of div in the AlertController?

  • How exactly would you like it to look? You have 2 Divs elements 1 img element and a p element, I didn’t understand which exactly you want to align in the center, is horizontal vertical center or both?

  • @Magichat align everything. By the way, except the OK button. Or maybe also. huehe

  • I don’t know how you try : <div style="width:70%;float:None;display:block;margin:0 auto;text-align:center;"><img height="50" src="Assets/imgs/Success.png">' + '<p>Registered successfully!!! </p></div>'...

  • @Magichat had already tried so, but it doesn’t work.

1 answer

1


You need to define a css class for your Alertcontroller:

Example:

 let alert = this.alertCtrl.create({
      message: '<div><img height="80" src="http://www.catster.com/wp-content/uploads/2017/08/A-fluffy-cat-looking-funny-surprised-or-concerned.jpg"></div><div>' +
      '<p>Cadastrado com sucesso!!!</p>' +
      '</div>',
      buttons: ['Dismiss'],
      cssClass: 'custom-alert',
    });
    alert.present();
 }

Notice that in creating the alert I have an attribute cssClass where I pass a css class. css would look like this:

page-home {}

.custom-alert .alert-message{
    text-align: center;
    overflow: hidden;
}

OBS:. Note that the css are not inside the page stylization, that is, it has to be separated because the alert component is not rendered inside your page:

inserir a descrição da imagem aqui

In this example I used a text-align: center to align the components, but nothing prevents you from customizing the css the way you prefer.

Upshot:

inserir a descrição da imagem aqui

  • hahaha... I was putting the class inside the page-home! That’s why it wasn’t working. You can supplement your answer by talking about why you should stay out of page-home css? Worked right here. = D

  • @acklay opa, can yes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.