CSS: Input checkbox and radio button appearance - html

I am trying to fix checkbox with tickmarks and radio with periods.
toggling content:"" on unchecked state and content:"\2714" checked state I am getting a flicker. So I made color transparent. Is that correct?
The tick mark doesn't fit exactly to the box. How can alter only that tickmark?
If the sentence is split into lines, the square and text fall on the same margin. What should I do if both should be in seperate blocks?
The period (.) on the radio button, is tiny enough to be visible. If I increase the font-size it misaligns with the text.
I have copied bootstrap CSS and HTML format too.
Here is my jsfiddle link.
https://jsfiddle.net/488s5e70/2/
input[type="radio"],
input[type="checkbox"] {
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
outline: none;
}
input[type="checkbox"]+span:before {
width: 16px;
height: 16px;
border: 1px solid #000;
content: "\2714";
color: transparent;
display: inline-block;
font-size: 13px;
margin-right: 5px;
text-align: center;
}
input[type="checkbox"]:disabled+span {
color: #ddd;
}
input[type="checkbox"]:checked+span:before {
content: "\2714";
color: #000;
}
input[type="checkbox"]:disabled:checked+span:before {
content: "\2714";
color: #ddd;
}
input[type="checkbox"]:disabled+span:before {
border: 1px solid #ddd;
}
input[type="radio"]+span:before {
width: 16px;
height: 16px;
border: 1px solid #000;
content: ".";
color: transparent;
display: inline-block;
font-size: 13px;
font-weight: bold;
margin-right: 5px;
border-radius: 8px;
text-align: center;
}
input[type="radio"]:disabled+span {
color: #ddd;
}
input[type="radio"]:checked+span:before {
content: "\00b7";
color: #000;
}
input[type="radio"]:disabled:checked+span:before {
content: "\00b7";
color: #ddd;
}
input[type="radio"]:disabled+span:before {
border: 1px solid #ddd;
}
<div class="container">
<div class="checkbox">
<label>
<input type="checkbox" value="" checked>
<span>Option one is this and that—be sure to include why it's greatOption one is this and that—be sure to include why it's greatOption one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="">
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="" disabled>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="" disabled checked>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1">
<span>Option one is this and that—be sure to include why it's great</span>
</label>
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
<label>
<input type="radio" name="optionsRadios2" id="optionsRadios1" value="option1" disabled>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
<label>
<input type="radio" name="optionsRadios2" id="optionsRadios1" value="option1" checked disabled>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
</div>

Transparency instead content ""
its right way! its even better. every thing is OK
fitting the tick mark
when its about a font, there is no warranty about a same and true display in all browsers, i just prefer a background image for check box, a png image
if you have mobile side users, note that all contents inside your elements (tick and ball) probably got change
plain text replaced with android emoticons
seperate blocks
A table with only 2 column could be fastest solution.
Radio
use another character, my offering is "●" or "•"
however the best way is background-image
input[type="radio"]:checked+span:before{
content:"\25CF"; /* or any better character */
color:#000;
}

Right, some things I would change to fix this.
Flexbox, Flexbox everywhere.
Why use a period, when we can use a BLACK CIRCLE
You can find different shapes here if you type "(Hex value)" as the content in you before it will appear.
Here's what I've done.
input[type="radio"],
input[type="checkbox"] {
position: relative;
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
outline: none;
}
input[type="checkbox"]+span:before {
width: 16px;
height: 16px;
border: 1px solid #000;
content: "\2714";
color: transparent;
display: inline-block;
font-size: 13px;
margin-right: 5px;
text-align: center;
/* new code notice me! */
display: inline-flex;
align-items: center;
justify-content: center;
}
input[type="checkbox"]:disabled+span {
color: #ddd;
}
input[type="checkbox"]:checked+span:before {
content: "\2714";
color: #000;
}
input[type="checkbox"]:disabled:checked+span:before {
content: "\2714";
color: #ddd;
}
input[type="checkbox"]:disabled+span:before {
border: 1px solid #ddd;
}
input[type="radio"]+span:before {
width: 16px;
height: 16px;
border: 1px solid #000;
content: ".";
color: transparent;
display: inline-block;
font-size: 13px;
font-weight: bold;
margin-right: 5px;
border-radius: 8px;
text-align: center;
/* new code notice me! */
display: inline-flex;
align-items: center;
justify-content: center;
}
input[type="radio"]:disabled+span {
color: #ddd;
}
/* \25cf is the code for BLACK CIRCLE */
input[type="radio"]:checked+span:before {
content: "\25cf";
color: #000;
}
input[type="radio"]:disabled:checked+span:before {
content: "\25cf";
color: #ddd;
}
input[type="radio"]:disabled+span:before {
border: 1px solid #ddd;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<div class="container">
<div class="checkbox">
<label>
<input type="checkbox" value="" checked>
<span>Option one is this and that—be sure to include why it's greatOption one is this and that—be sure to include why it's greatOption one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="">
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="" disabled>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="" disabled checked>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1">
<span>Option one is this and that—be sure to include why it's great</span>
</label>
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
<label>
<input type="radio" name="optionsRadios2" id="optionsRadios1" value="option1" disabled>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
<label>
<input type="radio" name="optionsRadios2" id="optionsRadios1" value="option1" checked disabled>
<span>Option one is this and that—be sure to include why it's great</span>
</label>
</div>
</div>
The flexbox lines are;
display: inline-flex;
align-items: center;
justify-content: center;
They mean;
- things inside me should use flexbox,
- they should be centered vertically,
- they should be centered horizontally.
I hope you find this helpful :-)

