Adding Asterisk to Required Fields in Bootstrap 3

Adding asterisk to required fields in Bootstrap 3

Use .form-group.required without the space.

.form-group.required .control-label:after {
content:"*";
color:red;
}

Edit:

For the checkbox you can use the pseudo class :not(). You add the required * after each label unless it is a checkbox

.form-group.required:not(.checkbox) .control-label:after, 
.form-group.required .text:after { /* change .text in whatever class of the text after the checkbox has */
content:"*";
color:red;
}

Note: not tested

You should use the .text class or target it otherwise probably, try this html:

<div class="form-group required">
<label class="col-md-2 control-label"> </label>
<div class="col-md-4">
<div class="checkbox">
<label class='text'> <!-- use this class -->
<input class="" id="id_tos" name="tos" required="required" type="checkbox" /> I have read and agree to the Terms of Service
</label>
</div>
</div>
</div>

Ok third edit:

CSS back to what is was

.form-group.required .control-label:after { 
content:"*";
color:red;
}

HTML:

<div class="form-group required">
<label class="col-md-2"> </label> <!-- remove class control-label -->
<div class="col-md-4">
<div class="checkbox">
<label class='control-label'> <!-- use this class as the red * will be after control-label -->
<input class="" id="id_tos" name="tos" required="required" type="checkbox" /> I have read and agree to the Terms of Service
</label>
</div>
</div>
</div>

Bootstrap 3 - Add an asterisk to the input on the same row

Demo Fiddle

Try using:

.form-group div{
position:relative;
margin-right:15px;
}
.form-group div:after{
position:absolute;
content:'*';
color:red;
right:-10px;
top:0;
}

nb. You also hadnt closed one of your div tags in your posted code.

Adding a red asterisk to required fields

Rather than ::before use ::after and remove the float: right.

::before places a pseudoelement before the element you're selecting. ::after will place it after, so rather than putting the asterisk before the element you want and moving it with a float/position you can just place it after naturally.

The reason the asterisk was moved too far to the right is because floating moves the element to the right side of the parent container (not always the parent element, let me know if you want me to elaborate). So by floating right you were telling it to move to the far right of the label container which means just to the left of the text boxes, and not to the right of the label text.

https://jsfiddle.net/h4depmdf/1/

.required-field::after {
content: "*";
color: red;
}

Use CSS to automatically add 'required field' asterisk to form inputs

Is that what you had in mind?

http://jsfiddle.net/erqrN/1/

<label class="required">Name:</label>
<input type="text">

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

.required:after {  content:" *";  color: red;}
<label class="required">Name:</label><input type="text">

Add an asterisk to a label if field is required

I don't really know Angular…

But I would say you can do something like this, using pseudo-element ::after:

label {  border: 1px solid gray;  padding: 4px 8px;}
label.asterisk-if-mandatory::after { content: " *";}
<label class="asterisk-if-mandatory">Item is mandatory</label><label class="">Item is not mandatory</label><label class="asterisk-if-mandatory">Item is mandatory</label>


Related Topics



Leave a reply



Submit