【问题标题】:How to center <ion-label> inside a <ion-list-header> in Ionic 4?如何在 Ionic 4 的 <ion-list-header> 中居中 <ion-label>?
【发布时间】:2019-12-24 11:11:46
【问题描述】:

&lt;ion-label&gt; 中的文本如何在 Ionic 4 的 &lt;ion-list-header&gt; 元素中水平居中?

我找到了如下所示的 text-center 属性实用程序,并记录在 ionic doc 中,但它不起作用。

我也尝试添加class="ion-center-text"

但是当&lt;ion-label&gt;&lt;ion-list-header&gt; 中时,这两种测试都不起作用。 但是,如果 &lt;ion-label&gt; 被包裹在 &lt;ion-item&gt; 中,这将很有效!

    <ion-list-header text-center>
      <ion-label>Text not centered</ion-label>
    </ion-list-header>

或:

    <ion-list-header>
      <ion-label text-center>Text still not centered</ion-label>
    </ion-list-header>

    <ion-list-header>
      <ion-label class="ion-text-center">Text still not centered</ion-label>
    </ion-list-header>

    <ion-list-header  class="ion-text-center">
      <ion-label>Text still not centered</ion-label>
    </ion-list-header>

我只需要一个简短而干净的解决方案来将我的 &lt;ion-label&gt; 集中在一个 &lt;ion-list-header&gt; 中。

有人有与 ionic 4 的“最佳实践”相匹配的解决方案吗?


在@annaya 的帮助下找到的解决方案是在display:block 上更改&lt;ion-list-header&gt; 的显示模式,或者更好地保留display:flex 并添加justify-content:center。 像这样:

    <ion-list-header>
      <ion-label>Text centered</ion-label>
    </ion-list-header>

与:

    list-item-header{
        justify-content: center;
    }

感谢回复、cmets 和英文更正;)

【问题讨论】:

    标签: ionic-framework label ionic4


    【解决方案1】:

    ion-list-header{
         display:block;
         text-align: center;
    }
    <ion-list-header>
          <ion-label>Text not centered</ion-label>
    </ion-list-header>

    【讨论】:

    • 如果你愿意,你也可以添加类名
    • 非常感谢@annaya!你让我朝着好的方向前进。但是经过一些测试后,我更喜欢保留 display:flex 并添加 justify-content: center 来做同样的结果。带显示:块;我失去了文本的垂直中心。谢谢。
    • 很高兴帮助你@cesar jacquet
    【解决方案2】:

    如果你想在 ion-list-header 中居中,可以将ion-label 的左右边距设置为 auto,例如

    ion-label {
        margin-left: 0px;
        margin-right: 0px;
    }
    

    另外,ion-list-header 带有一些默认填充,请确保将其设置为零。

    ion-list-header {
        padding-left: 0px;
    }
    

    【讨论】:

    • 是的,这也有效!谢谢@Patwan。但我更喜欢编辑后的解决方案。
    【解决方案3】:

    我发现 ion-item 和 ion-label 有相当固执的行为; 当它们不适合您的应用程序时,退回到 div、h1 等是没有羞耻的; 如果你只是在你的 ion-list-header 中放置一个 h1 或 h2 ,它应该可以用最少的样式来做你想做的事情。

    【讨论】:

    • 你是对的,但我尽量与离子命题保持密切联系并使用他们的工具。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 2018-09-25
    • 2021-01-08
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多