Move placeholder above the input on focus - html

I'm looking for css code that move the placeholder text above the input on focus. I found this code here. This code is perfect but my input tag is wrapped inside <span> and for that reason general sibling selector is not working. Any ideas how to edit this css?
<div>
<span class='blocking-span'>
<input type="text" class="inputText" />
</span>
<span class="floating-label">Your email address</span>
</div>

You can use the CSS pseudo-selector :placeholder-shown in this case to detect when to move a fake placeholder out of the way. See example below:
label {
margin:20px 0;
position:relative;
display:inline-block;
}
span {
padding:10px;
pointer-events: none;
position:absolute;
left:0;
top:0;
transition: 0.2s;
transition-timing-function: ease;
transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
opacity:0.5;
}
input {
padding:10px;
}
input:focus + span, input:not(:placeholder-shown) + span {
opacity:1;
transform: scale(0.75) translateY(-100%) translateX(-30px);
}
/* For IE Browsers*/
input:focus + span, input:not(:-ms-input-placeholder) + span {
opacity:1;
transform: scale(0.75) translateY(-100%) translateX(-30px);
}
<label>
<input placeholder=" ">
<span>Placeholder Text</span>
</label>

With the given links CSS etc, simply move the floating-label inside the blocking-span.
By using position: relative on the div the floating-label will still re-position as if it were outside the blocking-span
div {
position: relative; /* make label relate to div */
padding-top: 10px; /* make space for label */
}
.inputText {
font-size: 14px;
width: 200px;
height: 25px;
}
.floating-label {
position: absolute;
pointer-events: none;
left: 15px;
top: 18px;
transition: 0.2s ease all;
}
input:focus ~ .floating-label,
input:not(:focus):valid ~ .floating-label {
top: -6px;
}
<div>
<span class='blocking-span'>
<input type="text" class="inputText" required/>
<span class="floating-label">Your email address</span>
</span>
</div>

