<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Micah Jones</title>
    <description>The latest articles on DEV Community by Micah Jones (@micahjones13).</description>
    <link>https://dev.to/micahjones13</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F849732%2F95353791-df1f-4f19-8bc0-4ae1037310a3.jpeg</url>
      <title>DEV Community: Micah Jones</title>
      <link>https://dev.to/micahjones13</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/micahjones13"/>
    <language>en</language>
    <item>
      <title>Styling a native range input to be different heights before and after the thumb</title>
      <dc:creator>Micah Jones</dc:creator>
      <pubDate>Mon, 18 Apr 2022 22:57:58 +0000</pubDate>
      <link>https://dev.to/micahjones13/styling-a-native-range-input-to-be-different-heights-before-and-after-the-thumb-4ome</link>
      <guid>https://dev.to/micahjones13/styling-a-native-range-input-to-be-different-heights-before-and-after-the-thumb-4ome</guid>
      <description>&lt;p&gt;Styling native HTML elements can be tricky, and the range input is certainly no different. I was tasked with updating our slider component, which uses the classic &lt;code&gt;&amp;lt;input type=’range’&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem:
&lt;/h2&gt;

&lt;p&gt;You probably know about styling the track and the thumb through pseudo classes such as ::-webkit-slider-runnable-track and ::-webkit-slider-thumb, or the Moz and MS versions of each. These are great, and allow you to style the track and thumb respectively, but what gets tricky is when you want the height of the track different on each side of the thumb, like my design called for.&lt;/p&gt;

&lt;p&gt;My first thought was, “I’ll just &lt;code&gt;::before&lt;/code&gt; or &lt;code&gt;::after&lt;/code&gt; the thumb, and change the track height that way. Unfortunately, the &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt; pseudo-elements don't work with the pseudo-classes for the track and thumb. Well, what now? I can change the height of the track, but that sets the height for the entire track, which we don’t want.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution:
&lt;/h2&gt;

&lt;p&gt;In order to get the track two different heights on either side of the thumb, you can take advantage of passing the &lt;code&gt;background-image&lt;/code&gt; attribute two arguments. The caveat to this though, is that &lt;code&gt;background-image&lt;/code&gt; doesn’t accept a straight color - so no hex or rgb values allowed. It does, however, accept gradients! So, you can do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.slider::-webkit-runnable-track {
    background-image: 
       linear-gradient(blue, blue), linear-gradient(blue, blue);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding two linear gradients of just blue won’t make it look any different right now from simply doing background-color: blue, but once we add the other background properties, some magic can happen.&lt;/p&gt;

&lt;p&gt;The other background properties we’ll use are &lt;code&gt;background-size&lt;/code&gt;, &lt;code&gt;background-repeat&lt;/code&gt;, and &lt;code&gt;background-position&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.slider::-webkit-runnable-track {
  background-image: 
    linear-gradient(blue, blue), linear-gradient(blue, blue);
  background-repeat: no-repeat no-repeat;
  background-position: left;
  /* background-size: ???? */
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting the background repeat to &lt;code&gt;no-repeat no-repeat&lt;/code&gt; makes the track color consistent, and setting the position to left left-centers the track.&lt;/p&gt;

&lt;p&gt;The next property we’ll need is &lt;code&gt;background-size&lt;/code&gt; and this is where we’ll need some sort of logic. In this example, I’ll be using vanilla JS and CSS custom properties.&lt;/p&gt;

&lt;p&gt;We’ll need to set a CSS custom property, and then change that properties value based on events from the user. For reference, this is the HTML structure we’ll be using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="wrapper"&amp;gt;
  &amp;lt;div class='slider-wrapper'&amp;gt;
    &amp;lt;input class='slider' type="range" max="100" min="0"&amp;gt;. 
    &amp;lt;/input&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we’ll need to create a CSS custom property to change inside of our .css file. For this, I’m using &lt;code&gt;--valuePercent&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.wrapper{
  --valuePercent = 50%;
}
.slider::-webkit-runnable-track {
  background-image: 
    linear-gradient(blue, blue), linear-gradient(blue, blue);
  background-repeat: no-repeat no-repeat;
  background-position: left;
  /* background-size: ???? */
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the CSS custom prop is created, we can modify it in our JS. Next, find the slider:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const slider = document.querySelector('.slider');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then, we need to add an event listener to it and create a function that runs when that listener is triggered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const slider = document.querySelector('.slider');
slider.addEventListener('input', updateValue);
function updateValue(){ 
  const val = slider.value;
  const valPercent = `${val}%`;
  slider.style.setProperty('--valuePercent', valPercent);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since this is an &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; element, we can listen to the input event. Our &lt;code&gt;updateValue&lt;/code&gt; function then fires and in turn gets the current value of the slider, changes that to a percent string, and updates our CSS custom prop with that percent.&lt;/p&gt;

&lt;p&gt;Now, we can use the &lt;code&gt;background-size&lt;/code&gt; to determine when the track should be different heights!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.wrapper{
  --valuePercent = 50%;
}
.slider::-webkit-runnable-track {
  background-image: 
    linear-gradient(blue, blue), linear-gradient(blue, blue);
  background-repeat: no-repeat no-repeat;
  background-position: left;
  background-size: var(--valuePercent) 5px, 100% 1px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because we have two background image’s, we can set each one of those to a different width and height using &lt;code&gt;background-size&lt;/code&gt;. Now, the width of the first background image is equal to &lt;code&gt;--valuePercent&lt;/code&gt;, and will be 5px tall. The second background size is set to 100% width, which means that the entire track will be effected by the 1px height. But since the left side of the thumb is 5px, that’s what we’ll see.&lt;/p&gt;

&lt;p&gt;Now our track is two different heights on either side of the thumb! You can now use the &lt;code&gt;::-webkit-slider-thumb&lt;/code&gt; and the other div classes to style this however you’d like. Keep in mind, this implementation is only using webkit, and other browsers use different prefixes: -mz for Firefox and -ms for IE.&lt;/p&gt;

&lt;p&gt;You can see my entire example at this CodePen &lt;a href="https://codepen.io/micahjones/pen/gOWXRjY"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
