Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center <ion-item-divider> text in Ionic 4

How can text be horizontally centered within a <ion-item-divider> element in Ionic 4?

I can't find anything in the Ionic 4 docs on centering html component text. In the Ionic 3 docs, I found the text-center attribute utility as shown below and documented here, but it doesn't work.

<ion-content padding>
  <ion-list>
    <ion-item>
      Hello, I am left aligned
    </ion-item>
    <ion-item-divider text-center>
      PLEASE CENTER ME!
    </ion-item-divider>
  </ion-list>
</ion-content>

I also tried adding style="text-align: center;" to the <ion-item-divider> element, but that did nothing as well.

like image 308
Lightbeard Avatar asked Jul 29 '18 15:07

Lightbeard


People also ask

Is there a text-center attribute in ionic 3?

In the Ionic 3 docs, I found the text-center attribute utility as shown below and documented here, but it doesn't work. I also tried adding style="text-align: center;" to the <ion-item-divider> element, but that did nothing as well.

What is the use of textarea in ionic?

ion-textarea The textarea component is used for multi-line text input. Unlike the native textarea element, the Ionic textarea does not support loading its value from the inner content. The textarea component accepts the native textarea attributes in addition to the Ionic properties.

What does XC mean in Ionic elements?

They are designed to have multiple children, and center them on a single axis. xc is a visual metaphor for centering a single child right in the cross of an “x”. I figure “Ionic gonna Ionic” when it comes to top-level elements like <ion-header> or <ion-content>, so I just leave them be and work one level inside them.

What are item dividers in a list?

Item Dividers are block elements that can be used to separate items in a list. They are similar to list headers, but instead of being placed at the top of a list, they should go in between groups of items. <!-- Item Dividers in a List --> The color to use from your application's color palette.


3 Answers

I ended up doing this:

  <ion-item-divider>
    <div style="width: 100%; text-align: center;">PLEASE CENTER ME!!</div>
  </ion-item-divider>

ugly :(

like image 56
Lightbeard Avatar answered Nov 15 '22 13:11

Lightbeard


You can create a css class and call it center. I used the global variables.scss in the directory theme:

.center {
    text-align: center;
}

then you can do something like this:

<ion-label>
    <p class="center">Some text</p>
</ion-label>

Please Don't use inline styles.. What's so bad about in-line CSS?

like image 40
Zonker Avatar answered Nov 15 '22 12:11

Zonker


In Ionic 4 it looks like you're supposed to set the text-center on the ion-label, try:

<ion-item-divider> <ion-label text-center>PLEASE CENTER ME!!</ion-label> </ion-item-divider>

like image 20
Volmar Avatar answered Nov 15 '22 14:11

Volmar