If you change html structure then your reference example is work but if you don't want change html structure then you need to write little jQuery. You can check this.
$(function(){
$('.blocking-span input').on('focus', function(){
$(this).parents('.parents-elm').addClass('foucs-content'); // When focus the input area
});
$(document).mouseup(function(e){
if($(e.target).parents('.blocking-span input').length==0 && !$(e.target).is('.blocking-span input')){
$('.parents-elm').removeClass('foucs-content');
}
});
});
div{
position: relative;
}
.blocking-span{
display: block;
}
.blocking-span input{
border: 1px solid #eaeaea;
height: 80px;
padding-top: 30px;
padding-left: 20px;
padding-right: 20px;
width: 100%;
}
.floating-label{
display: inline-block;
font-size: 15px;
left: 20px;
line-height: 20px;
position: absolute;
top: -webkit-calc(50% - 10px);
top: -moz-calc(50% - 10px);
top: calc(50% - 10px);
transition: top 0.3s ease-in-out 0s;
}
.foucs-content .floating-label{
top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="parents-elm">
<span class='blocking-span'>
<input type="text" class="inputText" />
</span>
<span class="floating-label">Your email address</span>
</div>

I have used contact form 7 and it gaves me this additional span. But I already found solution how to block this additional span. There is a filter that can remove this contact form 7 span. Here is the link.

Related

HTML/CSS, a checkbox checked make another buttons unchecked

enter image description hereI have a toggle button implemented as checkbox element and some buttons, there are all clickable, the problem is: when this toggle button be clicked, the button clicked before will be unclicked.
Here is the html and css file on github:
https://github.com/jokao1030/Test/tree/b121d8ff0b586cdfc34d1f65996aec9c7a73965c
Maybe someone know how is it going? Please give me some advise, thank you !
It is becoming unchecked because you are not using checkboxes, you are using buttons. Buttons are not meant for use like this, they are meant for one time clicks. As soon as you click somewhere else on the page, the button is no longer active which is why your styling no longer applies to it. Instead, use <input type="radio"> or <input type="checkbox">
I think you will want to do more than this, but this should get you started.
Create an array to keep track of the selected cities.
When the slider is clicked, pop off the last element in the array (which removes that city from the array of selected cities), and at the same time remove the "selected" class so that the background coloring is also removed
const selected = [];
document.querySelectorAll('button').forEach((el) => {
el.addEventListener('click', (e) => {
const city = e.target.classList[0];
document.querySelector(`.${city}`).classList.add('selected');
if (!selected.includes(city)) selected.push(city);
console.log(selected);
});
});
document.querySelector('#check').addEventListener('click', () => {
const uncity = selected.pop();
document.querySelector(`.${uncity}`).classList.remove('selected');
console.log(selected);
});
.choicediv, .ein_auspendler{
position:relative;
max-width: 500px;
}
.selected{
background: #d6d8ff;
}
.slider{
position: absolute;
cursor: pointer;
border: 1.5px solid #adb0ff;
border-radius:10px;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ffffff;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
font-weight: bold;
border-radius:6px;
height: 24px;
width: 80px;
left:3px;
bottom: 1px;
top: 4px;
background-color: #d6d8ff;
-webkit-transition: .4s;
transition: .4s;
}
#check:checked + .slider:before {
-webkit-transform: translateX(150px);
-ms-transform: translateX(150px);
transform: translateX(150px);
}
.text {
color: black;
}
.text:after {
position: absolute;
top: 7px;
right: 8px;
content: "Auspendler";
font-size:14px;
}
.text:before {
position: absolute;
top: 7px;
left: 8px;
content: "Einpendler";
font-size:14px;
}
#check + .slider + .text:after {
font-weight:normal;
}
#check + .slider + .text:before {
font-weight:bold;
}
#check:checked + .slider + .text:after {
font-weight:bold;
}
#check:checked + .slider + .text:before {
font-weight:normal;
}
.auswahl_buttons{
position:relative;
right:0px;
top:10px;
width:240px;
height:36px;
}
/* BUTTONS */
button{
background-color: white;
border: 1px solid #D6D8FF;
border-radius: 12px;
}
button:hover{
background: #D6D8FF;
}
button:focus{
background: #D6D8FF;
};
<div class="choicediv">
<h1>Pendlermobilität</h1>
<h2>Berlin - Brandenburg</h2>
<p1>Möchten Sie eingehende oder<br></p1>
<p2>ausgehende Bewegeungen ansehen?<br></p2>
<label class="ein_auspendler">
<input type="checkbox" id="check" unchecked>
<div class="slider"></div>
<div class="text"></div>
</label>
<p3>Von wo soll es losgehen?<br></p3>
<!-- ---------BUTTONS------------->
<div class="container">
<button class ="Berlin">Berlin</button>
<button class ="Barlim">Barlim</button>
<button class="Cottbus">Cottbus</button>
<button class="Dahme-Spreewald">Dahme-Spreewald</button>
<button class="Frankfurt (Oder)">Frankfurt (Oder)</button>
<button class="Havelland">Havelland</button>
<button class="Märkisch-Oderland">Märkisch-Oderland</button>
<button class="Oberhavel">Oberhavel</button>
<button class="Oberspreewald-Lausitz">Oberspreewald-Lausitz</button>
<button class="Oder-Spree">Oder-Spree</button>
<button class="Ostprignitz-Ruppin">Ostprignitz-Ruppin</button>
<button class="Potsdam">Potsdam</button>
<button class="Potsdam-Mittelmark">Potsdam-Mittelmark</button>
<button class="Prignitz">Prignitz</button>
<button class="Spree-Neiße">Spree-Neiße</button>
<button class="Teltow-Fläming">Teltow-Fläming</button>
<button class="Uckermark">Uckermark</button>
</div>

CSS Checkbox styling - size color change

