Tips for correctly positioning CSS elements:

I am currently working on a slider using noUi Slider and aiming for an elegant solution. To accommodate the large size of the handle, I expanded the base UI with extra values which are not allowed, causing the handle to jump back to the permitted values. To address this issue, I added plus and minus buttons where the restricted values are located.

However, I am facing a problem where I can't make the control symbols (plus/minus) disappear behind the handle when it is moved over them. They remain visible on top, and I am struggling to find a solution to hide them behind the handle. Any help or advice on this matter would be greatly appreciated.

Below is the code I have implemented (styles can be found on JSFiddle):

$(document).ready(function(){

    var sliders = document.getElementById('red'),
        input = document.getElementById('handle'),
        sliderPlus = document.getElementById('slider-amount-plus'),
        sliderMinus = document.getElementById('slider-amount-minus'),
        termCur = 500;

    noUiSlider.create(sliders, {
        start: termCur,
        step: 50,
        connect: "lower",
        range: {
            'min': 0,
            'max': 1100
        },
        pips: {
            mode: 'values',
            values: [100, 500, 1000],
            density: 4.5
        }
    });
    $('<div class="value">' + termCur + ' zł' + '</div>').appendTo($('.noUi-handle', sliders));


sliders.noUiSlider.on('change', function ( values, handle ) {
    if ( values[handle] < 100 ) {
        sliders.noUiSlider.set(100);
    } else if ( values[handle] > 1000 ) {
        sliders.noUiSlider.set(1000);
    }
});
sliders.noUiSlider.on('update', function( values ) {
    termCur = values;
    if( termCur >= 100 && termCur <= 1000 ) {
    $('.value', sliders).text(parseInt(termCur) + ' zł');}
});
sliderPlus.addEventListener('click', function(){
    if(termCur < 1000) {
        var setValue = parseInt(termCur) + 50;
        sliders.noUiSlider.set(setValue);
    }
}); 
sliderMinus.addEventListener('click', function(){
    if(termCur > 100) {
        var setValue = parseInt(termCur) - 50;
        sliders.noUiSlider.set(setValue);
    }
}); 

<div class="sliders" id="red">
        <a class="controls-symbols slider-minus" id="slider-amount-minus"><i class="fa fa-minus"></i></a>
        <a class="controls-symbols slider-plus" id="slider-amount-plus"><i class="fa fa-plus"></i></a>
    </div>

https://jsfiddle.net/o7Ly845j/

Answer №1

Upon examining @zgood's latest fiddle, I deduced that the issue was not solely related to the z order anymore. It appeared that both the slider bar and the plus/minus elements were handling the events simultaneously, leading to conflicts. By rearranging the order of events so that the plus/minus elements received the event first and prevented it from reaching the bar, I managed to resolve most of the problems with just a simple modification to the mousedown event. Consequently, I could eliminate all the checks for values greater than 100 and less than 1000, as the bar started behaving correctly.

Another observation I made was that once the slider button reached 50/1050, it obscured the plus/minus buttons. To address this, I adjusted the z-index of the plus/minus elements to 13 instead of 3 in the fiddle. Whether z-index value of 3 or 13 is preferable is subjective, so feel free to experiment with both options and decide for yourself.

Visit the updated fiddle here

$(document).ready(function(){

    var sliders = document.getElementById('red'),
        input = document.getElementById('handle'),
        termCur = 500;

    noUiSlider.create(sliders, {
        start: termCur,
        step: 50,
        connect: "lower",
        range: {
            'min': 0,
            'max': 1100
        },
        pips: {
            mode: 'values',
            values: [100, 500, 1000],
            density: 4.5
        }
    });
    $('<div class="value">' + termCur + ' zł' + '</div>').appendTo($('.noUi-handle', sliders));

    var c = $('#red > span').clone(true);
    $('#red > span').remove();
    $('.noUi-base').prepend(c);

    sliders.noUiSlider.on('update', function( values ) {
        var val = parseInt(values);
        termCur = val;
        $('.value', sliders).text(termCur + ' zł');
    });
    $('#slider-amount-plus').click(function(e){
        if(termCur < 1100) {
            termCur = termCur + 50;
            sliders.noUiSlider.set(termCur);
            $('.value', sliders).text(termCur + ' zł');
        }
       e.stopPropagation();
    }).mousedown(function(e){
       e.stopPropagation();
    });     
    $('#slider-amount-minus').click(function(e){ 
        if(termCur > 0) {
            termCur = termCur - 50;
            sliders.noUiSlider.set(termCur);
            $('.value', sliders).text(termCur + ' zł');
        }
    }).mousedown(function(e){
       e.stopPropagation();
    }); 

});

Answer №2

Clusters of elements sharing a common ancestor that advance or retreat together in the stacking arrangement constitute what is referred to as a stacking context. An in-depth comprehension of stacking contexts is crucial for fully understanding how z-index and the stacking order function.

Each stacking context is anchored by a single HTML element acting as its fundamental component. Upon forming a new stacking context on an element, said stacking context restricts all its offspring elements to occupy a specific position within the stacking order. This implies that if an element resides in a stacking context at the bottom of the stacking order, it is impossible to have it appear ahead of another element situated in a distinct stacking context positioned higher in the stacking order, even with a z-index value exceeding a billion!

New stacking contexts can be established on an element through one of these methods:

  • When an element serves as the root node of a document (the element)

  • When an element possesses a positioning attribute other than static alongside a non-auto z-index value

  • When an element features an opacity value below 1

  • Besides opacity, numerous CSS properties introduce stacking contexts too. These encompass: transforms, filters, css-regions, paged media, and perhaps additional ones.


The following are the essential guidelines for determining stacking sequence within a singular stacking context (from rearward to forefront):

  • The root element of the stacking context

  • Positioned elements (and their descendants) possessing negative z-index values (higher values get piled upfront of lower values; components with identical values are stacked based on their occurrence in the HTML)

  • Non-positioned elements (arranged according to their appearance in the HTML)

  • Positioned elements (and their progeny) with a z-index value set as auto (organized by their visual presence in the HTML)

  • Positioned elements (and their descendants) featuring positive z-index values (higher values overshadows lower values; assets with the equivalent worth are assembled following their presentation in the HTML)

Note: Positioned elements with detrimental z-indexes are primarily sequenced within a stacking context, hence they end up stationed behind every other constituent. This situation enables an element to potentially materialize at the back of its parental figure, which typically isn't feasible. Such occurrence exclusively transpires if the parent entity of the element exists within the same stacking context and doesn't serve as the primary element in that regard.


I devised a "hackish" technique utilizing jQuery for achieving this task, you can explore the Fiddle by accessing this link. However, consider this approach provisional since I am currently refining the positioning procedure due to its reliance on fixed values. Additionally, I am investigating why the CSS behavior isn’t responsive and will disclose my findings here soon.

     $(document).mousemove(function (e) {
         var newpos = $(".noUi-handle").offset().left;

         if (newpos < 0) {
             $(".slider-minus").hide();
         } else { 
             $(".slider-minus").show();
         }

         if (newpos > 830) {
             $(".slider-plus").hide();  
         } else {  
             $(".slider-plus").show();
         }     
    });

update Here is an enhanced version, presently investigating the issue regarding the non-cooperative CSS behavior.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

transforming JSON information into tables on a webpage

Can someone help me with the challenge of incorporating a massive JSON file into an HTML table? I am encountering an issue where I continuously receive the error message Uncaught TypeError: v.forEach is not a function. Any guidance would be greatly appreci ...

What is the best way to rearrange the order of navbar items when working with two nav bars and a single toggle in bootstrap 4?

I am looking to implement a design with two navigation bars that share a single toggle. The first navbar should include the logo, email icon, and a search box, while the second navbar is reserved for additional links. On smaller screens, when the toggle ...

Numerous textareas fail to function properly according to JQuery's standards

Need help with resizing multiple textarea elements dynamically as the user types in them (on the Y-axis). Currently, I have code that successfully resizes a single textarea, but it does not work when there are multiple textareas on the page. Here is the c ...

I encountered an issue while generating a crypto address on the Waves blockchain using the @waves/waves-crypto library in TypeScript

Encountering an issue with finding "crypto-js" in "@waves/waves-crypto". Despite attempts to uninstall and reinstall the module via npm and importing it using "*wavesCrypto", the error persists within the module's index.d.ts file. I am attempting to ...

Transfer data in scripts to dynamically load Ajax pages

Is there a way to send variables or parameters from the original page to a script tag on another page that is loaded using jQuery Ajax? Here are the scripts: Page 1 <script> $(function(){ $('#button').click(function(){ $.ajax({ type:"P ...

Ensure the menu bar remains at the top of the page without using the position:

Check out this awesome fiddle here! My menu bar looks great at the top, but for some reason it won't stay in place when the user scrolls down. I've tried adding position:fixed to the CSS under the first #cssmenu, but it messes everything up. Her ...

Unable to modify jwplayer css styles to customize the chromecast icon

Is there a way to customize the chromecast icon within the JWPlayer skin to have a specific color? I've identified that the styles I need to change are --connected-color and disconnected-color when inspecting the element. Despite my attempts through ...

The CSS background-image and background-color do not load on IE8

I have a specific style applied to a link: #block-menu-menu-top-menu a.contact-us-link { background-image: url("../images/top-menu-contact.png") no-repeat; background-color: none; height: 28px; text-indent: -9999px; width: 34px; } However, when ...

Jquery experiencing compatibility issues with Internet Explorer

I created this jQuery function to check a PHP page and display the result. It works perfectly in other browsers, but unfortunately I am experiencing issues with Internet Explorer (IE). This is concerning because most users use IE as their browser. $(docum ...

PHP - Implementing a Submit Button and Modal Pop-up AJAX across multiple browsers in PHP

As a newbie in PHP AJAX coding, I'm facing a challenge with having two browsers. My goal is to click the submit button on one browser and have a modal popup on the other browser simultaneously. Essentially creating a chat system where only a button an ...

What is the process for altering the initial day of the week in the full-calendar plugin within a Laravel application?

After installing the Laravel fullcalendar package, I noticed that the start day of the week is set to Sunday. However, I would like to change it to Saturday. Can someone please assist me with this issue? Below is the function in my Controller: public func ...

The textbox fails to update when the condition in the IF statement is met

In the following code, I have an input box with the ID volumetric_weight that gets updated on keyup. However, the second textbox with the ID volumetric_price does not update as expected, even though I believe I wrote it correctly. I am wondering if there ...

Utilizing Python Selenium to Interact with Buttons

Encountering an issue while trying to use Selenium to click on a tab or button within my web application. Below are the code snippet and CSS selectors: Error message: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: #t ...

Validating Forms using JQuery

I am currently working on a jQuery AJAX code to validate specific fields in an HTML form before submission. If any errors occur during validation, I want the error message to be displayed in a div with the ID "response." However, the code doesn't seem ...

The most straightforward method for transforming a C# method into an asynchronous call for a Page/Web Method or jQuery

Looking for a way to call a C# method asynchronously in a web form without triggering a postback? Consider using either a Page Method, Web Method, or jQuery. The method in question handles multiple tasks such as calling other methods, rebuilding a treeview ...

Use Onblur and Onfocus events in an input field that is read-only

Here is an input field: <input class="" type="text" id="Name_11_1" name="Name" value="Your name:"> I would like to modify it as follows: <input class="" type="text" id="Na ...

Fullscreen images stop from getting stretched

I am facing an issue with making images full-screen without them stretching on larger screen sizes. Is there a technique to keep the image size consistent regardless of the screen dimensions? I am aware of how background images can be made fullscreen usi ...

Retrieve the values of the children within a div tag

Hello there, I am attempting to retrieve all the child values from the parent div .clonedInput when the .send button is clicked. I also want the output to be formatted as shown below and placed in a temporary alert(); variable for code confirmation. //Exa ...

"How can we pause the setInterval when the user hovers over the Ajax Quick Cart and resume it once

Currently, I am working on configuring my Ajax Quick Cart to delay a setInterval function when the user hovers over the cart. The goal is to have the cart close automatically after 3 seconds once an item has been added. However, as I'm not very profic ...

Is it possible for PHP to not provide an image to HTML?

I'm currently facing an issue trying to display an image in HTML using a PHP script. Despite my best efforts, it's not functioning as expected :-( Here is the code snippet from my image.php script: <? $_GET['f'] = 'all_thre ...