Search code examples
htmlcsssassionic2ionic3

Render content of ion-content to the centre of screen in IONIC 2/3


I have created a login page with username and password inputs followed by button and hyperlink. I want to place entire ion-content to centre of screen both horizantally and vertically.

ion-content {
  position: absolute;
  top: 50%;
  left: 0%;
  right: 50%;
  margin: 0 auto;
  margin-top: 0;
  margin-left: 0;
}

// .div1 {
//     position: absolute;
//     background-color: green;
//     top: 0;
//     left: 0;
//     right: 0;
//     bottom: 0;
//     display: block;
//     width: 100%;
//     height: 100%; // contain: layout size style;
// }
// // .Absolute-Center {
// //   margin: auto;
// //   position: absolute;
// //   top: 0; left: 0; bottom: 0; right: 0;
// // }
// div {
//     position: absolute;
//     top: 0;
//     bottom: 0;
//     left: 0;
//     right: 0;
//     margin: auto;
//     width: 100%;
//     height: 100%;
//     background-color: transparent;
// }
<ion-header>

  <ion-navbar>
    <ion-title>login</ion-title>
  </ion-navbar>

</ion-header>


<ion-content>


  <ion-item>
    <ion-label floating>User Name</ion-label>
    <ion-input [(ngModel)]="user.username" type="text" style="background-color:transparent"></ion-input>
  </ion-item>
  <ion-item>
    <ion-label floating>Password</ion-label>
    <ion-input [(ngModel)]="user.password" type="password"></ion-input>
  </ion-item>
  <button padding (click)="homePage()" color=secondary ion-button full round>Login</button>
  <a (click)="registerPage()">New ? Register here</a>


</ion-content>

Can you suggest some other ways to achieve it. the output should be very responsive.


Solution

  • You could add this style rule in the app.scss file:

    .vertical-center {
        .fixed-content,
        .scroll-content {
            display: flex;
            align-items: center;
    
            ion-list {
                max-width: 300px; 
                width:100%; 
                margin: auto; 
                text-align: center;
            }
        }
    }
    

    And then wrap the items inside of an ion-list like this:

    <ion-header>
        <ion-navbar>
            <ion-title>login</ion-title>
        </ion-navbar>
    </ion-header>
    <ion-content padding class="vertical-center">
        <ion-list>
            <ion-item no-padding>
                <ion-label floating>User Name</ion-label>
                <ion-input [(ngModel)]="user.username" type="text" style="background-color:transparent"></ion-input>
            </ion-item>
            <ion-item no-padding>
                <ion-label floating>Password</ion-label>
                <ion-input [(ngModel)]="user.password" type="password"></ion-input>
            </ion-item>
    
            <button padding (click)="homePage()" color=secondary ion-button full round>Login</button>
    
            <a (click)="registerPage()">New ? Register here</a>
        </ion-list>
    </ion-content>
    

    Please take a look at this working plunker