I am trying to do simple checkbox styling.
The color change is not working.
How to change the color and border?
input[type=checkbox]{
height: 25px;
width: 25px;
background-color: #eee;
}
input[type=checkbox]:checked {
background-color: #3ee738;
}
<p>
<input type="checkbox" id="showall" name="showall" value=0>
<label for="showall">Show All</label>
</p>
<p>
<input type="checkbox" id="showfl" name="showfl" value=0>
<label for="showfl">Filtered</label>
</p>
<p>
input[type=checkbox] + label {
display: block;
margin: 0.2em;
cursor: pointer;
padding: 0.2em;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox] + label:before {
content: "\2714";
border: 0.1em solid MediumSeaGreen;
border-radius: 0.2em;
display: inline-block;
width: 1em;
height: 1em;
padding-left: 0.2em;
padding-bottom: 0.3em;
margin-right: 0.2em;
vertical-align: bottom;
color: transparent;
transition: .2s;
}
input[type=checkbox] + label:active:before {
transform: scale(0);
}
input[type=checkbox]:checked + label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #fff;
}
input[type=checkbox]:disabled + label:before {
transform: scale(1);
border-color: #aaa;
}
input[type=checkbox]:checked:disabled + label:before {
transform: scale(1);
background-color: #bfb;
border-color: #bfb;
}
<p>
<input type="checkbox" id="showall" name="showall" value=0>
<label for="showall">Show All</label>
</p>
<p>
<input type="checkbox" id="showfl" name="showfl" value=0>
<label for="showfl">Filtered</label>
</p>
<p>
Checkboxes are not currently able to be styled with CSS, as they are typically based on the user's operating system's default styling. The way to get around this with CSS is to style another element, in your case, the <label> tag to serve as the visual indicator of the checkbox, and then some pseudo selector and pseudo element CSS to trigger the change when the input is checked.
Because the input and the label are connected via the id and for attributes, shrinking the input hide it works effectively and still maintains accessibility.
li {
position:relative;
}
input {
height:1px;
width:1px;
position:absolute;
opacity:0;
top:0;
left:0;
}
label {
cursor:pointer;
padding-left:25px;
}
label::before {
content:"";
position:absolute;
left:0;
top:0;
height:15px;
width:15px;
border:1px solid blue;
}
label::after {
content:"";
color:white;
position:absolute;
left:2px;
top:0;
}
input[type=checkbox]:checked + label::before {
background:blue;
}
input[type=checkbox]:checked + label::after {
content:"✓";
}
<ul>
<li><input id="cat" type="checkbox"> <label for="cat">Cat</label></li>
<li><input id="dog" type="checkbox"> <label for="dog">Dog</label></li>
</ul>
What you need to do is to hide the actual html element of the checkbox and create a new one that can be styled easily.
So the logic of this approach is to have a span for the box and a span for the icon and style each accordingly - showing the icon when the input is checked using the :checked pseudoselector.
Note that the label wraps around the whole group so that clicking anywhere on the label will toggle the checkbox - doing it this way removeds the need for the for attribute.
I also added a checked function that will toggle the other checkbox whebn the showAll check is checked.
Edit - note that you can use other methods of getting the icon in there- you may prefer the font awesome fa-check icon for example.
const showAll = document.querySelector("#showAll");
showAll.addEventListener('click', () => toggleAll());
function toggleAll() {
document.querySelector("#showfl").checked = showAll.checked;
}
.checkbox-wrapper {
margin-bottom: 16px;
}
input[type=checkbox]{
display: none
}
.checkbox-box{
display: inline-block;
height: 25px;
width: 25px;
background-color: #eee;
text-align: center
}
.checkbox-icon {
visibility: hidden
}
input[type=checkbox]:checked + .checkbox-box{
background-color: #3ee738;
}
input[type=checkbox]:checked + .checkbox-box .checkbox-icon {
visibility: visible
}
<div class="checkbox-wrapper">
<label>
<input type="checkbox" id="showAll" name="showAll" />
<span class="checkbox-box">
<span class="checkbox-icon">&check;</span>
</span> Show All
</label>
</div>
<div class="checkbox-wrapper">
<label>
<input type="checkbox" id="showfl" name="showfl" />
<span class="checkbox-box">
<span class="checkbox-icon">&check;</span>
</span> Filtered
</label>
</div>

How do I create permanent placeholders? CSS / HTML

