简体   繁体   中英

How to align an ion-card in middle of the screen (vertically and horizontally) for iOS and Android?

How to align an ion-card in middle of the screen (vertically and horizontally)?

Debugging on iOS simulator displays the card but with more margin on the left side than the right. So not really centred. To center correctly i had to left: 47.5% ; instead of left: 50%

  • simple ion-card

     <ion-card class="error"> <ion-card-header class="error-header"> <ion-icon name="error"></ion-icon> Error </ion-card-header> <ion-card-content class="error-body"> Error occurred! </ion-card-content> </ion-card> 
  • styling in the sass file

     .error { transform: translateX(-50%) translateY(-50%); top: 50%; left: 50%; position: absolute; .error-header { position: relative; text-align: center; top: 36%; font-size: 3.0em; width: 100%; font-weight: bold; color: red; } .error-body { font-size: 1.0em; text-align: center; position: relative; top: 52%; width: 100%; } } 

IMO check your css it should be

    .error {

 transform: translateX(-50%) translateY(-50%);
    top: 50%;
    left: 50%;
    position: absolute;
     } // close here

.error-header {
  position: relative;
  text-align: center;
  top: 36%;
  font-size: 3.0em;
  width: 100%;
  font-weight: bold;
  color: red;
}

.error-body {
  font-size: 1.0em;
  text-align: center;
  position: relative;
  top: 52%;
   width: 100%;
}

// not here

also if don't know size of card then make position fixed like try

   .error {

 transform: translateX(-50%) translateY(-50%);
    top: 50%;
    left: 50%;
    position: fixed;
     }

if you set fixed position then try

 .error {

 transform: translateX(-50%) translateY(-50%);
    top: 50%;
    left: 50%;
    position: absolute;
    margin-top: -50px;
    margin-left: -50px;
    width: 100px;
    height: 100px;
     }

OR

better you may go through this

.error { width: 50%; height: auto; margin: auto; }

您需要在一行中添加XY坐标

transform: translate(-50%, -50%);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM