html - 在所需表格标签的冒号前添加星号

标签 html css

我正在尝试制作一个包含一些必填字段的表单,我想在必填字段的标签上添加一个星号。我遇到的问题是我找不到办法让星号出现在冒号之前,而不是之后。它需要看起来像这样: How the label should look

但现在它看起来像这样:

How the label looks now

这是我的 html:

<li class="form-row">
  <label class="required" for="first-name">First Name:</label>
  <input type="text" placeholder="John" required/>
</li> 

以及我最初尝试解决此问题的 CSS:

.required:after {
   content: "*"
 }

最佳答案

假设您可以修改 HTML,您只需插入一个 <span>标记在冒号周围,然后使用目标 .required span:before :

.required span:before {
  content: "*"
}
<li class="form-row">
  <label class="required" for="first-name">First Name<span>:</span></label>
  <input type="text" placeholder="John" required/>
</li>

关于html - 在所需表格标签的冒号前添加星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56744589/

相关文章:

html - <link> 标签中是否需要 ="text/css"类型?

javascript - 隐藏的 Div 在 Javascript 与其他 Div 切换之前占用空间

javascript - Google Chrome 中大于 16777216 像素的内容未呈现

php - 名字字段被插入到电子邮件中两次

html - 导航栏和侧边栏同时使用 Bootstrap ?

html - 如何使我的 SVG 图像适合父包装 div?

html - 仅使用一个 div 仅倾斜一侧的 div 边框

html - 将默认类应用于所有类型的 *n*

html - CSS ':checked' 不工作

javascript - 我的表格内的绝对位置 Div 超出了表格和包装 div