How to create an animated email input when user clicks on it using CSS - html

I want to create a cool animation that when the user clicks on the email input or the password the label of the input will go up and have a nice transition in the bottom border.
This is what I have:
And this is what I want to create:
My code:
.form {
position: relative;
display: flex;
flex-direction: column;
margin-bottom: 1rem;
}
.input {
background: none;
color: #c6c6c6;
font-size: 1.8rem;
padding: 1.6rem;
display: block;
border: none;
border-bottom: 1px solid #c6c6c6;
width: 100%;
}
.label {
position: absolute;
color: #c6c6c6;
font-size: 1.6rem;
left: 0.5rem;
top: 1rem;
}
<div class="form">
<input class="email input" type="email" name="email" />
<label class="label" for="email">Email Address</label>
</div>
<div class="form">
<input class="password input" type="password" name="password" />
<label class="label" for="password">Password</label>
</div>
I've searched a lot but every code example I found was with SCCS, SASS and I don't understand it. So please help me with plain CSS. Any help would be greatly appreciated!

I created an animated demo using HTML and CSS.
.main {
width: 500px;
margin: 50px 100px;
;
}
.form-group {
position: relative;
margin-bottom: 45px;
}
input {
display: block;
width: 300px;
font-size: 14pt;
padding: 10px;
border: none;
border-bottom: 1px solid #ccc;
}
input:focus {
outline: none;
}
label {
position: absolute;
top: 10px;
left: 5px;
color: #999;
font-size: 14pt;
font-weight: normal;
pointer-events: none;
transition: all 0.2s ease;
}
input:focus ~ label,
input:valid ~ label {
top: -20px;
font-size: 10pt;
color: #5264AE;
}
.bar {
display: block;
position: relative;
width: 320px;
}
.bar:before,
.bar:after {
content: "";
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #5264AE;
transition: all 0.2s ease;
}
.bar:before {
left: 50%;
}
.bar:after {
right: 50%;
}
input:focus ~ .bar:before,
input:focus ~ .bar:after {
width: 50%;
}
<div class="main">
<div class="form-group">
<input type="text" name="email" required />
<span class="highlight"></span>
<span class="bar"></span>
<label for="email">Email</label>
</div>
<div class="form-group">
<input type="password" name="password" required />
<span class="highlight"></span>
<span class="bar"></span>
<label for="password">Password</label>
</div>
</div>

