Custom Checkbox Styling - Label jumps on check/uncheck - html

I am styling a checkbox using CSS3. Everything works fine except that the label jumps whenever I check and uncheck the checkbox. Could you please tell me why?
input[type="checkbox"]{
display:none;
}
input[type="checkbox"] + label::before{
background-color: white;
border: 1px solid #ccc;
content: '';
display: inline-block;
height: 15px;
width: 15px;
text-align: center;
margin: 0 5px -2px 0;
}
input[type="checkbox"]:checked + label::before{
content: '\2713';
}
<div>
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Check 1</label>
</div>

You could add an overflow hidden to your pseudo element to prevent the jumping effect. I also updated the css a little bit to compensate for the overflow and the fact that the arrow wasn't really centered properly in the box.
JSFIDDLE Example
Here is my take on it:
input[type="checkbox"]{
display:none;
}
input[type="checkbox"] + label::before{
background-color: white;
border: 1px solid #ccc;
content: '';
display: inline-block;
height: 22px; /*Change width and height to compensate*/
width: 22px;
text-align: center;
margin: 0 5px -2px 0;
/*Added styles*/
overflow: hidden;
top: 3px;
position: relative;
}
input[type="checkbox"]:checked + label::before{
content: '\2713';
}

You can make the position for pseudo element to absolute and place it accordingly.
Here is a solution.
div
{
padding-left:20px;
}
input[type="checkbox"]{
display:none;
}
input[type="checkbox"] + label::before{
background-color: white;
border: 1px solid #ccc;
content: '';
display: inline-block;
height: 15px;
width: 15px;
text-align: center;
margin: 0 5px -2px 0;
position:absolute;
left:10px;
}
input[type="checkbox"]:checked + label::before{
content: '\2713';
}
<div>
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Check 1</label>
</div>
There might be other attractive solutions too, this is just one.

Update
As reported by OP, there was(is?) a jumping behavior still present on demo. I added 2 more properties that might resolve the problem. I am getting the same desired results so I can't test it myself (hard to fix what you can't see).
Additional CSS
margin: 0 5px -5px 0;
line-height: 100%;
position: relative; // This should keep the input positioned in one spot.
float: left; // This should keep any extra movements of label and/or input from jumping as well.
The point of adding these 2 properties is that both of them take the element(s) out of the normal flow of the document, therefore having little to no interaction with other elements that might normally nudge or displace inputs and/or labels.
looked for that funky margin offset that'll always get added on in order to counteract that jumping behavior.
balanced out the neg and pos values
and added line-height: 100%
margin: 0 5px -5px 0;
line-height: 100%;
Also replaced the div with a fieldset it's not necessary, it just looks better. :)
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + label::before {
background-color: white;
border: 1px solid #ccc;
content: '';
display: inline-block;
height: 15px;
width: 15px;
text-align: center;
margin: 0 5px -5px 0;
line-height: 100%;
position: relative;
float: left;
}
input[type="checkbox"]:checked + label::before {
content: '\2713';
}
<fieldset>
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Check 1</label>
</fieldset>

Related

on mouse hover give tooltip only to a particular text field

