简体   繁体   English

使用CSS将'*'添加到具有'required'属性的输入字段标签中

[英]Add '*' to the label of input fields with the 'required' attribute with CSS

I am wondering if there is a way using only CSS to add an asterisk to a label element when the label's for attribute is for an input element with a required html attribute. 我想知道是否有一种方法,当标签的for属性用于具有required html属性的input元素时,是否仅使用CSS向label元素添加星号。 Alternatively, we could use logic around if the label is directly followed by an input element which has the required attribute. 另外,如果label后面紧跟具有required属性的input元素,则可以使用逻辑。

What does work is something like this: 起作用的是这样的:

 input[required] + label:after { content: '*'; color: red; } 
 <form> <div> <input for="name" type="text" required /> <label id="name">Name</label> </div> <div> <input for="age" type="text" /> <label id="age">Age</label> </div> </form> 

But what I'm talking about is if the label and input are swapped around like the example below, which is more common. 但是我要说的是labelinput是否像下面的示例那样交换,这是更常见的情况。 The CSS sibling selector + doesn't work for this case. CSS兄弟选择器+在这种情况下不起作用。 Is there any way to do some like above when the label element is first and the input is second? label元素为第一个而input为第二个时,有什么方法可以执行上述操作?

 /* ??? */ 
 <form> <div> <label for="name">Name</label> <input id="name" type="text" required /> </div> <div> <label for="age">Age</label> <input id="age" type="text" /> </div> </form> 

Thanks 谢谢

I suggest you keep old html and use css flex to order position 我建议您保留旧的html并使用CSS flex来订购位置

 input[required] + label:after { content: '*'; color: red; } div { display: flex; text-align: center; align-items: flex-start; } div input { width: 100px; height: 20px; order: 2; } div label { width: 100px; height: 20px; order: 1; } 
 <form> <div> <input for="name" type="text" required /> <label id="name">Name</label> </div> <div> <input for="age" type="text" /> <label id="age">Age</label> </div> </form> 

See the codepen 代码笔

You can use float for label to positining label at left like below: 您可以使用float来将标签放置在左侧的标签上,如下所示:

 label { float: left; } input[required] + label:after { content: '*'; color: red; } } 
 <form> <div> <input for="name" type="text" required /> <label id="name">Name</label> </div> <div> <input for="age" type="text" /> <label id="age">Age</label> </div> </form> 

There is no way for CSS to "render backwards" the way you want it too (not without SASS/SCSS). CSS也无法以您想要的方式“向后渲染”(并非没有SASS / SCSS)。

One solution would be to add a "required" class to the labels that correspond to required fields. 一种解决方案是将“必需”类添加到与必填字段相对应的标签中。

<style>
    label.required:after { content: '*';color:red; }
</style>

<form>
  <div>
    <label for="name" class="required">Name</label>
    <input id="name" type="text" required />
  </div>
  <div>
    <label for="age">Age</label>
    <input id="age" type="text" />
  </div>
</form>

Another option is the use of CSS3 Orders property, but the parent element would need to be set to "display:flex". 另一个选择是使用CSS3 Orders属性,但是父元素需要设置为“ display:flex”。

<style>
    div { display:-webkit-flex;display:flex; }
    div label { order:1; }
    div input { order:2; }
    div input[required] + label:after { content: '*';color:red; }
</style>

<form>
  <div>
    <input id="name" type="text" required />
    <label for="name">Name</label>
  </div>
  <div>
    <input id="age" type="text" />
    <label for="age">Age</label>
  </div>
</form>

EDIT 6/25/2019 SASS/SCSS Example EDIT 6/25/2019 SASS / SCSS示例

div {
    display:-webkit-flex;
    display:flex;
    label {
        order:1;
    }
    input {
        order:2;
        &[required] + label:after {
            content: '*';color:red;
        }
    }
}

More information about Parent Selectors in SCSS. 有关SCSS中的“父选择器”的更多信息。

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

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