Related

How to modify the styles of the values of the radio buttons in CSS?

I am completely new to programming and I am trying to make a simple survey page to start. I am only using CSS and HTML. I have made radio buttons but I am not sure how to 'select' them in CSS.
Below is my HTML code. I would like to style the questions that are in element <p> but I want to do them all differently. I know I can select p {'how I want font, etc.. styled here} and then style in CSS but I want them all slightly different colors. When I try .survey-question-1 p {'how I want font styled here'} nothing happens.
I really don't know what selectors to use to call the elements I want to change.
<div class='survey-name'>
First name: <input type='text' id= 'firstname' name='FirstName'><br>
Last name: <input type='text' name='LastName'><br>
</div>
<div class='survey-question-1'>
<p>Are you a Front-End or Back-End Developer?</p>
<input type='radio' name='developer' value='Front-End'> Front-End<br>
<input type='radio' name='developer' value='Back-End'> Back-End<br>
</div>
<div class='survey-question-2'>
<p>How many years of experience do you have?</p>
<input type='radio' name='years' value='less than 1'> less than 1<br>
<input type='radio' name='years' value='1-2'> 1-2<br>
<input type='radio' name='years' value='2-3'> 2-3<br>
<input type='radio' name='years' value='3-4'> 3-4<br>
<input type='radio' name='years' value='4-5'> 4-5<br>
<input type='radio' name='years' vale='more than 5'> more than 5<br>
</div>
A good practice is to label your radio buttons. (See MDN page for labels). So I assume you will change your markup accordingly.
Secondly you probably want to use the attribute selector to target the radio buttons. You can also use the :checked pseudo selector for styling the checked radio button.
And thirdly, to style radio buttons you might need to apply appearance: none.
.survey-question-1 input[type="radio"] {
appearance: none;
-moz-appearance: none;
-webkit-appearance: none;
background: pink;
border: 0.5ex solid pink;
border-radius: 100%;
width: 1em;
height: 1em;
}
.survey-question-1 input[type="radio"]:checked {
background: rebeccapurple;
}
<form>
<fieldset class='survey-question-1'>
<legend>
Are you a Front-End or Back-End Developer?
</legend>
<label>
<input type='radio' name='developer' value='Front-End'>
Front-End
</label>
<label>
<input type='radio' name='developer' value='Back-End'>
Back-End
</label>
</fieldset>
</form>
Bear in mind, this is a hideous design, but it will show you how to change the color/styling of every single component on your page.
Let me know if there is specific styling you were after or if something is unclear.
/* Style Survey Name section */
.survey-name {
color: green;
}
.survey-name input {
border: 1px solid green;
}
/* Style Survey Q1 section */
.survey-question-1,
.survey-question-1 p {
color: red;
}
.survey-question-1 input[type='radio']:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: white;
content: '';
display: inline-block;
border: 2px solid gray;
}
.survey-question-1 input[type='radio']:checked:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: red;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid gray;
}
/* Style Survey Q2 section */
.survey-question-2,
.survey-question-2 p {
color: blue;
}
.survey-question-2 input[type='radio']:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: white;
content: '';
display: inline-block;
border: 2px solid gray;
}
.survey-question-2 input[type='radio']:checked:after {
width: 15px;
height: 15px;
border-radius: 15px;
top: -2px;
left: -1px;
position: relative;
background-color: blue;
content: '';
display: inline-block;
visibility: visible;
border: 2px solid gray;
}
<div class='survey-name'>
First name: <input type='text' id='firstname' name='FirstName' /><br />
Last name: <input type='text' name='LastName' /><br />
</div>
<div class='survey-question-1'>
<p>Are you a Front-End or Back-End Developer?</p>
<input type='radio' name='developer' value='Front-End' /> Front-End<br>
<input type='radio' name='developer' value='Back-End' /> Back-End<br>
</div>
<div class='survey-question-2'>
<p>How many years of experience do you have?</p>
<input type='radio' name='years' value='less than 1' /> less than 1<br>
<input type='radio' name='years' value='1-2' /> 1-2<br>
<input type='radio' name='years' value='2-3' /> 2-3<br>
<input type='radio' name='years' value='3-4' /> 3-4<br>
<input type='radio' name='years' value='4-5' /> 4-5<br>
<input type='radio' name='years' vale='more than 5'> more than 5<br>
</div>
See also JSFiddle
Resources I used:
Radio Button Styling
:After CSS

