Syntax Video
How To Make Amazing Range Inputs With CSS
Transcript
I'm going to show you how you can turn this into this with no libraries, no frameworks, mostly CSS, and a sprinkle of JavaScript. So, let's take a look at what we have. We're using an input type of range. Now, the range input in HTML is a little quirky, and we're going to dive into some of those quirks in this video, but as you can see, we basically just have an input slider. And as far as the CSS goes, we have some basic CSS to get us started, but nothing crazy. We're using color scheme light dark, so that way you're not staring at a light mode website. And we're also setting a couple of values. We're using the light dark function to set the foreground and background to white and black and swap them back and forth. And we also have our basic yellow color saved. You don't need the yellow. You don't need any of this stuff. All you need is the range input. And let's get going. So the first thing we want to do is make this thing vertical. Now, many people might reach for rotation or something like that to
get this to go vertical, but you can actually do it nice and easy using writing mode. So, let's go ahead and target the input. First, we're going to say input, and the type is going to be range. So, by targeting the input type of range, we're going to now say writing mode, and the writing mode is going to be vertical, and then LR. Now if we save this you can see that it actually kind of goes from the top down. You saw that there might have been another option which was RL. If we save that doesn't do it. So we need vertical LR as in vertical left to right. But then we also need direction as a property. The direction property is going to say RTL as in right to left. And we can save this. And now our input goes the correct way. Now, some of the trickiness is going to be getting these lines, but also the fact that we need progress on those lines. Now, if you haven't customized the range input before, you might not know that this thing is
quirky. There's all kinds of quirks, whether that's browser prefix hacks or various things here, but you may have seen these ticks and thought, "Ooh, the little ticks here, the little lines." Maybe maybe you were extra smart and thought, I could use a data list for this. And so you would head to the documentation and you'd see the data list, right? It looks like a perfect fit. Unfortunately, like most things in the range input, you can't style these lines. So even though we could get really really close just by having these lines here and a vertical input, using data list is not going to fly here. So that's when we need to reach for real custom styling. Now, there's a property you're going to be seeing used a lot in this video, which is appearance none.
This removes some of the default styling on this range. You can see when I apply this to the range, it immediately removes some of that. Now, this gets us a little bit there, but by no means is it going to get us all of the way there. So, what we want to do is next we want to style the thumb, as it's called, the little handle. I don't know why it's called a thumb, but it's called a thumb.
Now, to style this thing, you need to use a browser prefix. And what we want is to do ampersand colon- WebKit slider thumb. Now, you might not know this, but Chrome was actually WebKit for a long time before they moved to their own rendering engine. So, even though Safari is the main WebKit browser available, Chrome also has a ton of these WebKit prefixes still within their rendering engine that are supported.
Now, there's also a Mosilla one, which we'll let you do on your own time, I'll have it in my final code, but for the sake of brevity, we're just going to use the WebKit one in this demo. So, you might come in here and want to give this a background. Maybe a background color of something like red, right? Well, sadly, you can see that nothing happens when you go to style this. That's because we also need an appearance none on the thumb. When we do that, it goes away entirely. That's to be expected.
What we'll want to do is assign this thing an actual explicit width and height. I'm going to give this a width of 40 pixels. And then I'm going to give it a height of 18 pixels. These are just values that I've come up with. You can customize this to your heart's content. And if you're building this as a component, make these props. Right?
So, as you can see, that not only gives us a thumb, but it also extends the width of the overall track. And this could prove to be an issue for us if we're going to be styling the track and having the thumb wider than the track because no matter what you do here, extending the width of this thing is always going to push the track wider.
So, a solution to that is to use the scale property. We can scale this bad boy by 1.4 on the X dimension, keep it one on the Y, and then now the thumb extends beyond the track. Okay, we're starting to get a little bit closer. There's still some major pitfalls here, and we'll hit them in just one second, but before we do that, let's get this looking nice here with the black line in the middle. Now, there's a number of ways you could approach this black line in the middle with the border radius here. And I'm going to propose that I think this is the best way to do it. And oftentimes, I think this is an overlooked technique, which you will see over the course of this video using uh the same technique. That technique of course is linear gradients. Now linear gradients uh can be used for all kinds of stuff that aren't just a classic style gradient including patterns and lines. So if we change this background from red to a linear gradient, we'll
want to say that this thing goes to the bottom. And so we're starting at the top going to the bottom. And we're going to give this some values. Now I'm going to put these on their own line here just to give us some reading room. And we're going to say the first value is going to be vary yellow. So we're starting at yellow and we're going to go to about what? Well, we're going to go about 50% of the way, but we want the line to be two pixels. So we want to 50% of the way up one pixel. Uh so let's go ahead and do a calc. We'll say calc and we'll say 50% minus one pix. And so we need to go from zero to 50% minus1. That's the first part of the gradient. The second part of the gradient is going to be black. So we can say vary black and this is going to go from where?
And this is going to go from that amount which is 50% minus one going to go to 50% + 1. Okay. And then for the last value, it's going to be the exact same as the first, except for we're going to go 50% plus one. And then we're going to wrap it up with 100%.
We can save this. And you can see we now have a line with a black line dead in the center using just a little bit of calc and using hard stops on a linear gradient. That's really great. We can throw a border radius of like four pixels on this bad boy and it's going to be looking really good. Cool. We're looking really close. Sadly, I think most of the difficult challenges in this input are yet to come. So, let's talk about these black lines here. If we're thinking about doing these lines, I think initially you might have approached this in all kinds of different ways. Some people might go with after elements, uh, pseudo elements. Some people might just throw a bunch of spans in there and call it a day. There's a number of ways that you could do this, but I'm going to propose a CSSonly solution that affects on the range track itself, and that's also going to be using a linear gradient. So, what we need to do is we need to add
another pseudo element, which is the a. So, we'll say amperand colon and then hyphen, and we're going to target the WebKit slider runnable track. Again, there's a Mozilla equivalent to this that we are not including in this video, but will be in the final code. So, we have this and we have the WebKit slider runnable track. And what we want to do to this is we want to give this a background. Now, let's go ahead and we'll want to do a background. And this time, instead of a linear gradient, we're going to be using a CSS function that you may not know about, which is which is repeating linear gradient. That's right. Repeating linear gradient allows you to define a gradient and then as a background image that repeats, it will just repeat itself as the name suggests. So, when we create a repeating linear gradient, we're going to want to do the same thing where we start from top to bottom. And then we're
going to say, what should these lines look like? Well, a single line for right now is going to be fff. And it's going to go from zero to like two pixels because we want this to be two pixels tall. Then we want a space of however much transparency because you could come in here and you could say transparent and then this value could be something like okay start at 2 pixels and go for 10 pixels. Okay, when we save this it works. We get these repeating lines.
This is pretty close. Now there is still a gray background on here but we'll solve that in just one second. But more importantly, how do we make this more dynamic, more interesting, and more configurable? I'm going to go ahead and make a new CSS variable, and this is going to be lines. This is how many lines we would like this thing to have.
I'm going to say we would like it to have five lines. Now, we're going to be using this lines variable to do just a little bit of math on this value. The math to make that happen is with a calc. We're going to be saying, all right, we want 100% minus 2 pixels. Okay, we want it to go to the end of this repeating with that two pixels being the width of the lines.
You could make that a variable as well if you'd like that to be configurable. Then we're going to divide by and we'll say vary lines and then we'll save this. Now you can see we said we wanted five lines and it's given us 1 2 3 4 5 6. So if we want this to be the exact number of lines, we'll want to throw a paren around here and subtract one. So if we wanted five lines, this is now golden. The reason why this works is we are saying hey go if if we were to say hey just go to 100% of this it gives us one line. If we were to say 100% of this divided by the amount of lines, then we get a little bit closer. So then you add in maybe you divide it by the amount of lines. That gives you the correct amount of spacing.
But if we want a line to be at the bottom, we need to do the subtracting by the width of the line like this. So it's some basic math here, but that now gives us the top and bottom lines exactly where we want them. Now, to get rid of this gray background, we actually need to come all the way back up to the range input itself and give this a background of transparent.
Now that we have that, you can see we're cruising. In fact, this is really close. However, there's one major issue with this. And this is a major issue that today with CSS and HTML, even if you're only supporting the latest and greatest browsers, you can't fix this without JavaScript. And that is when you abandon the appearance of a range input, you lose the ability to style the progress meter of the input.
So in a default range input, there's the section uh below the range and above the range. And the below the range has a color to it. Now, sadly, we lose that the moment you lose appearance, the moment you add appearance, none. And can you style that? Yeah, you can change its color. Uh, cool. That's not really that great though. Now, I only want this to be filling the lines, but I want it to be dynamic here. So, we are going to have to bail out to do some JavaScript.
Now, you could do this all in your front-end framework of choice. So, if you are working in spelt or view or whatever, you can just do that in your component code, right? And that should be fairly trivial. But to keep this really simple, we're going straight vanilla JavaScript. So I'm going to make a script tag. We're also going to need a couple of things. We're going to need a wrapper around this. So we're going to add a range wrapper around this. And that's going to cover the label itself as well as the input. Okay. We're also going to want a range wrapper, believe it or not. Look at this naming. And this is going to cover the input range. Now, we're also going to want another HTML element. I'm going to choose to use a div, and this is going to be the range value. This is going to be the thing that actually moves up and down with JavaScript. Okay, so let's go ahead and write our script.
We're first going to target a couple of items here with document.query selector. We're going to get the range itself. So, we're going to say the input is equal to document dot query selector. And this is going to allow us to select something based on its ID. We have an ID of range and we're going to grab that like so. We're also going to want to output the value like we see here. Now, this can only be done with JavaScript right now. I'm going to add a span and I'm going to give this an ID of value for right now. Okay, let's go ahead and now do document query selector for the value value as well. We want this value to be populated on load. So we can say value.ext content is equal to input.
You can see we now have our value being output right here. Now, if I want these on separate lines like my demo, just use a break tag. Simple enough. You could also make this span uh display block. But why? We could just text line center this all. Now, we need to listen for an event on this thing. So, when this moves up and down, we want this value to change. We're going to need that value not only in our CSS, but as well as to get it output on the screen. So, we're going to say input.addeventlister.
And the event that we're going to listen for is on input. This is going to get access to the event itself. And here we go. Now I'm going to set a let current value is equal to e.target value just because we're going to need to use this value a number of times. And then I'm going to update the value.ext content with the current value.
We should now have a dynamic working input. Nice. Super cool. Now, typically with these inputs, you'll have a min and max to this thing. And this one doesn't have a min or max value. So, we can make this dynamic, but I think we should say the minimum value here is going to be equal to zero and the maximum value, let's say 200. So, this can now go up to 200. There we go. Down to zero. Now, from this, what we're going to want to do is we're going to want to grab the percentage in which it is changing. So, what is the percentage at which this is filled up? You could tell that would be the current value divided by the max. So we could say let progress is equal to the current value divided by and this is
comes from the input domax. Now that's really great. We now have the percentage and you might reach for animating height or even scale of what will be this range value. Right? So the idea here might be that you could come in and you could say all right what do we what do we have we have the range wrapper range wrapper the range wrapper needs a position relative and then we have the range value and the range value has a position of absolute right this thing needs to fill the space it also needs to have an inset of zero. So that way it's pin left, top, right, bottom. And then we're going to give this a background of yellow.
Now you can see it fills the space here. That's because the range wrapper is display block. You could say display inline block and that fixes it as well. Now you could give this an assigned width if you wanted to or you could say within our range div the label is going to be display block. And if we want the whole thing to be centered in its container since it's display inline block you can do text align center on the range the range class itself like this. Cool. So we're really close. Now there's a number of things wrong with this one. We can no longer grab the handle because of the range value. Let's throw a pointer events none on here.
Since this is just purely display, let's go ahead and add a query selector and get access to the range value. We'll select the class of range value and then be able to set the range value dotstyle dot height. And that could be equal to the progress times 100 plus a percentage like this. And this would actually get you kind of close. Now, it fills it up from the top down. Uh it's very kind of weird. This strategy is not going to work for a number of reasons. So you would say okay what if we did scale and we changed the scale we changed the transform origin and it's going up and down. Now that would work if this was
how you wanted to do this. If you wanted to, we could say for the range value transform origin. The transform origin could be 50% 100%. And then affect instead of affecting the height, we could do the scale. And then the property values here would be first and foremost we're not scaling we're not scaling the X at all but we are going to scale the Y and we no longer need this progress. Okay, so this actually might feel a little bit closer. Check it out. This actually is quite a bit closer. Now we do need to move it before this is affected. So let's add a function here which is going to be update progress. And this is going to take in a current it's going to take in a current value here. And what this will do is it
will first calculate the progress based on the current value. Then it will set the scale like this. We can bring this out of here. We can bring this out of here and we can run update progress with the current value and then we can now also use this function right here. We can do so where the current value is the input value.
Save this. And now it's working pretty good. Now I say this is really really close. If if you're happy with this, this is not a bad input right here. And in fact, this might even be more usable than the one with the colored lines. But the colored lines has a lot of really neat, interesting techniques. Now, if you were keeping these lines to be white, you could actually use a mix blend mode to get these to turn yellow, and that would be a great solution. The mix blend mode solution is really great.
But if you wanted those lines to be any other color, and you wanted the ones to be yellow, then that solution falls apart completely. really inflexible but really easy comparatively to what we're going to do. Now, let's actually show you a real working solution. Now, that working solution is to also use a linear gradient. Now, the linear gradient for this, if you think about it, is really the same as the one on the slider track because if we come in here, instead of saying the background is yellow, if we say the linear gradient is going to go from far, uh, and instead of having it be white, it's now yellow. You can see we kind of get yellow lines that kind of kind of fill the space, but when we scale they completely get shrunk in the wrong way. Not only that, the width is slightly off as well. Now, the reason why these lines are slightly too wide is because range actually has a bit of margin on it by default as an input. So,
if we put margin zero on the range, we now have lines that when at 100% fill this thing up completely. But that little bit shows a critical flaw. When we are scaling this thing up and down, these lines are always going to scale. Animating the height's not going to work either. You could do some stuff with a window, another wrapping div, and overflow hidden and making that. But then you get into all kinds of other issues. I have a solution that is much easier and much better, but it does use a technique that's often forgot about, and that is image masking. Now you can mask with a linear gradient and then instead of animating the size of the div, you're just changing the position of the gradient mask itself. So let's go ahead and see this thing in action. I think this is a really neat te technique. We can update this and instead of setting our scale property right here, what we're going to be doing is using this same progress value to set
the position in the gradient of the image mask. So the property that we're modifying is no longer scale, but it's going to be mask image. So we're going to say mask image like this is equal to and then we're going to say it's equal to let's use back tick for string interpolation here. And let's say linear gradient. Now this linear gradient might be hard to picture. So I am going to briefly change this mask image property to be background just so we can visualize it a little bit better. Now with our other linear gradients we've been going top to bottom but if you think about it we want this to control from the bottom to the top. So we can say to top instead just to keep this easy. And then from there we're going to want to say this starts at black and will go all the way and starting at zero will then end up going to progress.
and then progress times 100 because we need a percentage here. So then outside of that we can say percentage. So black from zero to the current progress percentage then comma we're going to have a hard stop of transparent and that transparent is going to be the same deal. It's going to be at 100 times progress and then we also need the percentage sign after this. So let's take a look and see what this gives us. So if we have our line here, you can now see it hovers right about where we are at as we go all the way to the top. Now this is really great. Now, now you will notice some zindex issues here as this kind of follows along with us. Now the solution to that can be done a number of ways.
none of which involve z-index because you end up having a weird stacking order here where the thumb and the track uh and then you have another div. Z-index isn't going to be your friend. But since this is just going to mask, all we need to make sure is that this black box never crosss paths with this black line, which you can see it does. The easiest way to do that would just be to tell this to lag behind by a couple of pixels or even in this case percentage. So we could always wrap this progress. You could always subtracted by something like 8 for the first value and then for the second value subtract it by something like six. Now this isn't a perfect solution because you can see at the very beginning there'll be a second when it's not. But it does give you the option to do even some more gradient stuff if you want to do that. Now all we need to do from here is to change the
style background to do B back to mask image. Now that we have this visualized and the next thing you know we have this range input that works, feels nice and fills up the space. Now we learned a lot of really interesting techniques here. A lot of cool stuff that you can do with this. The thing I think is cool about this technique is that so much of this is configurable. If we want to change these lines to red, they can be red. If we want to change these lines to blue, they can be blue. Nothing breaks. We're not relying on that type of technology.
Using these masks is really great. In fact, in fact, I'm going to move this down to something like RGB. We'll do 255, 255, 255. Make it slightly transparent by a lot transparent. And now we have a demo that looks more closely to what I had before. You could throw on this entire thing a font family monospace.
Throw a text transform uppercase and then throw a margin block end on this thing of one rem and we're pretty much there. You could control the colors on this thing just by changing a simple CSS var and it is fully stylable and configurable. You can change how many lines are on this thing.
Quote it with credit and a link. Quoting Syntax