I had a text field where on mouse hover I need to get some kind of tool tip which I created using some css and its fine.But the issue here is I need that only to some particular text fields but not for all.
How can I do this.
Can someone help me here
Please find the jsfiddle ,right now getting tool tip for both textfields..But need only for one.
http://fiddle.jshell.net/CvtLq/203/
Thanks
Thanks for the answers..Im adding some more where Iam getting the issue..
I had another text field where it had date picker.Now because of that css,even the date picker is getting that tooltip.
<div class="col-lg-4">
<input class="form-control" type="datetime" date-time auto-close="true" view="date" min-view="date" maxlength="10" format="dd/MM/yyyy" placeholder="renewal date" required="true">
</div>
Below is the screen shot of that
How can I prevent this coming because of that css for tooltip
Based on your jsfiddle the tooltip is created by the <span>...</span> element. You can remove this span to remove this tooltip:
span:before {
content: "";
border-style: solid;
border-width: 0 15px 15px 15px;
border-color: transparent transparent rgba(0,102,255,.5) transparent;
height: 0;
position: absolute;
top: -17px;
width: 0;
}
span {
background-color: rgba(0,102,255,.15);
border: 2px solid rgba(0,102,255,.5);
border-radius: 10px;
color: #000;
display: none;
padding: 10px;
position: relative;
}
input {
display: block
}
input:hover + span {
display: inline-block;
margin: 10px 0 0 10px
}
<input type="text">
<span>Some Text inside... </span>
<input type="text"><!--dont need on hover for this text field-->
<!--<span>please enter </span>-->
Now the tooltip is only for the first input.
Another option - add the tooltip class to the input and use this class to control which input tags show tooltip using input.show-tooltip:hover + span:
span:before {
content: "";
border-style: solid;
border-width: 0 15px 15px 15px;
border-color: transparent transparent rgba(0,102,255,.5) transparent;
height: 0;
position: absolute;
top: -17px;
width: 0;
}
span {
background-color: rgba(0,102,255,.15);
border: 2px solid rgba(0,102,255,.5);
border-radius: 10px;
color: #000;
display: none;
padding: 10px;
position: relative;
}
input {
display: block
}
input.show-tooltip:hover + span {
display: inline-block;
margin: 10px 0 0 10px
}
<input type="text" class="show-tooltip">
<span>Some Text inside... </span>
<input type="text"><!--dont need on hover for this text field-->
<span>please enter </span>
You can do this by just adding a data-attribute like data-show="tooltip" in my case, or you can use any class for the same.
Look at the snippet below:
.tooltip-container span:before {
content: "";
border-style: solid;
border-width: 0 15px 15px 15px;
border-color: transparent transparent rgba(0,102,255,.5) transparent;
height: 0;
position: absolute;
top: -17px;
width: 0;
}
.tooltip-container span {
background-color: rgba(0,102,255,.15);
border: 2px solid rgba(0,102,255,.5);
border-radius: 10px;
color: #000;
display: none;
padding: 10px;
position: relative;
}
.tooltip-container input {
display: block
}
/* Use a data-attribute or class and apply hover conditions */
.tooltip-container input[data-show="tooltip"]:hover + span {
display: inline-block;
margin: 10px 0 0 10px
}
<div class="tooltip-container">
<input type="text" data-show="tooltip">
<span>Some Text inside... </span>
<input type="text"><!--dont need on hover for this text field-->
<span>please enter </span>
</div>
Hope this helps!

CSS issue- Only partial text from the span is shown in the tool tip i have created