Divide options by border lines, hide when selected

input {
position: absolute;
left: -9999px;
}
input + label {
border-right: solid 1px #000;
padding-right: 8px;
margin-right: 8px;
}
input:checked + label {
display: none;
}
<input type="radio" id="one" name="option" checked/>
<label for="one">One</label>
<input type="radio" id="two" name="option" />
<label for="two">Two</label>
<input type="radio" id="three" name="option" />
<label for="three">Three</label>
There are three options, when the option is selected, it should be hidden, including the border line between the option and the next option.
For example,
When I select "One", I should see "Two | Three".
When I select "Two", I should see "One | Three".
When I select "Three", I should see "One | Two".
It does not necessary to be radio buttons, any other possible solutions are also welcome, but I want to achieve this by using CSS only.
Try the CSS selector :last-of-type. It's just, using CSS, you ~might~ have to give up that last border line in general.
code updated below
input {
position: absolute;
left: -9999px;
}
/*input:first-of-type + label {
border-right: solid 1px #000;
padding-right: 4px;
margin-right: 4px;
}*/
input:not(:first-of-type) + label {
border-left: solid 1px #000;
padding-left: 8px;
margin-left: 8px;
}
input:checked + label {
display: none;
}
/*label:not(:checked):last-of-type {
border-right: none;
}*/
<html>
<input type="radio" id="one" name="option" />
<label for="one">One</label>
<input type="radio" id="two" name="option" />
<label for="two">Two</label>
<input type="radio" id="three" name="option" />
<label for="three">Three</label>
<input type="radio" id="four" name="option" checked/>
<label for="four">Four</label>
<input type="radio" id="five" name="option" />
<label for="five">Five</label>
</html>

How to display and adjust elements when using br tag?

