I found some code that creates a slider, but I can't seem to get the movement to occur when the input is clicked on. It's actually not doing anything when activated. I'm using SASS as my preprocessor. I'm able to see the switch on the screen, but when I click to activate the slide feature, nothing happens.
HTML
<div className='prefilloptions_container'>
<div className='prefilloptions'>
<label>Defaults</label>
<input type='checkbox' name='default' value='yes' />
<span className='slider round' />
</div>
</div>
SCSS
.prefilloptions_container {
display: flex;
justify-content: center;
width: 100%;
.prefilloptions {
width: 5vw;
height: auto;
position: relative;
display: inline-block;
& input {
opacity: 0;
width: 0;
height: 0;
&:checked {
background-color: #2196f3;
}
&:focus {
box-shadow: 0 0 1px #2196f3;
}
}
label {
margin-right: 0.5vw;
}
input:checked + .slider {
background-color: #2196f3;
}
input:checked + .slider::before {
transform: translateX(26px);
}
input:focus + .slider {
box-shadow: 0 0 1px #2196f3;
}
.slider {
position: absolute;
cursor: pointer;
top: -3px;
left: 70px;
right: 0;
bottom: 0;
background-color: #ccc;
transition: 0.4s;
width: 2vw;
height: 1.7vh;
&::before {
position: absolute;
content: '';
height: 15px;
width: 15px;
left: 4px;
bottom: 4px;
background-color: white;
transition: 0.4s;
}
&.round {
border-radius: 34px;
width: 1.7vw;
&::before {
border-radius: 50%;
}
}
}
}
}
Looks like I was able to figure this out. Apparently, I needed to surround the input and span elements with label element rather than having the label element on it's own line.
Old Code
<div className='prefilloptions_container'>
<div className='prefilloptions'>
<label>Defaults</label>
<input type='checkbox' />
<span className='slider round' onClick={alertMe} />
</div>
</div>
New Code
<div className='prefilloptions_container'>
<div className='prefilloptions'>
<label>
Defaults
<input type='checkbox' />
<span className='slider round' onClick={alertMe} />
</label>
</div>
</div>
Mabye this HTML-slider will help you:
<input type="range" min="1" max="100" value="50">
Related
I have a slider that goes from 1 to 12. I would like the thumb to perfectly align with all the numbers above it when I move it up and down.
This is what I have so far, but as you can see, it is still slightly off.
Is there a way to achieve this?
body {
height: 100vh;
display: flex;
}
.container {
margin: auto;
width: 400px;
}
span {
display: block;
width: 1ch;
}
.numbers {
display: flex;
justify-content: space-between;
padding-left: .4em;
}
<div class="container">
<div class="numbers">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span>
</div>
<input style="width: 100%;" type="range" min="1" max="12" name="" id="" />
</div>
We can configure the number spans so they all have the same width and center text alignment. By setting their initial width to zero we negate the effect of their contents, which vary in size (1 vs 10, for example).
We can use flexbox on the slider as well, giving it the same flex-grow value as twice our 12 numbered spans. This lets us use half-widths for spacing. We add a span on each side of it with a value of half that of the numbered spans, shifting things over properly.
Finally, some margin tweaks get things pixel-perfect (in Chrome, at least).
Note that I've changed the primary width to a relative value so you can see that this works at any size. Have a look at the full screen demo.
The primary drawback here is that we have quantities hard-coded in the CSS. If the slider range changes, so must the CSS. Ideally we'd find a fully flexible solution.
If I'm not making sense, have a look at https://css-tricks.com/snippets/css/a-guide-to-flexbox.
body {
height: 100vh;
display: flex;
}
.container {
margin: auto;
width: 75vw;
}
.numbers,
.slider {
display: flex;
justify-content: space-between;
}
.numbers span,
.slider span {
width: 0;
flex-grow: 1;
text-align: center;
}
.slider span {
flex-grow: 1;
}
.slider input {
flex-grow: 24;
margin-left: -2px; /* account for UI sizing */
margin-right: -2px; /* account for UI sizing */
}
<div class="container">
<div class="numbers">
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span>
</div>
<div class="slider">
<span><!-- half --></span>
<input type="range" min="1" max="12" name="" value="1" id="" />
<span><!-- half --></span>
</div>
</div>
I think they can't be easily arranged, according to the idea you have to play with a margin left right or width : 0%; etc ...
If you want it is an alternative
const range = document.getElementById("range"),
rangeV = document.getElementById("rangeV"),
setValue = () => {
const newValue = Number(((range.value - range.min) * 100) / (range.max - range.min)),
newPosition = 10 - newValue * 0.2;
rangeV.innerHTML = `<span>${range.value}</span>`;
rangeV.style.left = `calc(${newValue}% + (${newPosition}px))`;
};
document.addEventListener("DOMContentLoaded", setValue);
range.addEventListener("input", setValue);
body {
min-height: 100vh;
padding: 0 10vh;
margin: 0;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
input[type="range"] {
-webkit-appearance: none;
margin: 20px 0;
width: 100%;
}
input[type="range"]:focus {
outline: none;
}
input[type="range"]::-webkit-slider-runnable-track {
width: 100%;
height: 4px;
cursor: pointer;
animate: 0.2s;
background: #03a9f4;
border-radius: 25px;
}
input[type="range"]::-webkit-slider-thumb {
height: 20px;
width: 20px;
border-radius: 50%;
background: #fff;
box-shadow: 0 0 4px 0 rgba(0, 0, 0, 1);
cursor: pointer;
-webkit-appearance: none;
margin-top: -8px;
}
input[type="range"]:focus::-webkit-slider-runnable-track {
background: #03a9f4;
}
.range-wrap {
width: 500px;
position: relative;
}
.range-value {
position: absolute;
top: -50%;
}
.range-value span {
width: 30px;
height: 24px;
line-height: 24px;
text-align: center;
background: #03a9f4;
color: #fff;
font-size: 12px;
display: block;
position: absolute;
left: 50%;
transform: translate(-50%, 0);
border-radius: 6px;
}
.range-value span:before {
content: "";
position: absolute;
width: 0;
height: 0;
border-top: 10px solid #03a9f4;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
top: 100%;
left: 50%;
margin-left: -5px;
margin-top: -1px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Input Range with Dynamic Label</title>
</head>
<body>
<div class="range-wrap">
<div class="range-value" id="rangeV"></div>
<input id="range" type="range" min="1" max="12" value="1" step="1" />
</div>
</body>
</html>
I have to make a file input field where a user has to click to open the files explorer and select his résumé. It should look like this:
Here is so far what I got:
<input type="file" name="mobile_num" placeholder="Attach Resume" required>
File input is one of those html elements that it is hard to control the style, every browser has its particular way with it. But there are some techniques to use, and here is one of them :
.group {
position: relative;
}
.group .btn {
pointer-events: none;
text-align: left;
background-color: transparent;
border: 1px solid gray;
padding: 0.5rem;
width: 100%;
border-raduis: 5px;
}
.group input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
}
<div class="group">
<button class="btn">Attach Resume</button>
<input type="file" name="mobile_num" placeholder="Attach Resume" required>
</div>
Add label element and use for attribute will resolve your issue. I also update code snippet, I hope it'll help you out. Thank you
.group {
position: relative;
}
.group .btn {
pointer-events: none;
text-align: left;
background-color: transparent;
border: 1px solid gray;
padding: 0.5rem;
width: 100%;
border-raduis: 5px;
}
.group input {
display: none;
}
.group label {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
}
<div class="group">
<button class="btn">Attach Resume</button>
<label for="mobile_num"></label>
<input id="mobile_num" type="file" name="mobile_num" placeholder="Attach Resume" required>
</div>
Step 1: Add a label tag as the parent of your file input
Step 2: Add some CSS to hide the default file input and display the
label as per your reference image
.file_input_btn {
display: block;
border: 1px solid grey;
border-radius: 5px;
width: 100%;
height: 45px;
font-size: 16px;
color: grey;
line-height: 45px;
padding-left: 10px;
}
.file_input_btn input {
display: none
}
<label for="file_input" class="file_input_btn">
Attach Resume
<input type="file" id="file_input" name="mobile_num" required>
</label>
I've tried to simplify my problem into the below:
.wrapper {
position: relative;
display: inline-flex;
min-height: 1.5rem;
border: 1px solid black;
margin-right: 1rem;
padding-right: 1.5rem;
}
.input {
position: absolute;
z-index: -1;
opactiy: 0;
}
.label-wrapper {
margin-left: 2rem;
}
.label {
position: relative;
margin-bottom: 0;
}
.label:before {
position: absolute;
top: 4px;
left: -24px;
display: block;
width: 20px;
height: 20px;
pointer-events: none;
content: "";
background-color: #fff;
}
.label:after {
position: absolute;
top: 4px;
left: -24px;
display: block;
height: 40px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 50% 50%;
}
<div class="wrapper">
<input class="input" type="checkbox"></input>
<label class="label">
<div class="label-wrapper">
Option 1
</div>
</label>
</div>
In addition to this, I have the following CSS (in a StyledComponent) that adds a "check" to the checkbox when the user clicks on the hidden input in the .input:
&[type="checkbox"]:checked ~ label::after {
background-image: url("${checkboxImage}");
top: -2px;
left: -29px;
width: 30px;
height: 30px;
}
This means it behaves as a checkbox should - however, I want to swap my elements around so that the checkbox sits to the right of the text instead of the left, to do this I have tried re-ordering the input so that it comes after the label but then the hidden input (which is handling the actual click behaviour is disjointed from the CSS checkbox? Can anyone point me in the direction of how I might resolve this?
Summary:
Using :before & :after to create a visual checkbox, but the logic for that checkbox is actually handled by input (which is hidden) - how can I move both the visual & hidden input to the right of the text?
Edit:
Its very simple with display: flex and flex-direction: row-reverse.
.label-wrapper {
margin-left: 24px;
margin-right: 0;
flex-grow: 1; /* ADDED THIS */
}
.label {
position: relative;
margin-bottom: 0;
display:flex;
flex-direction: row;
flex: 1; /* ADDED THIS */
}
.label.reverse{
flex-direction: row-reverse;
}
See the Snippet below:
.wrapper {
position: relative;
display: inline-flex;
min-height: 1.5rem;
border: 1px solid black;
margin-right: 1rem;
padding: 0.5rem;
width: 200px;
}
.input {
position: absolute;
z-index: -1;
visibility: hidden;
}
.label-wrapper {
margin-left: 24px;
margin-right: 0;
flex-grow: 1;
}
.label.reverse > .label-wrapper {
margin-left: 0;
margin-right: 24px;
}
.label {
position: relative;
margin-bottom: 0;
display:flex;
flex-direction: row;
flex: 1;
}
.label.reverse{
flex-direction: row-reverse;
}
.label:before {
position:absolute;
display: block;
width: 20px;
height: 20px;
content: "";
background-color: #fff;
border: 1px solid black;
border-radius:4px;
}
.label:after {
position: absolute;
display: block;
height: 20px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 80%;
}
input[type="checkbox"]:checked ~ .label::after {
background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
width: 20px;
height: 20px;
}
<div class="wrapper">
<input class="input input1" name="checkbox1" id="checkbox1" type="checkbox">
<label class="label" for="checkbox1">
<div class="label-wrapper">
Option 1
</div>
</label>
</div>
<div class="wrapper">
<input class="input input2" name="checkbox2" id="checkbox2" type="checkbox">
<label class="label reverse" for="checkbox2">
<div class="label-wrapper">
Option 2
</div>
</label>
</div>
Are you trying to change the left to right default position of elements?
If that's the case you can use text-align or float. Or even flexbox.
Also, put the text inside a span or something so you can control it.
If I have understood you correctly, there are many ways based on context like using absolute positioning, or put the label before input and close it. or something like this:
<label class="">
<span class="">text</span>
<input class="" type="checkbox" name="order">
</label>
I'm trying to get the behavior of <input type="search"> on regular <input type="text">.
I tried to use the appearance: searchfield; which didn't applied this special behavior.
How I want it to look:
CodePen Example (Tested on Chrome Browser, and it doesn't work)
Searching for a pure CSS/HTML solution
Demo: https://codepen.io/vsync/pen/MWjdbgL
Presented here three ways of solving the issue:
1️⃣ When the input element has no focus, its type is set to search, which then shows the x (clear button), and when the element has focus - its type is set to text
<input type="search"
onmouseup="this._timer=setTimeout(el=>{el.type='text'},0,this)"
onblur="clearTimeout(this._timer);this.type='search'"
/>
2️⃣ Wrap the input with a <form> element so it could be cleared using native <button type='clear'>
input{ padding:.5em; font:1em Arial;}
input[type='text']:placeholder-shown + button{ opacity:0; pointer-events:none;}
form{ display:inline-block; position: relative; }
form:hover input[type='text']:not(:placeholder-shown) + button{ opacity: 1 }
form button{
--size: 18px;
position: absolute;
border: none;
display: block;
width: var(--size);
height: var(--size);
line-height: var(--size);
font-size: calc(var(--size) - 3px);
border-radius: 50%;
top: 0;
bottom: 0;
right: calc(var(--size)/2);
margin: auto;
background-color: salmon;
color: white;
padding: 0;
outline: none;
cursor: pointer;
opacity: 0;
transition: .1s;
}
<form>
<input type='text' placeholder=' ' />
<button type='reset'>×</button>
</form>
3️⃣ Use a non-<form> wrapper and javascript
input{ padding:.5em; font:1em Arial; }
input[type='text']:placeholder-shown + button{ opacity:0; pointer-events:none;}
.inputWrap{ display:inline-block; position: relative; }
.inputWrap:hover input[type='text']:not(:placeholder-shown) + button{ opacity: 1 }
.inputWrap button{
--size: 18px;
position: absolute;
border: none;
display: block;
width: var(--size);
height: var(--size);
line-height: var(--size);
font-size: calc(var(--size) - 3px);
border-radius: 50%;
top: 0;
bottom: 0;
right: calc(var(--size)/2);
margin: auto;
background-color: salmon;
color: white;
padding: 0;
outline: none;
cursor: pointer;
opacity: 0;
transition: .1s;
}
<div class='inputWrap'>
<input type='text' placeholder=' ' />
<button type='reset' onclick='this.previousElementSibling.value=""'>×</button>
</div>
<html>
<head>
<style>
.border-0{
border: none;
}
.search-border{
border: 1px solid;
border-radius: 5px;
width: 200px;
display: flex;
align-items: center;
}
</style>
</head>
<body>
<div class="search-border">
<input type="text" placeholder="Search" class="border-0">
<img src="/assets/close-icon-light.svg">
</div>
</body>
</html>
Hi,
Please try this code if this satisfies you. Use width of your choice for the search box. Also use a suitable close icon.
I am trying to change the default 'box image' of the checkbox with CSS, but it is not working. Is there any way around this?
.class_checkbox{
background: url("../images/button_bullet_normal.png") no-repeat scroll 0 0 transparent;
}
<input type="checkbox" class="class_checkbox">
You can use pure css, just add a label to the checkbox like this:
.check_box {
display:none;
}
.check_box + label{
background:url('images/check-box.png') no-repeat;
height: 16px;
width: 16px;
display:inline-block;
padding: 0 0 0 0px;
}
.check_box:checked + label{
background:url('images/check-box-checked.png') no-repeat;
height: 16px;
width: 16px;
display:inline-block;
padding: 0 0 0 0px;
}
Example HTML:
.check_box {
display:none;
}
.check_box + label{
background:url('images/check-box.png') no-repeat;
height: 16px;
width: 16px;
display:inline-block;
padding: 0 0 0 0px;
}
.check_box:checked + label{
background:url('images/check-box-checked.png') no-repeat;
height: 16px;
width: 16px;
display:inline-block;
padding: 0 0 0 0px;
}
<input type="checkbox" class="check_box" id="checkbox1">
<label for="checkbox1">
Try:
<input type="checkbox" class="input_class_checkbox">
jQuery
$('.input_class_checkbox').each(function(){
$(this).hide().after('<div class="class_checkbox" />');
});
$('.class_checkbox').on('click',function(){
$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))
});
Fiddle:
http://jsfiddle.net/cn6kn/
$('.input_class_checkbox').each(function(){
$(this).hide().after('<div class="class_checkbox" />');
});
$('.class_checkbox').on('click',function(){
$(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))
});
.class_checkbox {
width: 20px;
height: 20px;
background-color: red;
}
.class_checkbox.checked {
background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="input_class_checkbox">
I created a Fiddle using pure CSS.
The most voted answer doesn't handle click events and won't work well, because the checkbox value won't change.
This is my example:
http://jsfiddle.net/kEHGN/1/
Basically, we need the following html:
<div class="checkbox_wrapper">
<input type="checkbox" />
<label></label>
</div>
And the following CSS:
.checkbox_wrapper{
position: relative;
height: 16px;
width: 17px;
}
input[type="checkbox"] {
opacity:0;
height: 16px;
width: 17px;
position: absolute;
top: 0;
left: 0;
z-index: 2;
}
input[type="checkbox"] + label{
background:url('../images/unchecked.png') no-repeat;
height: 16px;
width: 17px;
display:inline-block;
padding: 0 0 0 0px;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
input[type="checkbox"]:checked + label{
background:url('../images/checked.png') no-repeat;
height: 16px;
width: 17px;
display:inline-block;
padding: 0 0 0 0px;
}
Just check the routes of the images, and the widths and heights should be equal to the width and height of the images.
On the fiddler I am using base64 encoded images.
I hope it can be useful.
I found another way, without using adjacent labels or surrounding divs.
My usecase was that I had a markdown parser that generates those nifty TODO lists and I wanted to style them. Changing the generated HTML wasn't a option, so I came up with this solution:
given a checkbox like this:
<input type="checkbox" id="cb">
You can style it with visibility: hidden on checkbox and visibility: visible on ::after, like this:
#cb {
visibility: hidden;
}
input#cb::after {
visibility: visible;
content: "F";
color: red;
background: green;
padding: 8px;
}
input#cb:checked::after {
content: " T ";
color: green;
background: red;
}
<!doctype html>
<html>
<body>
<input type="checkbox" id="cb">
</body>
</html>
EDIT:
#connexo in a comment pointed out that input elements cannot have content. It could be easly done using label element, for example like so (please someone correct me if this is wrong, I don't have non-webkit browser handy to test it).
#cb-span * {
visibility: hidden;
}
input#cb + label::after {
visibility: visible;
content: "F";
color: red;
background: green;
padding: 8px;
}
input#cb:checked + label::after {
content: " T ";
color: green;
background: red;
}
<!doctype html>
<html>
<body>
<span id="cb-span">
<input type="checkbox" id="cb">
<label for="cb"></label>
</span>
</body>
</html>
The checkbox works just like normal one and you can style it anyway you like.
Maybe it'll help someody (and I can bookmark it, because I haven't found anything like this on the web)
Maybe will be useful my sample with LESS.
<div class="custom-checkbox">
<input component="input" type="checkbox"/>
<label>Here is caption right to checkbox</label>
</div>
#imgsize: 25px;
.custom-checkbox{
position: relative;
> input[type="checkbox"] {
display:none;
position: absolute;
top: 0;
left: 0;
+ label{
background:url('../img/unchecked.png') no-repeat 0 0;
background-size:#imgsize;
height: #imgsize;
padding: 4px 0 5px 35px;
display: inline-block;
-webkit-transition-duration: 0.3s;
-moz-transition-duration: 0.3s;
transition-duration: 0.3s;
}
}
> input[type="checkbox"]:checked + label{
background:url('../img/checked.png') no-repeat;
background-size:#imgsize #imgsize;
}
}