iOS safari input caret color

2020-08-11 07:39发布

问题:

I have a small CSS issue with Safari on iPhone device. My search input is blue and when user focuses on it, caret is almost invisible:

In all desktop browsers, it has right color (white), even on desktop Safari. Any idea how to fix this and change caret color on iOS device?

Input styles:

input {
  background-color: $stateBlue;
  height: $navbarItemHeight;
  padding: 0 10px;
  vertical-align: bottom;
  color: $white;
  border-radius: $borderRadius;
  font-size: 1.1666666667em; // 16px
  -moz-appearance:none;
  -webkit-appearance:none;

  &::-webkit-input-placeholder {
    text-shadow: none;
    -webkit-text-fill-color: initial;
  }
}

回答1:

There is a W3C specification for this called caret-color which can be used like this:

input[type="text"].custom {
  caret-color: red;
}
<div>
  <label>default caret:</label>
  <input type="text"/>
</div>
<div>
  <label>custom red caret:</label>
  <input type="text" class="custom"/>
</div>

It's currently supported by Chrome (also on Android), Firefox, Opera and Safari (including iOS).



标签: ios css safari