Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 center ion grid within ion-content

Tags:

flexbox

ionic4

I simply have an ion-content within that an ion-grid within that an h1.

<ion-content no-padding no-bounce>
    <ion-grid no-padding>
        <h1>ion-grid</h1>
    </ion-grid>
</ion-content>

I have my ion-content set to 100vh, and the ion grid set to 80%; height. How do I vertically center the ion grid within the ion-content? And vertically center the h1 within the ion grid?

I tried setting the ion-content to display flex (which I'm sure it already is) and also align-content center. Same with the ion-grid.

Here is my CSS:

ion-content {
    display: flex;
    height: 100vh;
    width: 100vw;
    align-content: center;

    ion-grid {
        display: flex;
        height: 80%;
        width: 100%;
        background-color: blue;
        align-content: center;
    }
}

Heres the result (the ion-grid is stuck to the top of ion-content rather than vertically centered so align-content: center does not work. Same with ion grid and the h1):

enter image description here

like image 370
user10181542 Avatar asked Nov 25 '18 22:11

user10181542


People also ask

How do you center content 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.

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.

When using ion Col inside an ion grid we specify widths using directive Col n where n is a number what is the maximum value of N?

Rows are created a horizontal structure with various numbers of columns, a maximum of 12 columns can be fitted in the row to occupy the full width. Columns: To define a column or a cell use the ion-col tag, Columns are declared in the row to acquire the full available space inside the row.


1 Answers

With the below 3 lines we can add centered text.

<ion-content padding>   
  <ion-grid style="height: 100%">
    <ion-row justify-content-center align-items-center style="height: 100%">
      <h1>ion-grid</h1> 
    </ion-row> 
  </ion-grid>
</ion-content>

Here is the link for sample

like image 183
Jackson mj Avatar answered Oct 06 '22 15:10

Jackson mj