Checkbox CSS :checked styling - html

I'm trying to understand where the problem in my CSS is, but I really don't have any clue what is wrong.
Basically, I'm styling the default checkbox style with CSS. It works well until you try to check the checkbox.
The idea behind my code is when you check the checkbox, it should increase the size and change the background colour to red. In addition, it should keep the style till you unchecked the box (manually).
Do you have any idea where the problem is? In my opinion the problem is where the "input[type="checkbox"]:checked .check-box-effect {" begins.
Please find the code below
label {
display: inline-block;
color: #fff;
cursor: pointer;
position: relative;
}
label .check-box-effect {
display: inline-block;
position: relative;
background-color: transparent;
width: 25px;
height: 25px;
border: 2px solid #dcdcdc;
border-radius: 10%;
}
label .check-box-effect:before {
content: "";
width: 0px;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(45deg);
top: 13px;
left: 9px;
transition: width 50ms ease 50ms;
transform-origin: 0% 0%;
}
label .check-box-effect:after {
content: "";
width: 0;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(305deg);
top: 16px;
left: 10px;
transition: width 50ms ease;
transform-origin: 0% 0%;
}
label:hover .check-box-effect:before {
width: 5px;
transition: width 100ms ease;
}
label:hover .check-box-effect:after {
width: 10px;
transition: width 150ms ease 100ms;
}
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]:checked .check-box-effect {
background-color: red !important;
transform: scale(1.25);
}
input[type="checkbox"]:checked .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover .check-box-effect {
background-color: #dcdcdc;
transform: scale(1.25);
}
input[type="checkbox"]:checked:hover .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
<label>
<input type="checkbox" id="chkProdTomove" />
<span class="check-box-effect"></span>
</label>

You need to change all checked selectors to this:
input[type="checkbox"]:checked + .check-box-effect:before
label {
display: inline-block;
color: #fff;
cursor: pointer;
position: relative;
}
label .check-box-effect {
display: inline-block;
position: relative;
background-color: transparent;
width: 25px;
height: 25px;
border: 2px solid #dcdcdc;
border-radius: 10%;
}
label .check-box-effect:before {
content: "";
width: 0px;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(45deg);
top: 13px;
left: 9px;
transition: width 50ms ease 50ms;
transform-origin: 0% 0%;
}
label .check-box-effect:after {
content: "";
width: 0;
height: 2px;
border-radius: 2px;
background: #626262;
position: absolute;
transform: rotate(305deg);
top: 16px;
left: 10px;
transition: width 50ms ease;
transform-origin: 0% 0%;
}
label:hover .check-box-effect:before {
width: 5px;
transition: width 100ms ease;
}
label:hover .check-box-effect:after {
width: 10px;
transition: width 150ms ease 100ms;
}
input[type="checkbox"] {
display: none;
}
input[type="checkbox"]:checked + .check-box-effect {
background-color: red !important;
transform: scale(1.25);
}
input[type="checkbox"]:checked + .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked + .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover + .check-box-effect {
background-color: #dcdcdc;
transform: scale(1.25);
}
input[type="checkbox"]:checked:hover + .check-box-effect:after {
width: 10px;
background: #333;
transition: width 150ms ease 100ms;
}
input[type="checkbox"]:checked:hover + .check-box-effect:before {
width: 5px;
background: #333;
transition: width 150ms ease 100ms;
}
<label>
<input type="checkbox" id="chkProdTomove" />
<span class="check-box-effect"></span>
</label>

You need to target the next element when the checkbox is checked:
input[type="checkbox"]:checked + .check-box-effect {
background-color: red !important;
transform: scale(1.25);
}
Your current code is trying to target the .check-box-effect if it would be a child of the checkbox.

Related

Trying to make the accordion collapsible open slower