I attached your code modified to work as intended, you can change any value to customize as you want but here's an explanation of how it works:
Input element
I added a z-index so that the user can click where the label is positioned and click the input instead of the label itself, also added a transition to make the animation smooth.
input:focus refers when input is active (user click or selected by pressing tab key).
And here's where the magic happens and the explanations of complex selectors:
.input:focus~.label,
.input:not([value=""]):not(:focus):invalid~.label,
.input.has-content~.label {
font-size: .7rem;
top: -.4rem;
color: blue;
}
.input:focus ~ .label selects all input's element siblings with the class .label when input is focus so that when user focus in the input the label'll be above.
.input:not([value=""]):not(:focus):invalid~.label this selector'll catch when user unfocus the input but it got content, so the label don't goes down and overlap with the username. (as the password type input doesn't have value attribute I attach you a Js snippet that do the same trick for password element, simply adding the class has-content to the element)
Hope it helps you!
document.querySelector('.password').oninput = (e) => {
e.target.value.length > 0 ?
e.target.classList.add('has-content') :
e.target.classList.remove('has-content')
}
.form {
position: relative;
display: flex;
flex-direction: column;
margin: 1rem;
}
.input {
height: 20px;
background: none;
color: #c6c6c6;
font-size: 1rem;
padding: .5rem .7rem;
display: block;
border: none;
border-bottom: 2px solid #c6c6c6;
width: 100%;
z-index: 2;
transition: all .3s ease;
}
.input:focus {
outline: transparent;
border-color: blue;
}
.input:focus~.label,
.input:not([value=""]):not(:focus):invalid~.label,
.input.has-content~.label {
font-size: .7rem;
top: -.4rem;
color: blue;
}
.label {
position: absolute;
color: #c6c6c6;
font-size: 1rem;
left: 0.5rem;
top: .6rem;
transition: all .3s ease;
}
<div class="form">
<input class="email input" type="email" name="email" />
<label class="label" for="email">Email Address</label>
</div>
<div class="form">
<input class="password input" type="password" name="password" />
<label class="label" for="password">Password</label>
</div>

Related

How to display radio buttons as <div>

I want to display my radio buttons as so that i can give style sheet and make div clickable.
here are my radio buttons.
and I want to show them like this.
div as radio
This is what I have tried but did not work.
HTML
<div class="radio-toolbar">
<table>
<tr>
<td><input type="radio" id="radioone" name="product" value="first">
<label for="radioone">50</label></td>
<td><input type="radio" id="radiotwo" name="product" value="second">
<label for="radiotwo">100</label></td>
<td><input type="radio" id="radiothree" name="product" value="third">
<label for="radiothree">500</label></td>
</div>
CSS
.radio-toolbar input[type='radio']:focus + label {
border: 2px thin blue;
}
.radio-toolbar input[type='radio']:checked + label {
background-color: #86b1f7;
border-color: #4c4;
color: white;
}
So how can I achive this
Small help will be appreciated.
Thanks in advance :)
I had do this before.and I have the code preperd.I hope It will be useful for you.
.switch-field {
display: flex;
overflow: hidden;
direction: ltr;
float: right;
text-align:right;
}
.switch-field input {
position: absolute !important;
clip: rect(0, 0, 0, 0);
height: 1px;
width: 1px;
border: 0;
overflow: hidden;
}
.switch-field label {
/*width: 33.33%;*/
background-color: transparent;
color: #666666;
font-size: 14px;
line-height: 1;
text-align: center;
padding: 10px 12px;
margin-left: 0px;
/*margin-right: -1px;*/
border: 1px solid #ddd;
transition: all 0.1s ease-in-out;
}
.switch-field label:hover {
cursor: pointer;
}
.switch-field input:checked + label {
background-color: #F4F5F9;
box-shadow: none;
border-color: #3E7DE7;
color: #3E7DE7;
}
.switch-field label:first-of-type {
border-radius: 2px 0px 0px 2px;
}
.switch-field label:last-of-type {
border-radius: 0px 2px 2px 0px;
}
<div class="switch-field">
<input class="uk-radio" id="radio-six" type="radio" value="admin" name="user_type" checked>
<label for="radio-six">admin</label>
<input class="uk-radio" id="radio-seven" type="radio" value="user" name="user_type" >
<label for="radio-seven">user</label>
</div>
Here is one way you can customize your radio buttons.
.radio-toolbar .radio-item {
position: relative;
display: inline-block;
vertical-align: top;
margin: 15px 5px;
}
.radio-item input[type="radio"] {
position: absolute;
width: 0;
height: 0;
top: 0;
left: 0;
}
.radio-item label {
padding: 10px;
color: #fff;
border-radius: 10px;
background-color: grey;
border: 1px solid #000;
}
.radio-item input[type="radio"]:checked ~ label {
border-color: #f00;
}
<div class="radio-toolbar">
<div class="radio-item">
<input type="radio" id="radioone" name="product" value="first">
<label for="radioone">50</label>
</div>
<div class="radio-item">
<input type="radio" id="radiotwo" name="product" value="second">
<label for="radiotwo">100</label>
</div>
<div class="radio-item">
<input type="radio" id="radiothree" name="product" value="third">
<label for="radiothree">500</label>
</div>
</div>
Please let me know if this helps.
You need to hide the radio buttons and the left over label is still "clickable"
.radio-toolbar input[type="radio"] {
display: none;
}
I put all the working code on codepen here: https://codepen.io/stormingorman-the-vuer/pen/ExjKRvz
Try Like This. Make Your radio-toolbar input, width: 0;
also make your radio-toolbar label Like this
.radio-toolbar label {
background-color: #A9A9A9;
padding: 10px 20px;
font-size: 16px;
border-radius: 4px;
}

How to create material design input form using css and bootstrap?

