Reputation: 137
good day, I am not good at CSS and I am getting a hard time to position the checkbox label on top of checkbox itself. Can you please help me? I want it the label also to be center formatted text because I want also to change the text dynamically using jquery. If I checked it, the "Fix" text will become "Fixed" and this text should remain its centered position on the top of the checkbox. Here is my JSFIDDLE >>> https://jsfiddle.net/koykoys/d2cb96xo/ .
**Note: Please do not modify the "html" part, only the "css" part.
Thank you.
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label{
position: relative;
padding-left: 50px;
cursor: pointer;
}
/* checkbox aspect */
input[type="checkbox"]:not(:checked) + label:before,
input[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 2px;
width: 40px; height: 40px;
background: #f8f8f8;
border-radius: 3px;
border: 2px solid #aaa;
-webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
}
/* checked mark aspect */
input[type="checkbox"]:not(:checked) + label:after,
input[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 0; left: 5px;
font-size: 34px;
color: green;
transition: all .2s;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
-o-transition: all .2s;
}
/* checked mark aspect changes */
input[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
<div class="col-md-12">
<input id="checkid" type="checkbox">
<label for="checkid">Fixed</label>
</div>
Upvotes: 5
Views: 5150
Reputation: 104
Why Dont You Try something Simple....... Put your lableon top and add just a "br"
<!DOCTYPE html>
<html>
<body>
<form action="demo_form.asp" method="get"><lable>asdf</lable><br>
<input type="checkbox" name="vehicle" value="Bike"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Upvotes: 0
Reputation: 78
I added a margin-top: 50px
to force the box down and give a room for the label. Here's the complete CSS, I din't edit your original code I only added some lines. I wrote comments to the lines I added.
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
/* added */
opacity: 0;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label{
position: relative;
padding-left: 50px;
cursor: pointer;
/* added */
text-align: center;
padding-left: 4px;
}
/* checkbox aspect */
input[type="checkbox"]:not(:checked) + label:before,
input[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 2px;
width: 40px; height: 40px;
background: #f8f8f8;
border-radius: 3px;
border: 2px solid #aaa;
-webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
/* added */
margin-top: 20px;
}
/* checked mark aspect */
input[type="checkbox"]:not(:checked) + label:after,
input[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 0; left: 5px;
font-size: 34px;
color: green;
transition: all .2s;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
-o-transition: all .2s;
/* added */
margin-top: 20px;
}
/* checked mark aspect changes */
input[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
Upvotes: 1
Reputation: 8409
Simply do with some alignments remove padding and adjust position
input[type="checkbox"]:not(:checked), input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
input[type="checkbox"]:not(:checked) + label, input[type="checkbox"]:checked + label {
position: relative;
cursor: pointer;
}
/* checkbox aspect */
input[type="checkbox"]:not(:checked) + label:before, input[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left: 0;
top: 22px;
width: 40px;
height: 40px;
background: #f8f8f8;
border-radius: 3px;
border: 2px solid #aaa;
-webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
}
/* checked mark aspect */
input[type="checkbox"]:not(:checked) + label:after, input[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 16px;
left: 10px;
font-size: 34px;
color: green;
transition: all .2s;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
-o-transition: all .2s;
}
/* checked mark aspect changes */
input[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div class="col-md-12">
<input id="checkid" type="checkbox">
<label for="checkid">Fixed</label>
</div>
</body>
</html>
Upvotes: 2
Reputation: 11
Just add:
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label {
padding-top: 11px;
}
Upvotes: -1
Reputation: 56754
This will center the label above the "checkbox" (which is no longer actually an input type="checkbox"
since you are using pseudo elements on the label to visually replace it).
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label{
position: relative;
cursor: pointer;
display: inline-block;
padding-bottom: 50px;
}
/* checkbox aspect */
input[type="checkbox"]:not(:checked) + label:before,
input[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:50%; top: 20px;
width: 40px; height: 40px;
background: #f8f8f8;
border-radius: 3px;
border: 2px solid #aaa;
-webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
transform: translateX(-50%);
}
/* checked mark aspect */
input[type="checkbox"]:not(:checked) + label:after,
input[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 20px; left: 0;
right: 0;
font-size: 34px;
color: green;
transition: all .2s;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
-o-transition: all .2s;
text-align: center;
}
/* checked mark aspect changes */
input[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
<div class="col-md-12">
<input id="checkid" type="checkbox">
<label for="checkid">Fixed</label>
</div>
<div class="col-md-12">
<input id="checkid2" type="checkbox">
<label for="checkid2">Label can now vary in length</label>
</div>
Upvotes: 3
Reputation: 128791
Assuming you actually want your <label>
element on top of your checkbox (like your question states), and you're not instead wanting to vertically align your label's text like the other answers here are doing: Simply set your pseudo-elements to have a top
property set to 100%
(which will place them directly below the <label>
element), then give the <label>
element some bottom margin
to push any further elements down a little (to give space for the pseudo-elements), then set the <label>
to display
as block.
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
input[type="checkbox"]:not(:checked) + label,
input[type="checkbox"]:checked + label{
position: relative;
margin-bottom: 50px;
padding-bottom: 4px;
cursor: pointer;
display: block;
}
/* checkbox aspect */
input[type="checkbox"]:not(:checked) + label:before,
input[type="checkbox"]:checked + label:before {
content: '';
position: absolute;
left:0; top: 100%;
width: 40px; height: 40px;
background: #f8f8f8;
border-radius: 3px;
border: 2px solid #aaa;
-webkit-box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
box-shadow: groove 0 0 13px rgba(0, 0, 0, 0.5);
}
/* checked mark aspect */
input[type="checkbox"]:not(:checked) + label:after,
input[type="checkbox"]:checked + label:after {
content: '✔';
position: absolute;
top: 100%; left: 5px;
font-size: 34px;
color: green;
transition: all .2s;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
-o-transition: all .2s;
}
/* checked mark aspect changes */
input[type="checkbox"]:not(:checked) + label:after {
opacity: 0;
transform: scale(0);
}
input[type="checkbox"]:checked + label:after {
opacity: 1;
transform: scale(1);
}
<div class="col-md-12">
<input id="checkid" type="checkbox">
<label for="checkid">Fixed</label>
</div>
Upvotes: 1