This is my jsFiddle here
HTML Block:
<span class="areas">
<label><input type="checkbox" />Item <br/> 1</label>
<label><input type="checkbox" />Item 2</label>
<label><input type="checkbox" />Item 3</label>
<label><input type="checkbox" />Item 4</label>
<label><input type="checkbox" />Item 5</label>
<label><input type="checkbox" />Item 6</label>
<label><input type="checkbox" />Item 7</label>
</span>
</div>
CSS:
div.arealist { display: block; clear: both; margin-top: 40px; }
div.arealist>span { display: inline-block; padding: 25px; }
div.arealist>span label { display: inline-block; }
div.arealist>span.group { width: 75px; border: 1px solid #d0d0d0; }
div.arealist>span.areas { width: 300px; border: 1px solid #d0d0d0; }
This is how the elements are displayed when we enter a <br/> tag within an element :
I know that the <br/> tag just does what it needs to do so by introducing a line break here.
But my problem is how to display something like this ?
Meaning, Items are still displayed in original order; But notice Item 1. It just stacks itself and auto-adjusts whenever a <br/> is present.
I tried defining some divs and spans but not satisfied with clumsy code. What is the best way of doing this ?
I'm using angularjs, html5 in my project. Are there any inbuilt way of doing this ?
you can move label after checkbox , assign id for checkbox and bind it to label.
<input id="chk" type="checkbox" />
<label for="chk" style="vertical-align:middle; text-align:center;">Item <br/> 1</label>
Demo here.
There is an alternative of using CSS3 flexbox, which actually gets quite close to what you want. Contrary to popular belief, flexbox is actually rather widely supported: unless you want to extend support to IE9 and below. You need to apply flexbox on two levels though:
On the span.areas parent, because you want to distribute the individual <label> elements
Within each <label> element, as you want to position the checkbox such that it is vertically centered
There are of course other hacks such as display: table-cell, but I am generally in favour of flexbox due to its flexibility (no pun intended).
Here is a proof-of-concept example based on your code:
div.arealist {
display: block;
clear: both;
margin-top: 40px;
}
/* span.areas to have flexbox,
so we can distribute the label groups
*/
div.arealist>span {
display: flex;
flex-wrap: wrap;
padding: 25px;
}
/* Each label to have flexbox as well,
so we can center the checkbox vertically
*/
div.arealist>span label {
display: flex;
align-items: center;
text-align: center;
}
div.arealist>span.areas {
width: 300px;
border: 1px solid #d0d0d0;
}
<div class="arealist">
<span class="areas">
<label><input type="checkbox" />Item <br/> 1</label>
<label><input type="checkbox" />Item 2</label>
<label><input type="checkbox" />Item 3</label>
<label><input type="checkbox" />Item 4</label>
<label><input type="checkbox" />Item 5</label>
<label><input type="checkbox" />Item 6</label>
<label><input type="checkbox" />Item 7</label>
</span>
</div>
You can use text-align:center to the label element.
div.arealist>span label { display: inline-block; text-align:center; }
Use this
div.arealist>span label {
display: inline-block;
text-align: center;
float: left;
height: 30px;
padding: 5px;
}
Add text-align: center as below
div.arealist>span label { display: inline-block;text-align:center }
Demo here

Display all select option in single line

By default only one selected or the default option is displayed in the select box however I want that all the select option to be displayed on the same line and among them the selected option must be highlighted.
Following is my code
.selecttodiv{
display: inline-block;
height: 3em;
-webkit-appearance: none;
-moz-appearance: none;
}
.selecttodiv option{
display: inline-block;
width: 2.5em;
height: 2.5em;
}
<select class="selecttodiv">
<option>I</option>
<option>II</option>
<option>III</option>
<option>IV</option>
<option>V</option>
<option>VI</option>
</select>
following is the link to jsfiddle: https://jsfiddle.net/6yg4yhyy/
I am able to display all option on the single line but not when selected.
Thanks for any help.
You can try using size attribute on select and floating option.
On Chrome and FF it was displayed properly. IE (11) doesn't work.
JSFiddle
Maybe it's better to use some select plugin where you can style it as you want..
Don't style default form elements. It will cause more issues than you think. Better use some extension that provides stylable html wrapper.
E.g. select2, chosen
$(document).ready(function() {
$('.selecttodiv').select2({
width: '190px'
});
})
.select2-results li {
display: inline-block !important;
width: 30px;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.min.js"></script>
<select class="selecttodiv" multiple="multiple">
<option>I</option>
<option>II</option>
<option>III</option>
<option>IV</option>
<option>V</option>
<option>VI</option>
</select>
Update
Also it may be checkboxes:
.check {
display: inline-block;
}
span {
width: 20px;
height: 20px;
display: block;
color: black;
font-weight: bold;
cursor: pointer;
text-align: center;
}
input {
display: none;
}
input:checked + span {
background-color: blue;
color: white;
}
<label class="check">
<input type="checkbox" />
<span>I</span>
</label>
<label class="check">
<input type="checkbox" />
<span>II</span>
</label>
<label class="check">
<input type="checkbox" />
<span>III</span>
</label>
<label class="check">
<input type="checkbox" />
<span>IV</span>
</label>
<label class="check">
<input type="checkbox" />
<span>V</span>
</label>
<label class="check">
<input type="checkbox" />
<span>VI</span>
</label>

Radio Button CSS3 - Customization

I am trying to customize Radio buttons on bootstrap but it is not working, When i try outside bootstrap environment in plain HTML it works, Below is code for HTML followed by CSS I am using
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
My Current AMP Policies
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
Financial Planning and Retirement Advice
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios3" value="option3">
LifeCover
</label>
</div>
THE CSS
.newradio input[type=radio] {display: none;}
.newradio label {
display: inline-block;
cursor: pointer;
position: relative;
padding-left: 25px;
margin-right: 15px;
font-size: 13px;
}
.newradio label:before {
content: "";
display: inline-block;
width: 14px;
height: 14px;
border-radius: 7px;
margin-right: 10px;
position: absolute;
left: 0;
bottom: 3px;
background-color: #fff;
border:solid 1px #bcb882;
}
.newradio input[type=radio]:checked + .newradio label:before {
content: "";
background: url(../../Content/images/radio-check.png);
display: inline-block;
}
The reason for .newradio is I am using that div as parent class of above given HTML, I am getting proper display of my Radios but Checked status (the image radio-check) is not showing, I tried using color change instead of background image but that doesn't work either.
Does anyone has experience in customizing radio button in bootstrap? please guide
Manoj Soni
You need to find the bootstrap css class and overwrite it. Or use ID instead of class in the css. When that also don't work try !important.