I'm trying to make the accordion collapsible dropdown open a little slower, right now it opens instantly. Been trying to find online material on how to accomplish this with the code I already have. Can't seem to find anything online.
Any dropdown slowness, just trying to learn how to implement the code.
.so-accordion-wrapper {
padding-top: 2.5rem;
overflow: visible;
border-bottom: 1px solid #eaeaea;
margin-left: auto;
margin-right: auto;
position: relative;
max-width: 820px;
}
.so-tab {
position: relative;
width: 100%;
border-top: 1px solid #eaeaea;
overflow: hidden;
padding-left: 20px;
padding-right: 20px;
}
.so-tab label {
position: relative;
display: block;
padding: 0 25px 0 0;
margin-bottom: 15px;
margin-top: 15px;
line-height: normal;
cursor: pointer;
letter-spacing: 0.2em;
text-transform: uppercase;
font-size: 12px;
}
.so-tab input {
position: absolute;
opacity: 0;
z-index: -1;
}
.so-tab-content {
max-height: 0;
overflow: hidden;
transition: max-height .35s;
}
/* :checked */
.so-tab input:checked ~ .so-tab-content {
max-height: none;
}
/* Icon */
.so-tab label::after {
position: absolute;
right: 0;
top: -5px;
font-size: 1.5em;
color: #959595;
display: block;
-webkit-transition: all 0.50s ease;
-moz-transition: all 0.50s ease;
-o-transition: all 0.50s ease;
transition: all 0.50s ease;
}
.so-tab input[type=checkbox] + label::after {
content: "+";
}
.so-tab input[type=radio] + label::after {
content: "\25BC";
}
.so-tab input[type=checkbox]:checked + label::after {
transform: rotate(315deg);
}
.so-tab input[type=radio]:checked + label::after {
transform: rotateX(180deg);
}
<div class="so-accordion-wrapper">
<div class="so-tab"><input id="so-tab-1" type="checkbox" name="tabs"> <label for="so-tab-1">INGREDIENTS</label>
<div class="so-tab-content">
<p>Yes</p>
</div>
</div>
<div class="so-tab"><input id="so-tab-2" type="checkbox" name="tabs"> <label for="so-tab-2">PRECAUTION</label>
<div class="so-tab-content">
<p>No</p>
</div>
</div>
</div>
As you asked in a comment, here is what I mean:
Firstly, the only change you need is for to give max-height a value - "none" doesn't work.
You have already addressed the usual problem when doing a slide up/down in CSS - you cannot transition height - by using max-height which can be used for transitions. However it needs a value for the transition to work, unfortunately none won't work because the browser cannot calculate the values needed for the transition with no value.
FYI there is also no need to change the transition-timing-function (e.g. ease, ease-in-out, cubic-bezier) or add a transition to the open div (that doesn't actually do anything in your code).
The issue I mentioned is that the slide down action is happening much more quickly than the time you have specified, i.e. 0.35s - you can see it more clearly if you set it to e.g. 1s. The slide-up action takes longer as if it is taking the correct amount of time. See example:
.so-accordion-wrapper {
padding-top: 2.5rem;
overflow: visible;
border-bottom: 1px solid #eaeaea;
margin-left: auto;
margin-right: auto;
position: relative;
max-width: 820px;
}
.so-tab {
position: relative;
width: 100%;
border-top: 1px solid #eaeaea;
overflow: hidden;
padding-left: 20px;
padding-right: 20px;
}
.so-tab label {
position: relative;
display: block;
padding: 0 25px 0 0;
margin-bottom: 15px;
margin-top: 15px;
line-height: normal;
cursor: pointer;
letter-spacing: 0.2em;
text-transform: uppercase;
font-size: 12px;
}
.so-tab input {
position: absolute;
opacity: 0;
z-index: -1;
}
.so-tab-content {
max-height: 0;
overflow: hidden;
transition: max-height 1s;
}
/* :checked */
.so-tab input:checked ~ .so-tab-content {
max-height: 1000px;
}
/* Icon */
.so-tab label::after {
position: absolute;
right: 0;
top: -5px;
font-size: 1.5em;
color: #959595;
display: block;
-webkit-transition: all 0.50s ease;
-moz-transition: all 0.50s ease;
-o-transition: all 0.50s ease;
transition: all 0.50s ease;
}
.so-tab input[type=checkbox] + label::after {
content: "+";
}
.so-tab input[type=radio] + label::after {
content: "\25BC";
}
.so-tab input[type=checkbox]:checked + label::after {
transform: rotate(315deg);
}
.so-tab input[type=radio]:checked + label::after {
transform: rotateX(180deg);
}
<div class="so-accordion-wrapper">
<div class="so-tab"><input id="so-tab-1" type="checkbox" name="tabs"> <label for="so-tab-1">INGREDIENTS</label>
<div class="so-tab-content">
<p>Yes</p>
</div>
</div>
<div class="so-tab"><input id="so-tab-2" type="checkbox" name="tabs"> <label for="so-tab-2">PRECAUTION</label>
<div class="so-tab-content">
<p>No</p>
</div>
</div>
</div>
The reason for this is that the time to slide down is being calculated on the max-height you have set - e.g. 1000px in the example above. So it is taking the full 1-second to slide down 1000px, but your actual div isn't that big so it appears much more quickly.
FYI the same is happening on the slide-up, but it looks slower because it takes almost the full second to reach the "top" of the 1000px that is being shown.
The ways around this are:
use a more appropriate max-height, e.g. if you know your div is never going to be
bigger than 100px;, then set that to your max height - see the example below.
javascript (which should be avoided unless necessary and the above option is unacceptable to you - the more more processing you add to the client-side the slower the page loads - which is bad for both users and Google).
Example using a more appropriate max-height: max-height: 100px - you can see the slide down is now closer to the 1-second time you specified.
.so-accordion-wrapper {
padding-top: 2.5rem;
overflow: visible;
border-bottom: 1px solid #eaeaea;
margin-left: auto;
margin-right: auto;
position: relative;
max-width: 820px;
}
.so-tab {
position: relative;
width: 100%;
border-top: 1px solid #eaeaea;
overflow: hidden;
padding-left: 20px;
padding-right: 20px;
}
.so-tab label {
position: relative;
display: block;
padding: 0 25px 0 0;
margin-bottom: 15px;
margin-top: 15px;
line-height: normal;
cursor: pointer;
letter-spacing: 0.2em;
text-transform: uppercase;
font-size: 12px;
}
.so-tab input {
position: absolute;
opacity: 0;
z-index: -1;
}
.so-tab-content {
max-height: 0;
overflow: hidden;
transition: max-height 1s;
}
/* :checked */
.so-tab input:checked ~ .so-tab-content {
max-height: 100px;
}
/* Icon */
.so-tab label::after {
position: absolute;
right: 0;
top: -5px;
font-size: 1.5em;
color: #959595;
display: block;
-webkit-transition: all 0.50s ease;
-moz-transition: all 0.50s ease;
-o-transition: all 0.50s ease;
transition: all 0.50s ease;
}
.so-tab input[type=checkbox] + label::after {
content: "+";
}
.so-tab input[type=radio] + label::after {
content: "\25BC";
}
.so-tab input[type=checkbox]:checked + label::after {
transform: rotate(315deg);
}
.so-tab input[type=radio]:checked + label::after {
transform: rotateX(180deg);
}
<div class="so-accordion-wrapper">
<div class="so-tab"><input id="so-tab-1" type="checkbox" name="tabs"> <label for="so-tab-1">INGREDIENTS</label>
<div class="so-tab-content">
<p>Yes</p>
</div>
</div>
<div class="so-tab"><input id="so-tab-2" type="checkbox" name="tabs"> <label for="so-tab-2">PRECAUTION</label>
<div class="so-tab-content">
<p>No</p>
</div>
</div>
</div>
In this case, it might not be a big deal as 0.35s is quite fast anyway, but this is a very important point to remember in general, as it can have a much larger affect in other situations.
Your approach is correct, however css cannot create a transition from max-height: 0 to max-height: none. when using a value using pixels you should be able to pull it off, see my fiddle here with a transition:
.so-accordion-wrapper {
padding-top: 2.5rem;
overflow: visible;
border-bottom: 1px solid #eaeaea;
margin-left: auto;
margin-right: auto;
position: relative;
max-width: 820px;
}
.so-tab {
position: relative;
width: 100%;
border-top: 1px solid #eaeaea;
overflow: hidden;
padding-left: 20px;
padding-right: 20px;
}
.so-tab label {
position: relative;
display: block;
padding: 0 25px 0 0;
margin-bottom: 15px;
margin-top: 15px;
line-height: normal;
cursor: pointer;
letter-spacing: 0.2em;
text-transform: uppercase;
font-size: 12px;
}
.so-tab input {
position: absolute;
opacity: 0;
z-index: -1;
}
.so-tab-content {
max-height: 0;
overflow: hidden;
transition: max-height 0.5s cubic-bezier(0, 1, 0, 1);
}
/* :checked */
.so-tab input:checked ~ .so-tab-content {
max-height: 1000px;
transition: max-height 0.5s ease-in-out;
}
/* Icon */
.so-tab label::after {
position: absolute;
right: 0;
top: -5px;
font-size: 1.5em;
color: #959595;
display: block;
-webkit-transition: all 0.50s ease;
-moz-transition: all 0.50s ease;
-o-transition: all 0.50s ease;
transition: all 0.50s ease;
}
.so-tab input[type=checkbox] + label::after {
content: "+";
}
.so-tab input[type=radio] + label::after {
content: "\25BC";
}
.so-tab input[type=checkbox]:checked + label::after {
transform: rotate(315deg);
}
.so-tab input[type=radio]:checked + label::after {
transform: rotateX(180deg);
}
<div class="so-accordion-wrapper">
<div class="so-tab"><input id="so-tab-1" type="checkbox" name="tabs"> <label for="so-tab-1">INGREDIENTS</label>
<div class="so-tab-content">
<p>Yes</p>
</div>
</div>
<div class="so-tab"><input id="so-tab-2" type="checkbox" name="tabs"> <label for="so-tab-2">PRECAUTION</label>
<div class="so-tab-content">
<p>No</p>
</div>
</div>
</div>
Max-height to none is the issue here, you should give a value in pixels and play with the transition seconds.
Nothing else should change.Check my solution.
.so-accordion-wrapper {
padding-top: 2.5rem;
overflow: visible;
border-bottom: 1px solid #eaeaea;
margin-left: auto;
margin-right: auto;
position: relative;
max-width: 820px;
}
.so-tab {
position: relative;
width: 100%;
border-top: 1px solid #eaeaea;
overflow: hidden;
padding-left: 20px;
padding-right: 20px;
}
.so-tab label {
position: relative;
display: block;
padding: 0 25px 0 0;
margin-bottom: 15px;
margin-top: 15px;
line-height: normal;
cursor: pointer;
letter-spacing: 0.2em;
text-transform: uppercase;
font-size: 12px;
}
.so-tab input {
position: absolute;
opacity: 0;
}
.so-tab-content {
max-height:0px;
overflow: hidden;
transition: max-height .75s; //increase the seconds for more delay
}
/* :checked */
.so-tab input:checked ~ .so-tab-content {
max-height:100px;
}
/* Icon */
.so-tab label::after {
position: absolute;
right: 0;
top: -5px;
font-size: 1.5em;
color: #959595;
display: block;
-webkit-transition: all 0.50s ease;
-moz-transition: all 0.50s ease;
-o-transition: all 0.50s ease;
transition: all 0.50s ease;
}
.so-tab input[type=checkbox] + label::after {
content: "+";
}
.so-tab input[type=radio] + label::after {
content: "\25BC";
}
.so-tab input[type=checkbox]:checked + label::after {
transform: rotate(315deg);
}
.so-tab input[type=radio]:checked + label::after {
transform: rotateX(180deg);
}
<div class="so-accordion-wrapper">
<div class="so-tab"><input id="so-tab-1" type="checkbox" name="tabs"> <label for="so-tab-1">INGREDIENTS</label>
<div class="so-tab-content">
<p>Yes</p>
</div>
</div>
<div class="so-tab">
<input id="so-tab-2" type="checkbox" name="tabs">
<label for="so-tab-2">PRECAUTION</label>
<div class="so-tab-content">
<p>No</p>
</div>
</div>
</div>

CSS material desgin like input with default value doesn't go back to original state

I'm doing a material design input but I'm having this issue with the label text, when is empty the label text should appear in the input field as placeholder, but when I assign a default value in the input, the label doesn't disappear when I clear the input.
.group {
position: relative;
margin-bottom: 45px;
}
input {
font-size: 18px;
padding: 10px 10px 10px 5px;
display: block;
width: 300px;
border: none;
border-bottom: 1px solid #757575;
}
input:focus {
outline: none;
}
/* LABEL ======================================= */
label {
color: #999;
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 5px;
top: 10px;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
/* active state */
input:focus~label,
input:valid~label {
top: -20px;
font-size: 14px;
color: #5264AE;
}
/* BOTTOM BARS ================================= */
.bar {
position: relative;
display: block;
width: 300px;
}
.bar:before,
.bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #5264AE;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
.bar:before {
left: 50%;
}
.bar:after {
right: 50%;
}
/* active state */
input:focus~.bar:before,
input:focus~.bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
/* active state */
input:focus~.highlight {
-webkit-animation: inputHighlighter 0.3s ease;
-moz-animation: inputHighlighter 0.3s ease;
animation: inputHighlighter 0.3s ease;
}
/* ANIMATIONS ================ */
#-webkit-keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
#-moz-keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
#keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
<div class="group">
<input type="text" value="default value">
<span class="highlight"></span>
<span class="bar"></span>
<label>Name</label>
</div>
made a pen to show this behaviour:
https://codepen.io/anon/pen/djKvYL
That's because in the html, you are using value="". You should use <input type="text" placeholder="default value"> instead.
.group {
position: absolute;
margin-bottom: 45px;
}
input {
font-size: 18px;
padding: 10px 10px 10px 5px;
display: block;
width: 300px;
border: none;
border-bottom: 1px solid #757575;
}
input:focus {
outline: none;
}
/* LABEL ======================================= */
label {
color: #999;
font-size: 18px;
font-weight: normal;
pointer-events: none;
left: 5px;
top: 10px;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
/* active state */
input:focus~label,
input:valid~label {
top: -20px;
font-size: 14px;
color: #5264AE;
}
/* BOTTOM BARS ================================= */
.bar {
position: relative;
display: block;
width: 300px;
}
.bar:before,
.bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #5264AE;
transition: 0.2s ease all;
-moz-transition: 0.2s ease all;
-webkit-transition: 0.2s ease all;
}
.bar:before {
left: 50%;
}
.bar:after {
right: 50%;
}
/* active state */
input:focus~.bar:before,
input:focus~.bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
/* active state */
input:focus~.highlight {
-webkit-animation: inputHighlighter 0.3s ease;
-moz-animation: inputHighlighter 0.3s ease;
animation: inputHighlighter 0.3s ease;
}
/* ANIMATIONS ================ */
#-webkit-keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
#-moz-keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
#keyframes inputHighlighter {
from {
background: #5264AE;
}
to {
width: 0;
background: transparent;
}
}
<div class="group">
<label>Name</label>
<input type="text" placeholder="default value">
<span class="highlight"></span>
<span class="bar"></span>
</div>
Set required for your input type in html which should solve your issue. We do it for :valid psuedo class to check if something is typed into the input box or not. As the whole thing is done in pure CSS, this required is necessary. Else JavaScript should have to be used for that material effect.
Also, when there will be no valid input in the textbox, shadow appears in some browsers. So, in your CSS, put box-shadow: none; for the input. That should solve the problem(JSFiddle):
body {
margin: 40px;
}
.group {
position: relative;
margin-bottom: 45px;
}
input {
font-size: 18px;
padding: 10px 10px 10px 5px;
display: block;
width: 300px;
border: none;
border-bottom: 1px solid #757575;
box-shadow: none;
}
label {
color: #999;
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 5px;
top: 10px;
transition: 0.2s ease all;
}
input:focus~label,
input:valid~label {
top: -20px;
font-size: 14px;
color: #5264AE;
}
<div class="group">
<input type="text" value="default value" required>
<label>Name</label>
</div>

Clear user search input for search="text" using CSS

I would like add a clear input text feature to the existing search functionality. When user click the cross animation it should clear the input text instead of moving the arrows away. I have added display:none at last not valid, but not working as expected. Can someone please assist on how to implement this functionality ?.
.search-box {
position: relative;
display: inline-block;
border: 2px solid #000;
border-radius: 20px;
float:right;
}
.search-box input[type="text"] {
-webkit-appearance: none;
position: relative;
display: block;
width: 20px;
height: 20px;
margin: 0;
padding: 5px;
border: none;
border-radius: 20px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: none;
box-shadow: none;
z-index: 1;
-webkit-transition: all 0.5s ease-out 0.5s;
transition: all 0.5s ease-out 0.5s;
outline: none;
}
.search-box input[type="text"]:focus {
width: 280px;
height: 30px;
margin-right: 20px;
-webkit-transition: all 0.5s ease-out 0s;
transition: all 0.5s ease-out 0s;
}
.search-box input[type="text"]:focus + span::before {
height: 14px;
margin: -22px 0 0 -15px;
-webkit-transition: all 0.2s ease-out 0.5s;
transition: all 0.2s ease-out 0.5s;
}
.search-box input[type="text"]:focus + span::after {
visibility: visible;
margin: -22px 0 0 -15px;
-webkit-transition: all 0.2s ease-out 0.7s;
transition: all 0.2s ease-out 0.7s;
}
.search-box span {
display: block;
position: absolute;
right: 0;
bottom: 0;
width: 0;
height: 0;
z-index: 10;
}
.search-box span::before {
content: '';
display: block;
position: absolute;
top: 0;
left: 50%;
width: 2px;
height: 8px;
margin: -4px 0 0 0;
background-color: #000;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transition: all 0.2s ease-out 0.2s;
transition: all 0.2s ease-out 0.2s;
}
.search-box span::after {
content: '';
visibility: hidden;
display: block;
position: absolute;
top: 0;
left: 0;
width: 2px;
height: 14px;
margin: -36px 0 0 5px;
background-color: #000;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transition: all 0.2s ease-out 0s;
transition: all 0.2s ease-out 0s;
}
.search-box:not(:valid) ~ span {
display: none;
}
<form name="search">
<label class="search-box">
<input type="text" />
<span></span>
</label>
</form>
https://jsfiddle.net/0wansxza/
You could do it using jQuery with that $('#element').val('');.
jQuery(document).ready(function(){
jQuery('.search-box input[type="text"]').focusout(function() {
jQuery(this).val('');
})
});
.search-box {
position: relative;
display: inline-block;
border: 2px solid #000;
border-radius: 20px;
float:right;
}
.search-box input[type="text"] {
-webkit-appearance: none;
position: relative;
display: block;
width: 20px;
height: 20px;
margin: 0;
padding: 5px;
border: none;
border-radius: 20px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: none;
box-shadow: none;
z-index: 1;
-webkit-transition: all 0.5s ease-out 0.5s;
transition: all 0.5s ease-out 0.5s;
outline: none;
}
.search-box input[type="text"]:focus {
width: 280px;
height: 30px;
margin-right: 20px;
-webkit-transition: all 0.5s ease-out 0s;
transition: all 0.5s ease-out 0s;
}
.search-box input[type="text"]:focus + span::before {
height: 14px;
margin: -22px 0 0 -15px;
-webkit-transition: all 0.2s ease-out 0.5s;
transition: all 0.2s ease-out 0.5s;
}
.search-box input[type="text"]:focus + span::after {
visibility: visible;
margin: -22px 0 0 -15px;
-webkit-transition: all 0.2s ease-out 0.7s;
transition: all 0.2s ease-out 0.7s;
}
.search-box span {
display: block;
position: absolute;
right: 0;
bottom: 0;
width: 0;
height: 0;
z-index: 10;
}
.search-box span::before {
content: '';
display: block;
position: absolute;
top: 0;
left: 50%;
width: 2px;
height: 8px;
margin: -4px 0 0 0;
background-color: #000;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transition: all 0.2s ease-out 0.2s;
transition: all 0.2s ease-out 0.2s;
}
.search-box span::after {
content: '';
visibility: hidden;
display: block;
position: absolute;
top: 0;
left: 0;
width: 2px;
height: 14px;
margin: -36px 0 0 5px;
background-color: #000;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transition: all 0.2s ease-out 0s;
transition: all 0.2s ease-out 0s;
}
.search-box:not(:valid) ~ span {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="search">
<label class="search-box">
<input type="text" />
<span></span>
</label>
</form>
Using Just CSS. I had to modify your markup a little. To clear the input field i had to add a button that resets the form when clicked. See my code and comments below. Hope it helps
.search-box {
position: relative;
display: inline-block;
border: 2px solid #000;
border-radius: 20px;
float:right;
}
.search-box input[type="text"] {
-webkit-appearance: none;
position: relative;
display: block;
width: 20px;
height: 20px;
margin: 0;
padding: 5px;
border: none;
border-radius: 20px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: none;
box-shadow: none;
z-index: 11;/**Changed This**/
-webkit-transition: all 0.5s ease-out 0.5s;
transition: all 0.5s ease-out 0.5s;
outline: none;
}
/**Updated the below codes**/
.search-box input[type="text"]:focus,
.search-box .btn:focus + input{
width: 280px;
height: 30px;
margin-right: 30px;
-webkit-transition: all 0.5s ease-out 0s;
transition: all 0.5s ease-out 0s;
}
/**Updated the below codes**/
.search-box input[type="text"]:focus + span::before,
.search-box .btn:focus + input[type="text"] + span::before{
height: 14px;
margin: -22px 0 0 -15px;
-webkit-transition: all 0.2s ease-out 0.5s;
transition: all 0.2s ease-out 0.5s;
}
/**Updated the below codes**/
.search-box input[type="text"]:focus + span::after,
.search-box .btn:focus + input[type="text"] + span::after{
visibility: visible;
margin: -22px 0 0 -15px;
-webkit-transition: all 0.2s ease-out 0.7s;
transition: all 0.2s ease-out 0.7s;
}
.search-box span {
display: block;
position: absolute;
right: 0;
bottom: 0;
width: 0;
height: 0;
z-index: 10;
}
.search-box span::before {
content: '';
display: block;
position: absolute;
top: 0;
left: 50%;
width: 2px;
height: 8px;
margin: -4px 0 0 0;
background-color: #000;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transition: all 0.2s ease-out 0.2s;
transition: all 0.2s ease-out 0.2s;
}
.search-box span::after {
content: '';
visibility: hidden;
display: block;
position: absolute;
top: 0;
left: 0;
width: 2px;
height: 14px;
margin: -36px 0 0 5px;
background-color: #000;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transition: all 0.2s ease-out 0s;
transition: all 0.2s ease-out 0s;
}
/**
.search-box:not(:valid) ~ span {
display: none;
}**/
/**Added the below codes**/
.search-box .btn {
position: absolute;
top: 0;
right: 0;
bottom: 0;
background: transparent;
border: 0;
outline: 0;
width: 30px;
cursor: pointer;
z-index: 11;
}
<form name="search">
<label class="search-box">
<!--Added the button to clear the field on click-->
<button class="btn" type="reset"></button>
<input type="text" />
<span></span>
</label>
</form>

Pure CSS Animating Mobile menu activated using radiobutton

I have a Mobile menu that should be forming X when clicked, however it would not respond and is stuck on its shape. I don't know why it acted that why, but as far as I have analyzed, this code should animate the Menu to form X when clicked. I am attaching the code for reference.
These are the CSS and HTML
/* Checkbox Hack */
input[type=checkbox] {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
/* Default State */
.divko {
background: green;
width: 400px;
height: 100px;
line-height: 100px;
color: white;
text-align: center;
}
/* Toggled State */
.myspan {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}
.myspan:first-child {
transform-origin: 0% 0%;
}
.myspan:nth-last-child(2) {
transform-origin: 0% 100%;
}
input[type=checkbox]:checked ~ .myspan {
opacity: 1;
transform: rotate(45deg) translate(-2px, -1px);
background: #232323;
}
input[type=checkbox]:checked ~ .divko {
background: red;
}
<div for="toggle-1"> <span class="myspan"></span>
<span class="myspan"></span>
<span class="myspan"></span></div>
<input type="checkbox" id="toggle-1">
<div class="divko">I'm controlled by toggle. No JavaScript!</div>
Here is the dabblet
http://dabblet.com/gist/5b4667ecc7255c228cd488c080140d95
Put the checkbox before the div I've give the .menu class to, and apply the rule on input[type=checkbox]:checked ~ .menu > .myspan.
/* Checkbox Hack */
input[type=checkbox] {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index: 2;
-webkit-touch-callout: none;
}
/* Default State */
.divko {
background: green;
width: 400px;
height: 100px;
line-height: 100px;
color: white;
text-align: center;
}
/* Toggled State */
.myspan {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: 4px 2px;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}
input[type=checkbox]:checked ~ .menu > .myspan {
opacity: 1;
background: #232323;
}
input[type=checkbox]:checked ~ .menu > .myspan:first-child {
transform: rotate(45deg);
}
input[type=checkbox]:checked ~ .menu > .myspan:nth-child(2) {
transform: translate(-100px);
}
input[type=checkbox]:checked ~ .menu > .myspan:last-child {
transform: rotate(-45deg);
}
input[type=checkbox]:checked ~ .divko {
background: red;
}
<input type="checkbox" id="toggle-1">
<div for="toggle-1" class="menu">
<span class="myspan"></span>
<span class="myspan"></span>
<span class="myspan"></span>
</div>
<div class="divko">I'm controlled by toggle. No JavaScript!</div>

radio input styling not working for firefox

So I've styled my radio input using the css below. However in Firefox, a completely different radio appears and I am unable to style it at all.
It works perfectly in safari and chrome yet I cannot seem to figure out why it isn't working in Firefox.
input[type=checkbox] {
border: 0px;
cursor: pointer;
height: 20px;
margin: 0px 10px 0px 0px;
position: relative;
overflow: hidden;
vertical-align: -5px;
width: 20px;
}
input[type=checkbox]:before {
background-color: transparent;
border: 2px solid #E0E0E0;
content: '';
display: block;
height: 20px;
left: 0px;
opacity: 1;
position: absolute;
transition: opacity .15s linear;
top: 0px;
width: 20px;
}
input[type=checkbox]:after {
border: 2px solid #EC407A;
content: '';
display: block;
height: 23px;
left: -5px;
opacity: 0;
position: absolute;
top: -9px;
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
transition: opacity .15s linear;
width: 27px;
}
input[type=checkbox]:checked:before {
opacity: 0;
}
input[type=checkbox]:checked:after {
opacity: 1;
}
input[type=checkbox]:focus {
box-shadow: none;
outline: 0px;
}
.radio-wrapper {
position: relative;
}
input[type='radio'] {
-webkit-appearance: none;
-moz-appearance: none;
-ms-appearance: none;
-o-appearance: none;
appearance: none;
position: relative;
right: 0;
bottom: 0;
left: 0;
height: 20px;
width: 20px;
-webkit-transition: all 0.15s ease-out 0s;
-moz-transition: all 0.15s ease-out 0s;
transition: all 0.15s ease-out 0s;
background: #cbd1d8;
border: none;
color: #fff;
cursor: pointer;
display: inline-block;
outline: none;
position: relative;
border-radius: 50%;
}
#-webkit-keyframes click-wave {
0% {
height: 40px;
width: 40px;
opacity: 0.35;
position: relative;
}
100% {
height: 200px;
width: 200px;
margin-left: -80px;
margin-top: -80px;
opacity: 0.0;
}
}
#-moz-keyframes click-wave {
0% {
height: 40px;
width: 40px;
opacity: 0.35;
position: relative;
}
100% {
height: 200px;
width: 200px;
margin-left: -80px;
margin-top: -80px;
opacity: 0.0;
}
}
#keyframes click-wave {
0% {
height: 40px;
width: 40px;
opacity: 0.35;
position: relative;
}
100% {
height: 200px;
width: 200px;
margin-left: -80px;
margin-top: -80px;
opacity: 0.0;
}
}
.option-input {
-webkit-transition: all 0.15s ease-out 0s;
-moz-transition: all 0.15s ease-out 0s;
transition: all 0.15s ease-out 0s;
background: #7CE1C9;
border: none;
color: #fff;
cursor: pointer;
display: inline-block;
outline: none;
position: relative;
margin-right: 0.5rem;
}
.option-input:hover {
background: #9faab7;
}
.option-input:checked {
background: #7CE1C9;
}
.option-input:checked::before {
height: 20px;
width: 20px;
position: relative;
content: '\2716';
display: inline-block;
font-size: 14.66667px;
text-align: center;
line-height: 20px;
}
.option-input:checked::after {
-webkit-animation: click-wave 0.65s;
-moz-animation: click-wave 0.65s;
animation: click-wave 0.65s;
background: #7CE1C9;
content: '';
display: block;
position: relative;
}
.option-input.radio {
border-radius: 50%;
}
.option-input.radio::after {
border-radius: 50%;
}
input[type='radio'] + label {
user-select: none;
cursor: pointer;
padding-left: 30px;
}
input[type='radio'] + label:before,
input[type='radio'] + label:after {
background: #fff;
background-clip: padding-box;
border: 2px solid #EC407A;
border-radius: 50%;
bottom: 3px;
content: '';
display: block;
height: 16px;
left: 3px;
margin-top: -8px;
position: absolute;
top: 50%;
transition: all 0.3s ease;
width: 16px;
}
input[type='radio'] + label:after {
background: #7CE1C9;
border: 2px solid #fff;
transform: scale(0);
-wekbit-transform: scale(0);
transition: all 0.3s ease;
}
input[type='radio']:focus + label:before {
box-shadow: 0 0 5px #EC407A;
}
input[type='radio']:checked + label:before {
border-color: #EC407A;
}
input[type='radio']:checked + label:after {
transform: scale(0.75, 0.75);
-webkit-transform: scale(0.75, 0.75);
}
`
<form>
<li class='initialForm'><span class='optionText'>$option1</span><strong><input type='radio' class='option-input radio option1' id='$id' value='1' name='option'></strong>
</li>
<li class='initialForm'><span class='optionText'>$option2</span><strong><input type='radio' class='option-input radio option2' id='$id' value='2' name='option'></strong>
</li>
</form>
You are using -webkit CSS prefixes, which is specifically for Chrome and Safari (or webkit based browsers).
You should ALSO use -moz CSS prefixes, specifically for Firefox. And, of course, if you want to support IE, they may have additionally or different stylings for certain methods.
Specifically for your transform method, in Firefox it is considered experimental, and perhaps -moz-transform is more appropriate.
Source: https://clarknikdelpowell.com/blog/styling-radio-buttons-select-boxes-in-firefox/
Even though it’s a part of Chrome, Safari and Firefox (-webkit-appearance, -moz-appearance), isn’t actually CSS. A lot of people use -webkit-appearance:none to reset the appearance of everything from buttons, inputs, you name it. However, there are a couple of Firefox bugs that make -moz-appearance:none a dangerous proposition.
Since appearance does not work completely on both checkboxes and radio buttons. You can still style radio buttons, you’re just going to have to use a bit of trickery in order to pull it off. Here’s how you’d do it.
Set the radio input to display: none.
Style a span adjacent to the radio button the way you’d like to style the radio button.
Toggle its appearance using the :checked attribute.
For Browser compatibility and further info see source
Toggle its appearance using the :checked attribute.
I found this blog post online and it tackles the problem you are having: https://clarknikdelpowell.com/blog/styling-radio-buttons-select-boxes-in-firefox/
edit:
In general, it's considered bad practice to style radio buttons directly. The reason you are having an issue is because -webkit-appearance, -moz-appearance is not actually CSS. Since you can't style the radio buttons directly, you have to use a bit of a work around.
Set the radio input to display: none.
Style a span adjacent to the radio button the way you’d like to style the radio button.
Toggle its appearance using the :checked attribute.
I sketched up a basic example on jsFiddle using some of the code you specified: https://jsfiddle.net/o3nurghw/2/
HTML:
<form>
<li class='initialForm'>
<span class='optionText'>$option1</span>
<input type='radio' class='option-input radio option1' id='$id' value='1' name='option' />
<span class="overlay"></span> <!-- the adjacent span -->
</li>
<li class='initialForm'>
<span class='optionText'>$option2</span>
<input type='radio' class='option-input radio option2' id='$id' value='2' name='option' />
<span class="overlay"></span> <!-- the adjacent span -->
</li>
</form>
CSS:
.initialForm {
margin-bottom: .5em;
display: block;
cursor: pointer;
font-size: 16px;
line-height: 1em;
}
.overlay {
position: relative;
right: 0;
bottom: 0;
left: 0;
height: 20px;
width: 20px;
-webkit-transition: all 0.15s ease-out 0s;
-moz-transition: all 0.15s ease-out 0s;
transition: all 0.15s ease-out 0s;
background: #cbd1d8;
border: none;
color: #fff;
cursor: pointer;
display: inline-block;
outline: none;
position: relative;
border-radius: 50%;
}
.radio {
display: none;
}
.radio:checked + span:before {
width: .75em;
height: .75em;
margin: .125em;
display: block;
content: " ";
background-color: #666;
border-radius: 100%;
}