Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic 2 vertical alignment using ion-grid

Tags:

flexbox

ionic2

I have 2 buttons in a screen in Ionic 2, and I want to align them both together (one on top of the other, but together) in the middle of the screen (horizontal and vertical alignment).

I want to use ion-grid, no paddings, margins, floats or percentages.

So I have this

<ion-content>
  <ion-grid>
    <ion-row>
      <ion-col text-center>
        <button>button 1</button>
      </ion-col>
    </ion-row>
    <ion-row>
      <ion-col text-center>
        <button>button 2</button>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

With <ion-col text-center> I can align them to the center horizontally, but for vertical alignment I can´t see anything that I can apply to , so I tried this:

ion-grid {
  justify-content: center;
}

But nothing happened. I checked and this is being applied to the page, but for some reason it doesn´t work. Any ideas?

like image 924
David Avatar asked Feb 06 '17 13:02

David


People also ask

How do you vertically align an element?

For example, if you're trying to align something horizontally OR vertically, it's not that difficult. You can just set text-align to center for an inline element, and margin: 0 auto would do it for a block-level element.

How do you use an ion grid?

Working with the Ionic Grid System is straightforward. There are two main classes – row for working with rows and col for columns. You can choose as many columns or rows you want. All of them will adjust its size to accommodate the available space, although you can change this behavior to suit your needs.

How do you make an ion grid responsive?

Create Responsive Formhtml` page and add the following code inside the ion-content tag. We created a simple form in Ionic app which will adjust automatically on various devices. In the small device, every column will occupy full width, and it will be converted into a 2 column layout in the horizontal view.

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.


2 Answers

Use this:

ion-grid {
    height: 100%;
    justify-content: center;
}
like image 134
amin arghavani Avatar answered Sep 22 '22 03:09

amin arghavani


You can use the code below in a column (e.g.: flex-col) inside ion-grid to align centered vertically and horizontally:

.flex-col {
  display: flex;
  justify-content: center;
  align-items: center;
}
like image 24
Matheus Abreu Avatar answered Sep 26 '22 03:09

Matheus Abreu