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;
}
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.
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.
The new recommended way is to apply this using class="ion-text-center" .
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With