【问题标题】:Render content of ion-content to the centre of screen in IONIC 2/3在 IONIC 2/3 中将 ion-content 的内容渲染到屏幕中心
【发布时间】:2017-12-16 15:04:16
【问题描述】:

我创建了一个登录页面,其中包含用户名和密码输入,然后是按钮和超链接。 我想将整个离子含量水平和垂直放置在屏幕中心。

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>

您能否建议一些其他方法来实现它。输出应该非常灵敏。

【问题讨论】:

    标签: html css sass ionic2 ionic3


    【解决方案1】:

    您可以使用flexbox 将内容垂直和水平居中。这可能会帮助您入门:

    ion-content {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 100vh;
    }
    <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>

    【讨论】:

    • 您的建议不起作用,它一直停留在屏幕顶部
    • 尝试给身体height: 100%
    • .ion-content 需要 100% 的高度。在我的回答中,这是使用100vh 实现的,它确实有效。我不知道为什么它在你的情况下不起作用。您也可以尝试将height: 100% 添加到html
    • 尝试将height: 100vh 添加到body
    【解决方案2】:

    您可以在app.scss 文件中添加此样式规则:

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

    然后将这些项目包装在 ion-list 中,如下所示:

    <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>
    

    请查看this working plunker

    【讨论】:

    • 它在屏幕中心水平对齐所有内容。Flex-direction 在我的场景中应该是列类型
    • 哦,对了,就我而言,我将所有内容都包含在一个表单中。我已经用你的评论更新了答案。
    • 它与屏幕顶部对齐.. 不居中。
    • 你能推荐一些我可以掌握 flexbox 实现的链接吗?提前致谢
    • 当然...我曾经向css-tricks tutorials 学习 css 相关的东西。 MDN 也可能是一个很好的起点:)
    【解决方案3】:

    根据您的浏览器支持要求,您可以使用 CSS3 'transform' 属性,结合绝对定位,将 div 放置在页面中间。

    首先,使用 position:absolute 将 div 放置在页面顶部和左侧的 50% 处:

    position:absolute;
    top:50%;
    left:50%;
    

    https://jsfiddle.net/bfdqL5um/1/
    (红色边框纯粹是为了说明盒子)

    这会将 div 的左上角置于页面的中心。然后,您可以使用 transform/translate 将其向后移动 50% 的高度和宽度:

    transform:translate(50%,50%);
    

    框现在完全居中。
    https://jsfiddle.net/bfdqL5um/2/

    IE9+ 和所有其他主流浏览器都支持此功能。

    请注意,使用绝对定位时,您必须注意任何父元素的位置。 (即,如果定位了图标内容父项之一,它可能会影响如何应用该父项的绝对定位)。

    (如果格式或礼仪不好,请先在 StackOverflow 上发帖道歉!)

    【讨论】:

    • 我只使用了 -50%,但这是第一个能够为我工作的东西
    【解决方案4】:

    这对我有用:

    <ion-grid  class="center-grid">
    </ion-grid>
        ion-grid {
            &.center-grid {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%) !important;
                display: block !important;
                /*width: 100%;*/
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 2017-11-29
      • 2019-04-23
      • 2016-08-24
      • 1970-01-01
      • 2015-06-15
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多