I want to design following material design input form using css and bootstrap. Following code is I am currently using. But it doesn't provide exact result I want.
Code Pen Link : View Source Code Here
HTML CODE :
<div class="container">
<h2>Google Material Design in CSS3<small>Inputs</small></h2>
<form>
<div class="group">
<input type="text" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Name</label>
</div>
<div class="group">
<input type="text" required>
<span class="highlight"></span>
<span class="bar"></span>
<label>Email</label>
</div>
</form>
<p class="footer">
a tutorial by scotch.io
</p>
</div>
But I want this design :
CSS Only solution; use combination of sibling selector ~ on the label and :valid pseudo selector on the input.
body {
margin: 10px;
}
.form-group>label {
bottom: 34px;
left: 15px;
position: relative;
background-color: white;
padding: 0px 5px 0px 5px;
font-size: 1.1em;
transition: 0.2s;
pointer-events: none;
}
.form-control:focus~label {
bottom: 55px;
}
.form-control:valid~label {
bottom: 55px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-6">
<br>
<div class="form-group">
<input type="text" class="form-control" id="usr" required>
<label for="usr">Name</label>
</div>
<div class="form-group">
<input type="text" class="form-control" id="password" required>
<label for="usr">Password</label>
</div>
</div>
</div>
</div>
Since you've tagged Bootstrap 4, I'm assuming you wanted the solution with regards to that framework.
Setup a default form-group, label, and input markup like this;
<div class="form-group">
<label for="usr">Name:</label>
<input type="text" class="form-control" id="usr">
</div>
Then add this css, what this would do is
position label relative to its container (form-group)
then we specified the top and left positions so that it would land
on top of the input field
I added a white background and padding to the label so that it would have a box around the label.
.form-group > label {
top: 18px;
left: 6px;
position: relative;
background-color: white;
padding: 0px 5px 0px 5px;
font-size: 0.9em;
}
Here's a fiddle with that code on bootstrap 4;
http://jsfiddle.net/rw29jot4/
For the animation, check this fiddle, we need to utilize click events and move the position of the label;
Updated code with animation;
http://jsfiddle.net/sedvo037/
EDIT: Please see my answer below which uses only CSS.
Try with this code.
HTML:
<div class="main_div">
<div class="group">
<input type="text" required="required"/>
<label>Name</label>
</div>
</div>
CSS:
.main_div{
padding: 30px;
}
input,
textarea {
background: none;
color: #c6c6c6;
font-size: 18px;
padding: 10px 10px 10px 15px;
display: block;
width: 320px;
border: none;
border-radius: 10px;
border: 1px solid #c6c6c6;
}
input:hover{
border: 3px solid black;
}
input:focus,
textarea:focus {
outline: none;
border: 3px solid black;
}
input:focus ~ label, input:valid ~ label,
textarea:focus ~ label,
textarea:valid ~ label {
top: -5px;
font-size: 12px;
color: #000;
left: 11px;
}
input:focus ~ .bar:before,
textarea:focus ~ .bar:before {
width: 320px;
}
input[type="password"] {
letter-spacing: 0.3em;
}
.group{
position: relative;
}
label {
color: #c6c6c6;
font-size: 16px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 15px;
top: 12px;
transition: 300ms ease all;
background-color: #fff;
padding: 0 2px;
}

How to make the label go up after clicking with css?

I want to make a form when on clicked the label would go up 15px and you can write the text, but I can't figure out how to do it with my current code. I think the problem is how I select the label and input.
I tried these ways:
.form-row-field-input:focus ~ label,
.form-row-field-input:valid ~ label {
top: -12px;
left: 0;
font-size: 12px;
color: #003333;
font-weight: bold;}
input[type=text]:focus~label {
display:block;
color: black;
top: -20px;
font-size: 14px;}
<div class="form-inputs">
<form action="" method="POST">
<div class="form-row">
<span class="form-row-number">01</span>
<label for="POST-name" class="form-row-field">First name</label>
<input type="text" class="form-row-field-input">
</div>
<div class="form-row">
<span class="form-row-number">02</span>
<label for="POST-lastname" class="form-row-field">Last name</label>
<input type="text" class="form-row-field-input">
</div>
<div class="form-row">
<span class="form-row-number">03</span>
<label for="POST-email" class="form-row-field">Email</label>
<input type="text" class="form-row-field-input">
</div>
<input class="form-btn" type="submit" value="Get it">
</form>
</div><!--end-->
/** CSS **/
.form-inputs {
padding: 40px;
flex: 60%;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
position: relative;
}
.form-btn {
margin: 20px 0px;
background-color: #f6fe00;
color: black;
padding: 10px 40px;
font-weight: 700;
border: none;
cursor: pointer;
}
.form-row {
border-bottom: 1px solid grey;
padding: 10px 0px;
position: relative;
}
.form-row-number {
color: #8f63ff;
padding-right: 10px;
}
.form-row-field {
color: #9b91f5;
position: absolute;
pointer-events: none;
transition: 0.5s;
top: 10px;
margin-left: 1em;
}
.form-row-field-input {
background-color: inherit;
border: none;
display: block;
position: absolute;
top: 10px;
transition: 0.5s;
margin-left: 1.5em;
}
I expect that when clicked on input, the label would go up with a transition
The general sibling selector in CSS that you're using can only target elements that come AFTER, so since the label comes before the input in your HTML, it's not actually applying the CSS.
You can get the intended effect simply be moving the label element after the input. Google's Material UI also takes this approach in order to keep it primarily CSS to achieve the intended visual effect.
Here it is with the label elements moved to after the input and slightly tweaked CSS:
<body>
<div class="form-inputs">
<form action="" method="POST">
<div class="form-row">
<span class="form-row-number">01</span>
<input type="text" class="form-row-field-input">
<label for="POST-name" class="form-row-field">First name</label>
</div>
<div class="form-row">
<span class="form-row-number">02</span>
<input type="text" class="form-row-field-input">
<label for="POST-lastname" class="form-row-field">Last name</label>
</div>
<div class="form-row">
<span class="form-row-number">03</span>
<input type="text" class="form-row-field-input">
<label for="POST-email" class="form-row-field">Email</label>
</div>
<input class="form-btn" type="submit" value="Get it">
</form>
</div>
<!--end-->
</body>
.form-inputs {
padding: 40px;
flex: 60%;
display: flex;
flex-direction: column;
align-content: center;
justify-content: center;
position: relative;
}
.form-btn {
margin: 20px 0px;
background-color: #f6fe00;
color: black;
padding: 10px 40px;
font-weight: 700;
border: none;
cursor: pointer;
}
.form-row {
border-bottom: 1px solid grey;
padding: 10px 0px;
position: relative;
}
.form-row-number {
color: #8f63ff;
padding-right: 10px;
}
.form-row-field {
color: #9b91f5;
position: absolute;
pointer-events: none;
transition: 0.5s;
top: 10px;
margin-left: 1em;
}
.form-row-field-input {
background-color: inherit;
border: none;
display: block;
position: absolute;
top: 10px;
transition: 0.5s;
margin-left: 1.5em;
}
.form-row-field-input:focus~label {
top: -5px;
font-size: 12px;
color: #003333;
font-weight: bold;
}
https://jsfiddle.net/7urgeL60/

Floating label styling

I am using the CSS found here to make floating form labels. I chose this styling because there is no JavaScript needed and the HTML markup is simple enough (i.e. a div with a class and then only a label and input tag). If there are other simple style sheets available please let me know.
I am a backend developer and I suck at CSS and cannot figure out how to adjust this CSS so it looks nice when:
There is a select tag (<select/> instead of an <input/> tag)
When the background is not white
Any help will be greatly appreciated!
<form class="float-label" spellcheck="false">
<legend>Float Label Demo</legend>
<!-- we need a wrapping element for positioning the label -->
<!-- the required attribute is ... required! -->
<div class="control">
<input type="text" name="title" placeholder="Title" required />
<!-- yes, this is not nice, in real live, do it with JS -->
<label for="title">Title</label>
</div>
<div class="control small">
<input type="text" name="price" placeholder="Price" required />
<label for="price">Price</label>
</div>
<div class="control medium">
<input type="text" name="location" placeholder="Specific location (optional)" required />
<label for="location">Specific location (optional)</label>
</div>
<div class="control">
<textarea name="description" placeholder="Description" required></textarea>
<label for="description">Description</label>
</div>
</form>
#border: 1px solid #ddd;
#padding: 10px;
#label-font-size: 13px;
*,
*:before,
*:after {
box-sizing: border-box;
}
body {
width: 100%;
min-height: 300px;
}
// Demo styles
form {
width: 600px;
margin: 2em auto;
font-family: 'Source Sans Pro', sans-serif;
font-weight: 300;
font-size: 22px;
legend {
font-size: 2em;
margin-bottom: 1em;
width: 100%;
border-bottom: #border;
}
}
// float label
.float-label {
.control {
float: left;
position: relative;
width: 100%;
border-bottom: #border;
padding-top: #padding + #label-font-size;
padding-bottom: #padding;
// you proably want to replace these with your grid classes
&.small {
width: 30%;
border-right: #border;
}
&.medium {
width: 70%;
padding-left: #padding;
}
&:last-child {
border: 0;
}
}
input, textarea {
display: block;
width: 100%;
border: 0;
outline: 0;
resize: none;
// inactive but shown label (exceptions: opacity and top)
& + label {
position: absolute;
top: 10px;
transition: top 0.7s ease, opacity 0.7s ease;
opacity: 0;
// Some nice styling
font-size: #label-font-size;
font-weight: 600;
color: #ccc;
}
// THE MAGIC
// as soon as we start typing, the "required" attribute will
// set the state to valid and our pseudo selector will match
&:valid + label {
opacity: 1;
top: 3px;
}
// and we highlight the focused input label
&:focus + label {
color: #2c8efe;
}
}
}
Here is what this styling looks like when the background is black:
You've copied the Less (non-compiled CSS) which will not render properly in browsers - so if you take examples from CodePen in the future keep that in mind :)
The inputs have a transparent background, which lends itself to any background colour ( or pattern ) so if you change the background colour of the body/parent it will still look good
This might be what you're looking for:
*,
*:before,
*:after {
box-sizing: border-box;
}
body {
width: 100%;
min-height: 300px;
background-color: #000;
}
form {
width: 600px;
margin: 2em auto;
font-family: 'Source Sans Pro', sans-serif;
font-weight: 300;
font-size: 22px;
}
form legend {
font-size: 2em;
margin-bottom: 1em;
width: 100%;
border-bottom: 1px solid #333;
}
.float-label .control {
float: left;
position: relative;
width: 100%;
border-bottom: 1px solid #333;
padding-top: 23px;
padding-bottom: 10px;
}
.float-label .control.small {
width: 30%;
border-right: 1px solid #333;
}
.float-label .control.medium {
width: 70%;
padding-left: 10px;
}
.float-label .control:last-child {
border: 0;
}
.float-label select,
.float-label input,
.float-label textarea {
display: block;
width: 100%;
border: 0;
outline: 0;
resize: none;
background: transparent;
color: #fff;
}
.float-label select + label,
.float-label input + label,
.float-label textarea + label {
position: absolute;
top: 10px;
transition: top 0.7s ease, opacity 0.7s ease;
opacity: 0;
font-size: 13px;
font-weight: 600;
color: #ccc;
}
.float-label select:focus + label,
.float-label input:valid + label,
.float-label textarea:valid + label {
opacity: 1;
top: 3px;
}
.float-label select:focus + label,
.float-label input:focus + label,
.float-label textarea:focus + label {
color: #ccc;
}
<form class="float-label" spellcheck="false">
<legend>Float Label Demo</legend>
<!-- we need a wrapping element for positioning the label -->
<!-- the required attribute is ... required! -->
<div class="control">
<input type="text" name="title" placeholder="Title" required />
<!-- yes, this is not nice, in real live, do it with JS -->
<label for="title">Title</label>
</div>
<div class="control small">
<input type="text" name="price" placeholder="Price" required />
<label for="price">Price</label>
</div>
<div class="control medium">
<input type="text" name="location" placeholder="Specific location (optional)" required />
<label for="location">Specific location (optional)</label>
</div>
<div class="control">
<select>
<option>option 1</option>
<option>option 2</option>
</select>
<label for="description">Description</label>
</div>
</form>
Have a look
*,
*:before,
*:after {
box-sizing: border-box;
}
body {
width: 100%;
min-height: 300px;
background-color: #000;
}
form {
width: 600px;
margin: 2em auto;
font-family: 'Source Sans Pro', sans-serif;
font-weight: 300;
font-size: 22px;
}
form legend {
font-size: 2em;
margin-bottom: 1em;
width: 100%;
border-bottom: 1px solid #333;
}
.float-label .control {
float: left;
position: relative;
width: 100%;
border-bottom: 1px solid #333;
padding-top: 23px;
padding-bottom: 10px;
}
.float-label .control.small {
width: 30%;
border-right: 1px solid #333;
}
.float-label .control.medium {
width: 70%;
padding-left: 10px;
}
.float-label .control:last-child {
border: 0;
}
.float-label select,
.float-label input,
.float-label textarea {
display: block;
width: 100%;
border: 0;
outline: 0;
resize: none;
background: transparent;
color: #fff;
}
.float-label select + label,
.float-label input + label,
.float-label textarea + label {
position: absolute;
top: 10px;
transition: top 0.7s ease, opacity 0.7s ease;
opacity: 0;
font-size: 13px;
font-weight: 600;
color: #ccc;
}
.float-label select:focus + label,
.float-label input:valid + label,
.float-label textarea:valid + label {
opacity: 1;
top: 3px;
}
.float-label select:focus + label,
.float-label input:focus + label,
.float-label textarea:focus + label {
color: #ccc;
}
option{color:#000;}
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:active,
input:-webkit-autofill:focus {
-webkit-box-shadow: 0 0 0 1000px #000 inset !important;
-webkit-text-fill-color: #ccc !important;
}
<form class="float-label" spellcheck="false">
<legend>Float Label Demo</legend>
<!-- we need a wrapping element for positioning the label -->
<!-- the required attribute is ... required! -->
<div class="control">
<input type="text" name="title" placeholder="Title" required />
<!-- yes, this is not nice, in real live, do it with JS -->
<label for="title">Title</label>
</div>
<div class="control small">
<input type="text" name="price" placeholder="Price" required />
<label for="price">Price</label>
</div>
<div class="control medium">
<input type="text" name="location" placeholder="Specific location (optional)" required />
<label for="location">Specific location (optional)</label>
</div>
<div class="control">
<select>
<option>option 1</option>
<option>option 2</option>
</select>
<label for="description">Description</label>
</div>
</form>
Add this code to your stylesheet :
.float-label select,
.float-label input,
.float-label textarea {
background: transparent;
}
And you will get the <input>, <textarea>and <select> with transparent background. So you can change the background color to the color of your choice later without any tension about form field background colors.
Thanks Nabeel
A simple answer is to change the background-color of the inputs directly.
Something like this;
input { background-color: blue;}
you could copy/paste that right into your CSS and use any color you want. I put this CSS into the following html code at W3schools.com and got this:
<!DOCTYPE html>
<html>
<body>
<form action="">
User name:
<br>
<style>
input {
background-color: blue;
}
</style>
<input type="text" name="userid">
<br>User password:
<br>
<input type="password" name="psw">
</form>
<p>The characters in a password field are masked (shown as asterisks or circles).</p>
</body>
</html>
I hope this works for you!

CSS ''background-color" attribute not working on checkbox inside <div>

The heading pretty much explains it. I have a couple of checkboxes inside a scrollable div. But for some reasons the 'background-color' attribute doesn't work. Although the 'margin-top' does seem to work...
Just puzzling me how one attribute can work and another not. It's also not like the div has it's own set of background color attributes that could potentially over ride the checkboxes attributes.
Anyways, below is my HTML (which is generated by JSP):
<div class="listContainer">
<input type="checkbox" class="oddRow">item1<br/>
<input type="checkbox" class="evenRow">item2<br/>
<input type="checkbox" class="oddRow">item3<br/>
<input type="checkbox" class="evenRow">item4<br/>
...
</div>
And here is my CSS:
.listContainer {
border:2px solid #ccc;
width:340px;
height: 225px;
overflow-y: scroll;
margin-top: 20px;
padding-left: 10px;
}
.oddRow {
margin-top: 5px;
background-color: #ffffff;
}
.evenRow{
margin-top: 5px;
background-color: #9FFF9D;
}
A checkbox does not have background color.
But to add the effect, you may wrap each checkbox with a div that has color:
<div class="evenRow">
<input type="checkbox" />
</div>
<div class="oddRow">
<input type="checkbox" />
</div>
<div class="evenRow">
<input type="checkbox" />
</div>
<div class="oddRow">
<input type="checkbox" />
</div>
In addition to the currently accepted answer: You can set border and background of a checkbox/radiobutton, but how it is rendered in the end depends on the browser. For example, if you set a red background on a checkbox
IE will show a red border instead
Opera will show a red background as intended
Firefox, Safari and Chrome will do nothing
This German language article compares a few browsers and explains at least the IE behavior. It maybe bit older (still including Netscape), but when you test around you'll notice that not much has changed. Another comparison can be found here.
You can use peseudo elements like this:
input[type=checkbox] {
width: 30px;
height: 30px;
margin-right: 8px;
cursor: pointer;
font-size: 27px;
}
input[type=checkbox]:after {
content: " ";
background-color: #9FFF9D;
display: inline-block;
visibility: visible;
}
input[type=checkbox]:checked:after {
content: "\2714";
}
<label>Checkbox label
<input type="checkbox">
</label>
After so much trouble i got it.
.purple_checkbox:after {
content: " ";
background-color: #5C2799;
display: inline-block;
visibility: visible;
}
.purple_checkbox:checked:after {
content: "\2714";
box-shadow: 0px 2px 4px rgba(155, 155, 155, 0.15);
border-radius: 3px;
height: 12px;
display: block;
width: 12px;
text-align: center;
font-size: 9px;
color: white;
}
<input type="checkbox" class="purple_checkbox">
It will be like this when checked with this code.
My solution
Initially posted here.
input[type="checkbox"] {
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
outline: 0;
background: lightgray;
height: 16px;
width: 16px;
border: 1px solid white;
}
input[type="checkbox"]:checked {
background: #2aa1c0;
}
input[type="checkbox"]:hover {
filter: brightness(90%);
}
input[type="checkbox"]:disabled {
background: #e6e6e6;
opacity: 0.6;
pointer-events: none;
}
input[type="checkbox"]:after {
content: '';
position: relative;
left: 40%;
top: 20%;
width: 15%;
height: 40%;
border: solid #fff;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
display: none;
}
input[type="checkbox"]:checked:after {
display: block;
}
input[type="checkbox"]:disabled:after {
border-color: #7b7b7b;
}
<input type="checkbox"><br>
<input type="checkbox" checked><br>
<input type="checkbox" disabled><br>
<input type="checkbox" disabled checked><br>
2022 - there is a much better solution to this problem now
Just use the accent-color property and make sure you achieve proper contrast ratios for accessibility:
.blue-checkbox {
accent-color: #00eaff;
height: 30px; /* not needed */
width: 30px; /* not needed */
}
<input class="blue-checkbox" type="checkbox" />
We can provide background color from the css file. Try this one,
<!DOCTYPE html>
<html>
<head>
<style>
input[type="checkbox"] {
width: 25px;
height: 25px;
background: gray;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: none;
outline: none;
position: relative;
left: -5px;
top: -5px;
cursor: pointer;
}
input[type="checkbox"]:checked {
background: blue;
}
.checkbox-container {
position: absolute;
display: inline-block;
margin: 20px;
width: 25px;
height: 25px;
overflow: hidden;
}
</style>
</head>
<body>
<div class="checkbox-container">
<input type="checkbox" />
</div>
</body>
</html>
The Best solution to change background checkbox color
input[type=checkbox] {
margin-right: 5px;
cursor: pointer;
font-size: 14px;
width: 15px;
height: 12px;
position: relative;
}
input[type=checkbox]:after {
position: absolute;
width: 10px;
height: 15px;
top: 0;
content: " ";
background-color: #ff0000;
color: #fff;
display: inline-block;
visibility: visible;
padding: 0px 3px;
border-radius: 3px;
}
input[type=checkbox]:checked:after {
content: "✓";
font-size: 12px;
}
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
<input type="checkbox" name="vehicle" value="Car" checked> I have a bus<br>
Improving another answer here
input[type=checkbox] {
cursor: pointer;
margin-right: 10px;
}
input[type=checkbox]:after {
content: " ";
background-color: lightgray;
display: inline-block;
position: relative;
top: -4px;
width: 24px;
height: 24px;
margin-right: 10px;
}
input[type=checkbox]:checked:after {
content: "\00a0\2714";
}
When you input the body tag, press space just one time without closing the tag and input bgcolor="red", just for instance. Then choose a diff color for your font.