I am working on a web form for my first official development job.
I am currently trying to make it so that my input fields placeholders don't disappear but instead i need them to shrink and move to the top of the input field.
I was able to find some code that should be getting the job done but is not.
Can anyone guide me on how to make this work?
Code I found: https://jsfiddle.net/p19j2nm5/
I've worked on this for hours trying all sorts of variations with no luck.
.wrapper /*wpcf7*/{
position:relative;
}
input {
font-size: 14px;
height: 40px;
}
.placeholder{
position:absolute;
font-size: 25px;
pointer-events:none;
left: 1px;
top: 1px;
transition: 0.1s ease all;
}
input:focus ~ .placeholder{
top: 1px;
font-size: 11px;
}
HTML from contact form 7:
<label> First Name:
[text first-name placeholder "First Name"] </label>
<label> Last Name:
[text* last-name placeholder "Last Name"] </label>
<label> Your Email:
[email* your-email placeholder "Example#Example.com"] </label>
Currently, none on my fields placeholders move when focused, but removing "~.placeholder" does make the field shrink when focused.
Example of what I am trying to get my fields to do:
input:focus ~ .lastname_floating-label,
input:focus ~ .firstname_floating-label,
input:not(:focus):valid ~ .firstname_floating-label,
input:not(:focus):valid ~ .lastname_floating-label{
top: 2px;
left: 2px;
bottom: 2px;
font-size: 11px;
opacity: 1;
}
.inputText {
font-size: 14px;
width: 200px;
height: 35px;
}
.firstname_floating-label, .lastname_floating-label {
position: absolute;
pointer-events: none;
left: 10px;
top: 10px;
transition: 0.2s ease all;
}
div{position:relative;}
<div>
<input type="text" class="inputText" required/>
<span class="firstname_floating-label">Firstname</span>
</div>
<div>
<input type="text" class="inputText" required/>
<span class="lastname_floating-label">Lastname</span>
</div>
Ok I changed according your request.
Other examples are wrong. Try to add more input and you can check that the place holder will be positioned in the first input. And than if you focus out the placeholder will cover the typed value.
Check this example if is ok using only HTML and CSS.
firstly to edit placeholders the selector you need to use is :placeholder. The effect you are trying to recreate is really just a trick with floating the form labels, instead of the placeholder text. There is a thorough explanation available here: https://css-tricks.com/float-labels-css/
If you look closely the jsfiddle example actually is not using a placeholder but a span tag with a class of .placeholder
Your own code has input placeholder attribute and you can target it and input :focus like this:
input::-webkit-input-placeholder {
color: #999;
font-size: 15px;
}
input:focus::-webkit-input-placeholder {
color: red;
font-size: 5px;
}
/* Firefox < 19 */
input:-moz-placeholder {
color: #999;
}
input:focus:-moz-placeholder {
color: red;
font-size: 5px;
}
/* Firefox > 19 */
input::-moz-placeholder {
color: #999;
}
input:focus::-moz-placeholder {
color: red;
font-size: 5px;
}
/* Internet Explorer 10 */
input:-ms-input-placeholder {
color: #999;
}
input:focus:-ms-input-placeholder {
color: red;
font-size: 5px;
}
This will make placeholder text go smaller on input focus.
Updated Daebak Do's answer after reading your comment.
I added some padding to the input, and moved the "placeholder" content inside the input (changing top value from -12px to 2px and adding a left: 2px). I also added margin-top property to make the transition smooth and not disturb the rest of the content on your page.
Now the placeholder content is located inside the input box, like on the screenshot you shared.
.wrapper{
position: relative;
}
input {
font-size: 14px;
height: 40px;
transition: padding-top 0.1s, margin-top 0.1s;
margin-top: 15px;
}
.placeholder {
position: absolute;
font-size:25px;
pointer-events: none;
left: 3px;
top: 18px;
transition: 0.1s ease all;
}
input:focus ~ .placeholder{
top: 2px;
left: 2px;
font-size: 11px;
}
input:focus{
margin-top: 1px;
padding-top: 15px;
transition: padding-top 0.1s, margin-top 0.1s;
}
<div class="wrapper">
<input type="text">
<span class="placeholder">Placeholder</span>
</div>
<p>Some content.</p>

Custom style radiobuttons

