15

I'm trying to style the placeholder of my Ionic 4 application.The HTML looks as follows:

<form>
  <ion-grid>
    <ion-row class='label'>Name</ion-row>
    <ion-row>
      <ion-item>
        <ion-input type='text' [(ngModel)]='recipe.name' name='name' placeholder='Name'></ion-input>
      </ion-item>
    </ion-row>
    <ion-row class='label'>Weight</ion-row>
    <ion-row>
      <ion-item>
        <ion-input type='number' [(ngModel)]='recipe.weight' name='weight' placeholder='Weight'></ion-input>
        <ion-label>kg</ion-label>
      </ion-item>
    </ion-row>
  </ion-grid>
</form>

If tried out Ionic 2.x solutions yet it did not work out.

I've figured out that if you set a color in ion-item it styles the entire text of the input field

ion-item {
    ion-input{
        color:red;
    }
}

when using the pseudo class :placeholder-shown or the pseudo element ::placeholder on ion-input though the styling shows no effect.

What am I doing wrong? Is there even a possibility in Ionic 4 to style input placeholder ?

Edit:

Stackblitz to fork with Ionic 4 and Angular 6

4

3 回答 3

19

使用此样式代码:

ion-input{
    --placeholder-color: red;
    --placeholder-opacity: 1;
}

见这里:https ://stackblitz.com/edit/angular6-with-ionic4-list-refresh-test-yq3ntj?file=src%2Fapp%2Fapp.component.html

于 2018-08-21T11:05:08.497 回答
1

某些组件,例如 datetime,使用您自己的 html 结构。所以我应用了更多标签来更改所有输入。

ion-datetime {
  --placeholder-color: rgba(211, 211, 211, 0.4);
}

ion-input, ion-textarea, ion-select {
  --placeholder-color: rgba(211, 211, 211, 1);
}

我不明白为什么我使用不同的 alpha 值......但它有效!

在此处输入图像描述

于 2020-10-29T21:26:59.237 回答
1

Please try this general styling to see if it has any effect:

::-webkit-input-placeholder { /* Chrome/Opera/Safari */
  color: red;
}
::-moz-placeholder { /* Firefox 19+ */
  color: red;
}
:-ms-input-placeholder { /* IE 10+ */
  color: red;
}
:-moz-placeholder { /* Firefox 18- */
  color: red;
}
于 2018-08-21T08:15:01.423 回答