I am trying to create a css tool-tip, the html and css code and also link to fiddle is given below
CHECK MY CODE HERE #JSFIDDLE
HTML
<a class="tooltip" href="#">CSS Tooltips 1
<span>Tooltip1</span></a>
</br>
<a class="tooltip" href="#">CSS Tooltips
<span>Tooltip This is not working for me </span></a>
CSS
.tooltip {
position: relative;
}
.tooltip span {
position: absolute;
width:140px;
color: #FFFFFF;
background: #000000;
height: 30px;
line-height: 30px;
text-align: center;
display:none;
border-radius: 2px;
padding:2px;
}
.tooltip span:after {
content: '';
position: absolute;
top: 50%;
right: 100%;
margin-top: -8px;
width: 0; height: 0;
border-right: 8px solid #000000;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
}
.tooltip:hover span {
display: block;
opacity: 0.8;
left: 100%;
top: 50%;
margin-top: -15px;
margin-left: 15px;
z-index: 999;
}
My issue is only half the text from <span>Tooltip This is not working for me </span> is shown in the corresponding tool-tip. I tried hard but couldn't debug it. Please help.
Thanking You
It's because you have a fixed width. To allow the tooltip to dynamically expand to the content's width remove the width property and set white-space:nowrap to keep the text inline.
.tooltip span {
position: absolute;
color: #FFFFFF;
background: #000000;
white-space: nowrap;
height: 30px;
line-height: 30px;
text-align: center;
display:none;
border-radius: 2px;
padding:2px;
}
http://jsfiddle.net/89rwu2db/3/
EDIT
As commented bellow, if you want to keep the fixed width, but wants the text to expand in height, remove the height property of the span, and it will grow (also, don't use white-space anymore):
.tooltip span {
position: absolute;
color: #FFFFFF;
background: #000000;
width:140px;
line-height: 30px;
text-align: center;
display:none;
border-radius: 2px;
padding:2px;
}
http://jsfiddle.net/89rwu2db/9/
The point is, setting a specific width or height prevents your element of growing automatically.
You need to change the width property of the second tooltip to fit all the text you want display.
Fixed Fiddle: http://jsfiddle.net/89rwu2db/8/
I added styling to the second span to increase the width.
<span style="width: 250px;">Tooltip This is not working for me </span>

CSS3 - label and checkbox

HTML
<div class="select">
<div>
<label for="kitty" class="kitty-label kitty-label-1 l-center">
</label>
<input type="checkbox" name="cats" value="1">
<label>Kitty One</label>
</div>
<div class="cly-pam" style="width:50%; float: left">
<label for="kitty" class="kitty-label kitty-label-2 l-center">
</label>
<input type="checkbox" name="cats" value="2">
<label>Kitty Two</label>
</div>
</div>
<div>
css
label{
cursor: pointer;
}
.kitty-label{
position: relative;
width: 100%;
&:hover,
&:focus,
&:active{
border-radius: 6px solid #fff;
}
}
.kitty-label-1{
display: inline-block;
background: url(../img/kitty1.png) no-repeat 0 0;
height: 142px;
width: 142px;
}
.kitty-label-2{
display: inline-block;
background: url(../img/kitty2.png) no-repeat 0 0;
height: 144px;
width: 144px;
}
.select input[type="checkbox"]{
display: none;
&:checked + label:before{
background: url(../img/tick.png) no-repeat;
}
}
The labels would have background image but the issue is that when focus, active or hover, the border-radius does not appear behind the images. Also the kitty images do not have border-radius edges. Wonder if should have image in circle shape or css3 can do that?
Also checkbox seems not to show the tick or anything. Tried to click on label (as in kitty image), tick doesn't appear?
Not sure where I might go wrong. Help will be very much appreciated.
Updated
<div>
<input type="radio" name="type" value="designer" id="designer">
<label for="designer" id="designer" class="inline-block testsign" style="background: url(../img/face.png) no-repeat center;">
</label></div>
CSS
.testsign{
width: 170px;
height: 170px;
border-radius: 100%;
&:hover,
&:focus,
&:active{
border: 15px solid #f3f3f3;
}
}
// [type="radio"]:not(:checked),
// [type="radio"]:checked {
// visibility: hidden;
input[type="radio"]:checked + label:after {
content:"";
display: block;
position: absolute;
height: 60px;
width: 60px;
background: #f0f1f1 url(../img/tick.png) no-repeat center center;
border-radius: 100%;
border: 10px solid #fff;
}
Attempted the example from #misterMan
Couldn't get the label:after to be positioned at the right bottom - tried top and left to position the tick circle, but the problem is that when checked, it will appear in the position which followed top and left. So if check extra element or image, tick circle will appear in the same place which is not right. Removed the top and left. There is no way to have tick circle positioned in right bottom appearing in each label whenever radio is checked?
Also another problem is that when border radius on the label is hovered on background image, and if checked radio, the tick circle (label:after) will appear, the tick circle will be "jumpy" whenever hovered on label. How to stop the jump? I tried to add absolute center and position relative but the labels will be out of the container.
Help or insight will be appreciated.
I love this type of stuff so I made this for you, if you are still looking for a solution. I have added the images with <img> as they are not decoration, they are primary content :)
It's nice and simple, and I think does what you want. Let me know!
Updated
Have an updated fiddle!
HTML
<form>
<input type="checkbox" id="pic1" />
<label for="pic1">
<img src="http://www.placehold.it/200" />
</label>
<input type="checkbox" id="pic2" />
<label for="pic2">
<img src="http://www.placehold.it/200" />
</label>
</form>
CSS
body {
margin: 0;
}
input[type=checkbox] {
display: none;
}
label {
position: relative;
display: block;
width: 200px;
cursor: pointer;
float: left;
margin: 10px 0 10px 10px;
}
input[type=checkbox] + label img:hover {
-webkit-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
}
input[type=checkbox]:checked + label img {
-webkit-border-radius: 50px;
-moz-border-radius: 50px;
border-radius: 50px;
}
input[type=checkbox]:checked + label img:hover {
-webkit-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
}
input[type=checkbox]:checked + label:after {
content:"";
display: block;
position: absolute;
bottom: 10px;
right: 10px;
height: 50px;
width: 50px;
background: url(http://i.imgur.com/i7379jf.png) no-repeat right bottom;
background-size: 50px;
}
input[type=checkbox]:checked + label:hover:after {
}

CSS3 radio button not working in Firefox

I'm trying to style a radio button using css3 pseudo elements. In works great in chrome but it jumps back to the default styling in firefox. Any idea?
Here's a fiddle: JSFIDDLE
The HTML:
<input class="fancy" name="cc1" type="radio" checked/>
<input class="fancy" name="cc1" type="radio"/>
The CSS:
input[type="radio"].fancy:before {
content:"";
width: 24px;
height: 24px;
background-color: #1f1f1f;
display: block;
border-radius: 50%;
position: relative;
top: -6px;
left: -6px;
box-shadow: 0 1px 2px rgba(255,255,255,.1),
inset 0 2px 2px rgba(0,0,0,.8);
}
input[type="radio"].fancy:checked:before {
visibility: visible;
content:"";
width: 24px;
height: 24px;
background-color: #1f1f1f;
display: block;
border-radius: 50%;
position: relative;
top: -6px;
left: -6px;
box-shadow: 0 1px 2px rgba(255,255,255,.2),
inset 0 2px 2px rgba(0,0,0,.8);
}
input[type="radio"].fancy:checked:after {
visibility: visible;
content:"";
width: 14px;
height: 14px;
display: block;
border-radius: 50%;
position: relative;
top: -25px;
left: -1px;
background: yellowgreen;
}
I'm trying to avoid background-images
Unfortunately, what you're trying to do is not valid -- ::before and ::after do not work on input elements (any input; it's not just restricted to radio buttons).
Proof available here: http://jsfiddle.net/LvaE2/1/ -- even in the most simple case, it doesn't work.
It also doesn't work in IE or Opera. The fact that it does work in Chrome is because Chrome is going beyond the spec in allowing it.
Your best bet is to do your styling on a label element that is linked to the actual radio button using the for attribute, and then set the radio button itself to display:none;. This is how everyone else does it.
Related question here: Which elements support the ::before and ::after pseudo-elements?
Hope that helps.
CSS3 Input radio will working in Firefox: version > 80 if added style appearance
input[type=checkbox] { appearance:initial; }
Please see the Snippet:
input[type=radio] { appearance:initial; }
input[type=radio]:after{
content: "after content";
width: 100px; height: 100px; display:block;
background: red;
}
input[type=radio]:checked:after{
content: "after content";
width: 100px; height: 100px; display:block;
background: blue;
}
<input type="radio">
<input type="radio">
<input type="radio">