I want to style my radio buttons so that that black dot inside the circle is for example red.
There are several examples available on the internet like this one: JSFIDDLE. The thing with this example is that it does not work in Internet Explorer.
Another point that makes my situation harder is that I can not, due to implementation requirements, add any other html objects to the following code:
<span class="custom-radio">
<input id="id4" type="radio" name="id_test" value="">
<label for="id4">No</label>
</span>
My question is: how can I create a custom radiobutton without adding extra HTML to the code above and still make it work in most browsers (IE, FF, Chrome)
You can hide the input itself with display:none and the use a pseudo-element on the label
input[type='radio'] {
display: none;
}
input[type='radio'] + label {
position: relative;
line-height: 1em;
}
input[type='radio'] + label:before {
content: '';
width: .5em;
height: .5em;
border-radius:100%;
margin-right: .5em;
display: inline-block;
background: red;
vertical-align: middle;
box-shadow: 0 0 0 .1em white, 0 0 0 .2em black;
}
input[type='radio']:checked + label:before {
background: green;
vertical-align: middle;
}
<span class="custom-radio">
<input id="id4" type="radio" name="id_test" value=""/>
<label for="id4">No</label>
</span>
After that it's just a matter of styling the pseudo-element to taste,
for something like changing the colour of the tick/ball you can use ::before:
input:checked ~ label::before{
content: "";
background: #F90 none repeat scroll 0% 0%;
width: 8px;
height: 8px;
position: absolute;
border-radius: 20px;
left: 7px;
top: 5px;
}
here's a fiddle
n.b. You'd be positioning the ::before element, so this would need tweeking to the correct position when used in your application
You cannot do that in IE because it does not allow you to use :before on input elements, at least according to this answer. I think the only thing you can do in your situation is to try adding :before on the label and position it over the checkbox.
Try this link
Styling Radio Buttons with CSS
Update : code (copy/past from above link):
<input id="choice-a" type="radio" name="g" />
<label for='choice-a'>
<span><span></span></span>
Choice A
</label>
input[type="radio"] {
opacity: 0;
position: absolute;
}
/* Matches the direct descendant of a label preceded by a
radio button */
input[type="radio"] + label > span {
position: relative;
border-radius: 14px;
width: 20px;
height: 20px;
background-color: #f0f0f0;
border: 1px solid #bcbcbc;
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.2);
margin: 0 1em 0 0;
display: inline-block;
vertical-align: middle;
}
/* Matches the direct descendant of a label preceded by a
checked radio button */
input[type="radio"]:checked + label > span {
background: linear-gradient(#a0e5f8, #75c7dc);
background: -webkit-linear-gradient(#a0e5f8, #75c7dc);
border-color: #41a6bf;
box-shadow: 0px 1px 2px rgba(65, 166, 191, 0.9) inset;
}
/* Matches a span contained by the direct descendant
of a label preceded by a checked radio button */
input[type="radio"]:checked + label > span span {
display: inline-block;
width: 8px;
height: 8px;
position: absolute;
left: 6px;
top: 6px;
border-radius: 5px;
border: none;
background: #167c95;
box-shadow: 0px 1px rgba(255, 255, 255, 0.3);
}
input[type="radio"]:focus + label > span {
box-shadow: 0px 0px 6px rgba(63, 165, 190, 1);
}
Fiddle example
Simply use a combination of hidden radio input elements and styled span elements wrapped in label elements, styled accordingly:
input[type=radio] {
display: none;
}
span {
border-radius: 100%;
height: 20px;
width: 20px;
display: inline-block;
border: 1px solid;
position: relative;
}
input[type=radio]:checked + span:before {
content: '';
position: absolute;
height: 50%;
width: 50%;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
background: green;
border-radius: 100%;
}
<label>
<input type="radio" name="myRadio" />
<span></span>Item 1
</label>
<label>
<input type="radio" name="myRadio" />
<span></span>Item 2
</label>
<label>
<input type="radio" name="myRadio" />
<span></span>Item 3
</label>
You cannot change the properties of radio buttons or checkboxes, but you can simulate them. Keep your HTML the same, and add this to your CSS.
.custom-radio input[type=radio] {
display:none;
}
.custom-radio label {
display:inline-block;
}
.custom-radio label:before {
content:"";
display:inline-block;
width:16px;
height:16px;
border-radius:50%;
background:url("http://s17.postimg.org/p1q2imsln/radio.png");
background-position:0% 0%;
}
.custom-radio label:hover:before {
background-position:0% 100%
}
.custom-radio input[type=radio]:checked~label:before {
background-position:100% 0%;
}
.custom-radio input[type=radio]:checked~label:hover:before {
background-position:100% 100%;
}
Simply provide an image that follows the template in the link that has red radio buttons.
Why even use the input[type="radio"] element at all? You can recreate in in html, css, and javascript and offer better browser support than any crazy css :before, :after stuff. Here is the code and a working jsfiddle:
Here is a link to the fiddle: https://jsfiddle.net/www139/ba5jn2e6/19/
Hope this helps anyone else with the issue. I experienced the same dilemma, so I decided to create it from scratch!
window.onload = function(){
var radioButtonGroups = document.getElementsByClassName('radioGroupContainer');
for(var i = 0; i != radioButtonGroups.length; i++)
{
var radioButtons = radioButtonGroups[i].getElementsByClassName('radioButtonContainer');
for(var i = 0; i != radioButtons.length; i++)
{
radioButtons[i].onclick = function(){
var value = this.children[0].getAttribute('name');
for(var i = 0; i != radioButtons.length; i++)
{
radioButtons[i].children[0].setAttribute('class','radioButtonDot');
}
this.children[0].setAttribute('class','radioButtonDotActive');
this.parentNode.setAttribute('name',value);
};
}
}
};
/*
* Created by William Green.
* Questions or comments? Email william.green#protonmail.com
* I would appreciate credit for this code if you use it; but it is not required.
* Last updated July 26, 2015
* Created July 26, 2015
*
*/
.radioButtonContainer {
background-color:#eee;
padding:5px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
display:table;
margin-top:5px;
margin-bottom:5px;
}
.radioButtonContainer .radioButtonDot {
width:16px;
height:16px;
background-color:transparent;
border:1px solid #000;
display:inline-block;
vertical-align:middle;
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%;
-o-transition:all .5s ease;
-moz-transition:all .5s ease;
-webkit-transition:all .5s ease;
-ms-transition:all .5s ease;
transition:all .5s ease;
}
.radioButtonContainer .radioButtonDotActive {
width:16px;
height:16px;
background-color:#1396DE;
border:1px solid transparent;
display:inline-block;
vertical-align:middle;
-moz-border-radius:50%;
-webkit-border-radius:50%;
border-radius:50%;
-o-transition:all .5s ease;
-moz-transition:all .5s ease;
-webkit-transition:all .5s ease;
-ms-transition:all .5s ease;
transition:all .5s ease;
}
.radioButtonContainer .radioButtonLabel {
background-color:transparent;
display:inline-block;
vertidal-align:middle;
border:0;
}
<div class="radioGroupContainer" id="radioChoicesOne">
<div class="radioButtonContainer">
<div class="radioButtonDot" name="optionOne"></div>
<input type="button" class="radioButtonLabel" value="Option One">
</div>
<div class="radioButtonContainer">
<div class="radioButtonDot" name="optionTwo"></div>
<input type="button" class="radioButtonLabel" value="Option Two">
</div>
<div class="radioButtonContainer">
<div class="radioButtonDot" name="optionThree"></div>
<input type="button" class="radioButtonLabel" value="Option Three">
</div>
</div>
<div id="radioButtonGroupOneValue"></div>
<input type="button" value="Get radio button value..." onclick="document.getElementById('radioButtonGroupOneValue').innerHTML = document.getElementById('radioChoicesOne').getAttribute('name');">

Style a checkbox in firefox — remove check and border

How do I style a checkbox in firefox, and have the checkmark and border disappear?
http://jsfiddle.net/moneylotion/qZvtY/
CSS:
body { background: black; }
#conditions-form { color: white; }
#conditions-form input[type=checkbox] {
display:inline-block;
-webkit-appearance: none;
-moz-appearance: none;
-o-appearance:none;
appearance: none;
width:19px;
height:19px;
background: url('http://demo.somedomain.com/wp-content/themes/themename/images/care-plan-checkbox.gif') no-repeat top left;
cursor:pointer;
}
#conditions-form input[type=checkbox]:checked {
background:url('http://demo.somedomain.com/wp-content/themes/themename/images/care-plan-checkbox-checked.gif') no-repeat top left;
}
HTML:
<form id="conditions-form">
<ul>
<li>
<input id="condition3" type="checkbox" name="conditions[condition3]"></input>
<label class="checkbox" for="condition3">Conditions 3</label>
</li>
</ul>
</form>
There's a quite easy way you can do this via <label> tags. Just place a label around the checkbox, and insert a dummy element that will be used for the custom styled checkbox. For example:
label.checkbox input[type="checkbox"] {display:none;}
label.checkbox span {
display:inline-block;
border:2px solid #BBB;
border-radius:10px;
width:25px;
height:25px;
background:#C33;
vertical-align:middle;
margin:3px;
position: relative;
transition:width 0.1s, height 0.1s, margin 0.1s;
}
label.checkbox :checked + span {
background:#6F6;
width:27px;
height:27px;
margin: 2px;
}
label.checkbox :checked + span:after {
content: '\2714';
font-size: 20px;
position: absolute;
top: -2px;
left: 5px;
color: #99a1a7;
}
<label class="checkbox">
<input type="checkbox"/>
<span></span>
I like cake
</label>
EDIT: Note that some choices of colours might render the state of your checkbox invisible for colourblind people. When making this code I didn't think of that, but the above demo might be invisible for R/G colourblind people. When implementing this, please do keep that in mind (pick bright/dark colours for example, or show some difference in shape)
The styles I used are just arbitrary, and you can change that to anything you want. You can even toggle certain text inside it via the ::before pseudo-element, such as what I've done here.
I wasn't able to open the image url you provided to use in your question, but I think you'll be able to include whatever image you want by simply modifying this code a little. Just change the current background color to the image URL you want to use.
Note: This won't work in some older browsers.
The accepted answer above is great but this slight tweak to the fiddle from Joeytje50 allows the check-boxes to be tabbed to.
Using opacity 0 instead of display none means the checkbox is still tabbable and hence accessible by keyboard.
Position absolute places the input checkbox top left of the drawn box meaning your formatting stays neat.
input[type="checkbox"] {
opacity: 0;
position: absolute;
}
input[type="checkbox"] + label {
text-align:center;
cursor:pointer;
}
input[type="checkbox"]:focus + label {
background-color: #ddd;
}
input[type="checkbox"] + label div {
display:inline-block;
line-height: 16px;
font-size: 12px;
height: 16px;
width: 16px;
margin:-0px 4px 0 0;
border: 1px solid black;
color: transparent;
}
input[type="checkbox"]:checked + label div {
color: black;
}
<input type="checkbox" id="c1" name="cc" />
<label for="c1">
<div>✔</div>Check Box 1<br />
</label>
<input type="checkbox" id="c12" name="cc" />
<label for="c12">
<div>✔</div>Check Box 2<br />
</label>
This tutsplus tutorial solved my question.
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label span {
display:inline-block;
width:19px;
height:19px;
margin:-1px 4px 0 0;
vertical-align:middle;
background:url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/391_checkboxes/check_radio_sheet.png) left top no-repeat;
cursor:pointer;
}
input[type="checkbox"]:checked + label span {
background:url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/391_checkboxes/check_radio_sheet.png) -19px top no-repeat;
}
<input type="checkbox" id="c1" name="cc" />
<label for="c1"><span></span>Check Box 1</label>
A cleaner solution IMHO that uses pure css to redraw the elements.
Codepen
input[type="checkbox"] {
opacity: 0;
position: absolute;
}
input[type="checkbox"] ~ label:before {
content: '';
display: inline-block;
cursor: pointer;
...
border: 3px solid #999;
border-radius: 2px;
transition: .3s;
}
input[type="checkbox"]:checked ~ label:before {
background: #333;
}