Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct method to center elements in ionic2

I have placed some text and a button on page. I am currenlty centering it using traditional css methods that i know of. Is this the correct way to center in IONIC 2?

<ion-content padding class="login-signup">

  <ion-card>

  <div class="or-label">
   SOME-TEXT
  </div>

  <div class="signup-button">
    <button outline>Signup</button>
  </div>

</ion-content>

// corresponding css

 .or-label {
    width: 20%;
    font-size: 16px;
    margin: 0 auto;
    margin-top: 2em;
    margin-bottom: 2em;
    height: 50px;
    text-align: center;
    color: red;
  }


.signup-button {
  text-align:center;
}
like image 322
runtimeZero Avatar asked Jun 27 '16 23:06

runtimeZero


People also ask

How do you center items in ionic?

You have to use ion-justify-content-center on a flexbox node. So either you put display: flex on your wrapper div node, or you just use ion-justify-content-center on <ion-row> like so. Save this answer.

How do you align elements to the center?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I center an ionic label?

The new recommended way is to apply this using class="ion-text-center" .

What is the best way to center a div?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.


1 Answers

Ionic 2 has some useful Utility Attributes that can be added to elements.

In this case adding text-center to an element will apply the text-align: center property to it, causing its inner content to be centered.

An example using your code would be something like...

<ion-card text-center>
    <div class="or-label">
        SOME-TEXT
    </div>
    <button outline>Signup</button>
</ion-card> 
like image 107
Will.Harris Avatar answered Oct 02 '22 18:10

Will.Harris