Can't get very small divs to center

Example: http://jsfiddle.net/nDWRm/4/
I want to make a plus and minus button using css or text or svg.
I tried this with the css and using textual + and - signs, but found those never line up. The problem is I can get it to look reasonably good on 100% zoom but when the zoom changes the left and top values also need to change to get the divs to center again. I haven't tried svg yet. I was just wondering why this doesn't seem possible.
css
#container{
height: 1.5em;
width: 1.5em;
border-radius: 5px;
border: 0.1em solid black;
cursor: pointer;
}
.vert{
top: 0.25em;
left: 0.65em;
position:absolute;
width: 0.3em;
height: 1.1em;
background-color: #424A49;
display: block;
}
.horz{
top: 0.65em;
left: 0.25em;
position:absolute;
width: 1.1em;
height: 0.3em;
background-color: #424A49;
display: block;
}
html
​
<div id="container">
<div class="vert"></div>
<div class="horz"></div>
</div>​
Using a monotype font I made this fiddle:
http://jsfiddle.net/nDWRm/17/
if you change the font-size of #container2 to for example 5em you'll see it not render correctly. And on a aesthetic note the + sign has rounded corners and the - doesn't, which is not very pleasing :P.
this fiddle shows the problem with a font very clearly: http://jsfiddle.net/nDWRm/25/
You can't just use random EM values. You have to calculate them on order to be translated to a round pixel value by the browser:
http://jsfiddle.net/meo/p7WMW/
(of course you can to it without scss, just by using http://pxtoem.com/ for example)
On js fiddle the base font size is 16px. So you need that in order to calculate your EM values.
scss
$base-font-size: 16px;
#function px2em($px, $contextPXSize : $base-font-size ){
#return ( $px / $contextPXSize ) * 1em;
}
a {
position: relative;
display: block;
margin: 2em;
width: px2em(40px); height: px2em(40px);
background: rgba(0,0,0,.1);
outline: 1px solid #000;
&:after, &:before {
content: "";
position: absolute;
background: black;
left: 50%; top: 50%;
}
&:after {
height: px2em(30px); width: px2em(4px);
margin: px2em(-15px) 0 0 px2em(-2px);
}
&:before {
height: px2em(4px); width: px2em(30px);
margin: px2em(-2px) 0 0 px2em(-15px);
}
&:hover:after {
display: none;
}
}
what would result in:
css
a{
position:relative;
display:block;
margin:2em;
width:2.5em; height:2.5em;
background:rgba(0, 0, 0, .1);
outline:1px solid #000
}
a:after,a:before{
content:"";
position:absolute;
background:#000;
left:50%; top:50%
}
a:after{
height:1.875em;
width:.25em;
margin:-.938em 0 0 -.125em
}
a:before{
height:.25em;
width:1.875em;
margin:-.125em 0 0 -.938em
}
a:hover:after{
display:none
}
​
You can use a Monotype font like Courier and a display switch in jQuery to get the effect you're after. Also using line height and text-align: center; on the text to get this to work in mulitple zooms. The inner divs use 100% height and width so will always fill the container. No positions are needed, and you can increase the font size to make the symbols bigger :)
CSS:
#container{
height: 1.5em;
width: 1.5em;
border-radius: 5px;
border: 0.1em solid black;
cursor: pointer;
font-family: Courier;
}
.vert,
.horz{
margin: 0 auto;
width: 100%;
height: 100%;
color: #424A49;
text-align: center;
line-height: 1.5em;
}
.vert
{
display: none;
}
​
http://jsfiddle.net/Kyle_Sevenoaks/nDWRm/13/