如何使用CSS在占位符中获取星号

Kir*_*ash 9 html css

我想在输入占位符中添加星号标记。像这样的东西: 在此输入图像描述

我已经搜索过互联网但找不到有效的解决方案。

我目前的做法:

目前我正在尝试将其添加到 after 伪元素中,但没有出现。

    input[type=text]::-webkit-input-placeholder:after {
       content: '*';
       color: red; vertical-align: top; font-size: 10px;
    }
    
    input[type=text]:-moz-placeholder:after { /* Firefox 18- */
       content: '*'; 
       color: red; vertical-align: top; font-size: 10px;
    }
    
    input[type=text]::-moz-placeholder:after {  /* Firefox 19+ */
       content: '*';
       color: red; vertical-align: top; font-size: 10px;
    }
    
    input[type=text]:-ms-input-placeholder:after {  
       content: '*';
       color: red; vertical-align: top; font-size: 10px;
    } 
Run Code Online (Sandbox Code Playgroud)
<input type="text" name="your-name" placeholder="Naam">
Run Code Online (Sandbox Code Playgroud)

我不想直接在占位符中添加符号。但会喜欢它以任何其他方式让我以不同的方式设计符号。(假设我想要我的符号为蓝色,但其余文本为灰色)。

所以,如果有人可以帮助我在占位符中添加一个星号。

JSFIDDLE

Bho*_*yar 7

为什么不在占位符属性本身中简单地使用 * 呢?

.asterisk::-webkit-input-placeholder {
    color:    #f00;
}
.asterisk:-moz-placeholder {
   color:    #f00;
   opacity:  1;
}
.asterisk::-moz-placeholder {
   color:    #f00;
   opacity:  1;
}
.asterisk:-ms-input-placeholder {
   color:    #f00;
}
Run Code Online (Sandbox Code Playgroud)
<input type="text" name="your_name" placeholder="*" class="asterisk" />
Run Code Online (Sandbox Code Playgroud)

EndNote:不能在替换的 html 元素中使用伪元素


根据您的评论,您还可以使用 required 属性,然后像这样设置它们的样式:

<input type="text" name="your_name" placeholder="*" required />
[required]::-webkit-input-placeholder {
    color:    #f00;
}
Run Code Online (Sandbox Code Playgroud)

根据您的下一个评论要求,您需要将需要星号的输入包装在如下所示的范围内:

<input type="text" name="your_name" placeholder="*" required />
[required]::-webkit-input-placeholder {
    color:    #f00;
}
Run Code Online (Sandbox Code Playgroud)
.input-group{
  position: relative;
}
.input-group::after{
  content: '*';
  position: absolute;
  top: 3px;
  left: 46px;
  color: #f00
}
Run Code Online (Sandbox Code Playgroud)

  • 哦对不起。你误会我了。看我的问题。在我的占位符中,我有“Naam”,但它应该显示“Naam *”,并带有红色星号。 (3认同)
  • 但这将为整个占位符添加颜色。我想要的是:只有占位符中的星号符号呈灰色,占位符中的其他文本呈红色。 (2认同)