简体   繁体   English

如何更改 Select 标签中占位符选项的颜色?

[英]How to change the color of a placeholder option in a Select tag?

I am trying to change the color of the first option (Business Type) in a select tag to gray so it looks like placeholder text for a text field.我正在尝试将选择标签中第一个选项(业务类型)的颜色更改为灰色,使其看起来像文本字段的占位符文本。 I tried several things that did not work.我尝试了几件不起作用的事情。 Is there any way this could be done simply with HTML and CSS?有什么办法可以简单地用 HTML 和 CSS 来完成?

    <select id="businesstype">
        <option value="" disabled selected hidden>Business Type</option>
        <option value="">Restaurant</option>
        <option value="">Hotel</option>
        <option value="">Café</option>
        <option value="">Shop</option>
        <option value="">Services Agency</option>
        <option value="">NGO</option>
        <option value="">Institution</option>
    </select>

This should work:这应该有效:

#businesstype div[disabled] {
    opacity: 0.5;
}

Also check out: https://www.w3schools.com/css/css_attribute_selectors.asp另请查看: https : //www.w3schools.com/css/css_attribute_selectors.asp

Target select to change the selected color style.目标选择以更改selected颜色样式。
Then, target all options from this select to reset their inherited color.然后,定位此选择中的所有选项以重置其继承的颜色。

 #businesstype { color: gray; } #businesstype option { color: black; }
 <select id="businesstype"> <option value="" disabled selected hidden>Business Type</option> <option value="">Restaurant</option> <option value="">Hotel</option> <option value="">Café</option> <option value="">Shop</option> <option value="">Services Agency</option> <option value="">NGO</option> <option value="">Institution</option> </select>

option:disabled {
    color: #000;
    font-weight: bold;
}

Add required in select tag and do like在选择标签中添加 required 并喜欢

  <select class="form-control form-item__element--select" required>
                      <option  disabled value="">Business Type*</option>
                       <option value="">Restaurant</option>
                       <option value="">Hotel</option>
                       <option value="">Café</option>

In CSS在 CSS 中

.form-item__element--select [disabled] {
  color: #DEDEDE;
  font-weight: 600;
  font-size: 16px;
   }
.form-item__element--select:invalid {
   color: #DEDEDE;
   font-weight: 600;
   font-size: 16px;
  }

You can do it by just adding following css,你可以通过添加以下css来做到这一点,

#businesstype option[disabled]:first-child {
    color: gray;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM