Ionic - Center text vertically in item-list with i

2019-03-20 16:36发布

问题:

I try to add a Ion-List to my apllication with items build like |Image|Text|Button

The Image and the Button get centerd vertically, but the text does not.

I tried some CCS found on the Internet, which works fine in Browser preview but not on a real device (Samsung Galaxy S3 Mini, Android 4.1.2)

style="position: absolute; top: 50%; transform: translateY(-50%);"

Code

    <ion-content>
       <ion-list>
       <ion-item class="item-avatar item-icon-right" collection-repeat="x in y" href="#/pages/{{x.id}}">
        <img src="img/{{x.icon}}.png">
        <div style="position: absolute;   top: 50%; transform: translateY(-50%);">{{x.name}}</div>
        <i class="icon ion-chevron-right icon-accessory"></i>
        </ion-item>
      </ion-list>
   </ion-content>

On Device

回答1:

You can try to use this css:

.item-text-center
{
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -moz-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    -moz-align-items: center;
    align-items: center;
    position: absolute;
    top: 0;
    height: 100%;
}

and apply it to your <div>

<div class="item-text-center item-text-wrap">{{x.name}}</div>

and this is the whole content:

  <ion-content class="padding">
    <ion-list>
      <ion-item class="item-thumbnail-left item-icon-right" collection-repeat="x in y">
        <img src="img/{{x.icon}}.png">
        <div class="item-text-center item-text-wrap">{{x.name}}</div>
        <i class="icon ion-chevron-right icon-accessory"></i>
      </ion-item>
    </ion-list>
  </ion-content>

This ionic play might help you.



回答2:

line-height works for me

css:
.item-text-center-vertical {
  line-height: 300%;
}

Inside avatar:
<h2 class="item-text-center-vertical">{{value}}</h>