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
Vertical Vim Cursor in Command Mode
How to Scroll a Div Content Between Another 2 Flex Divs
Loops Make Less to Run Out of Memory. (Structure Optimization)
How to Select from Only One Table with Web::Scraper
A Styled Ordered List Whose Nested List Should Have Numbers with Letters Using CSS Counter Property
Cannot Get CSS to Work in Itextsharp (5.4.3) When Making Pdf
Multiple Navbars on The Same Page with Twitter's Bootstrap 3
CSS Ie8 Can't Style a List Element
Can't Load CSS File in Javafx 8
Center Div in Parent with Only Min-Height, and Child May Without Height & with Relative Position