How come the position sticky content vanishes when a z-index is applied?

Can someone assist with the issue I'm having regarding the use of sticky and z-index? Whenever I apply a z-index to a positioned sticky element, its content disappears.

Question: How can I utilize z-index for a positioned sticky element without any compromise (in a simple way)?

Below is my problem:

.sticky,.no-sticky{
        width:250px;
        position: relative;

      }

      .sticky input{
         position: sticky;  
         z-index: 23;
      }

      .icon:before{
          content: "...";
          position: absolute;
          font-size: 31px;
          top: -16px;
          right: 84px;
      }

      /* ====== run ===================2 */

      .sticky1,.no-sticky1{
        width:250px;
        position: relative;

      }

      .sticky1 input{
         position: sticky;  
         /*z-index: 23;*/
      }
<div class="no-sticky">
            <input type="text">
            <span class="icon"></span>
      </div>

      <hr>

      <p>see below input ... dots are not appearing </p>

      <div class="sticky">
          <input type="text">
          <span class="icon"></span>
      </div>


      <h1>Run without z-index</h1>

      <div class="no-sticky1">
          <input type="text">
          <span class="icon"></span>
      </div>

      <hr>

      <div class="sticky1">
          <input type="text">
          <span class="icon"></span>
      </div>

Problem Showing:

#container{
        width:175px;
        height: 200px;
        overflow-y:scroll;
        overflow-x:hidden;
        position: relative;
        z-index:40px;
      }

      input{
         position: sticky;  
         top:0;
         left:0;
        border:none;
        outline: 1px solid #cccccc52;
      }

      .icon:before{
        content:"...";
        position: absolute;
        font-size:30px;
        top:-16px;
      }
<div id="container">
        <span style="position:relative;">
            <input type="text">
          <span class="icon"></span>
        </span>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
        <p>lorem</p>
      </div>

Please provide assistance. Thank you in advance!

Answer №1

CHOICE A

  .sticky input{
     position: sticky;  
     z-index: -1;
  }

Adjust the z-index of the input element to be behind other elements.

CHOICE B

  .icon:before{
      content: "...";
      position: absolute;
      font-size: 31px;
      top: -16px;
      right: 84px;
      z-index: 1;
  }

Increase the z-index value of the icon above 0.

Ensure that you correctly prioritize the z-index for the desired visual layering effect.

Answer №2

/* Is it possible for a class to be both sticky and relative at the same time? It seems contradictory as a class can either be sticky or not, you can't have multiple positions for it using different indexes */
.sticky {
position: sticky;
top: 0;
}

.icon2:after{
    content: "...";
    font-size: 31px;
}
.no-sticky {
height: 1000px;
}
/* When making something sticky, you need to specify its position. You seem to have done everything in reverse. Also, you need to set a height in order to scroll the sticky part. Otherwise, it won't move */
<div class="sticky">
    <input type="text>
    <span class="icon2"></span>
</div>
<div class="no-sticky">
    <input type="text>
    <span class="icon"></span>
</div>

<hr>

<p>The dots next to the input field below are not appearing </p>

Here's how you achieve that. Remember, you are moving the entire class, not just the icon.

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

Ajax loaded scripts are unable to access global variables

Index.html <script> let bar = 1; </script> This html page is being loaded multiple times on the page using AJAX: article.html <script> if (bar === 1) { // perform a task } // Error: bar is not defined </script> Bar is a simple ...

What are the steps for editing and preserving a logo with an 8-bit indexed color palette?

After purchasing a RE/MAX franchise, I now need to make edits to the official logo found on remax.com using Adobe Photoshop. However, I have encountered a problem when trying to edit their logo in Photoshop. When I open their PNG logo, the layer appears d ...

How does Facebook access the new hashtags when they are clicked on?

I've been developing a website recently and I'm really impressed with the way a popup page appears when you click on a hashtag. I've been wanting to incorporate a similar feature for a while now, but I'm not sure how to do it. Could thi ...

The information is failing to display properly within the mat-menu

Recently, I've been working on creating a navbar that includes a submenu. Even though the navigation bar data is loading properly, I am facing some issues with the submenu functionality. As a beginner in this area, I would appreciate any help or guida ...

Swapping out a button

I tried searching for a solution to my problem but I couldn't find it because of my limited English proficiency. Therefore, I apologize if my question has already been answered. https://i.sstatic.net/XhQBM.jpg I would like to place my button in the ...

Disabling the scrollTop() effect following a click event with onClick()

Utilizing the scrollTop() event to toggle the visibility of a div at a specific position and also using the onClick() event to permanently hide the div. While the events are functioning properly, an issue arises when scrolling the page causing the div to r ...

`Passing JavaScript variables through HTML anchor tags`

I am currently attempting to pass the id to the controller using this method: {{ route("admin.service.edit", '+val[0]+' )}} However, the '+val[0]+' is being interpreted as a string in the URL http://localhost:8000/admin/servi ...

Nested Property Binding in CallByName

I am looking to utilize CallByName in VBA to extract specific data from various webpages with differing html structures. In my scenario, I need to reference multiple parent nodes to access an element with or tags. Here is an example of the code: The eleme ...

Adding flair to a fresh element and then removing it upon its inception

I'm working with a JavaScript code that creates a new element when a button is clicked. I have a question about it. Here's the JavaScript code snippet: var comment = document.querySelector("#AddComment"); var req = new XMLHttpRequest(); if(comm ...

Mastering the Art of Modifying HTML with Node.js

Is it possible to manipulate HTML using Node.js? 1. First, I need to retrieve data from a database. 2. Then, I need to modify or add HTML code within Node. In essence, the goal is to fetch data and integrate it into an HTML file. The JavaScript snippet ...

Bootstrap relies on jQuery for its JavaScript functionality, so jQuery must be loaded before using Bootstrap's JavaScript

I encountered an issue while trying to load the Bootstrap library, consistently receiving this error message: Uncaught Error: Bootstrap's JavaScript requires jQuery Even though I have ensured that jQuery is loaded before attaching the Bootstrap li ...

How to extract JSON data from an HTTP request in Java

I'm seeking to scrape data from a specific link using a Java program. The initial page is not an issue, but when attempting to gather data from subsequent pages, the source code remains the same as on the first page. The vital information I require is ...

Arrange three images both vertically and horizontally at the center of the page

I am facing an issue with aligning a button and 2 images in a row on my website. Currently, this is how it looks: https://i.stack.imgur.com/yrQB6.png Here is the HTML code I am using: <div class="row text-center"> <div class="col-md-12"> ...

Arrange radio buttons vertically in a table

I am attempting to display a vertical list of radio buttons within a table cell. Is this achievable? Here is the code snippet I am currently working with: <table> <tr> <td> First Name </td> ...

Is there a special Angular technique to ensure a div remains scrolled to the bottom of its items?

In this interactive demonstration, you can see how jQuery can be utilized to create a terminal-like feature. This functionality ensures that as new items are added to a scrollable div, the scroll automatically locks at the bottom. Pay close attention to l ...

set ajax url dynamically according to the selected option value

My form features a select box with three distinct choices <select id="tool" name="tool"> <option value="option1">Option1</option> <option value="option2">Option2</option> <option value="option3">Option3</ ...

Unexpected behavior from flexbox layout

So I've been experimenting with Chrome lately. I created a div with display: -webkit-box;. Inside this div, there are 7 other divs all set to -webkit-box-flex: 1;. Oddly enough, the first div is smaller than the rest. Any ideas on why this might be ...

Tips for updating the id of a tag using JavaScript?

Html Code:- {% for post in posts %} <article class="media content-section"> <div class="media-body"> <h2><a id="post_title" class="article-title" href="{% url 'post-detail' post.slug %}">{{ post.title }}&l ...

Implementing nested popup windows using Bootstrap

I am facing an issue with my two-page sign-in and sign-up setup in the header of my angular2 project. On the sign-up page, I have a link that should redirect to the sign-in page when clicked, but I am struggling to make it work. Can someone provide guidanc ...

Transferring information from an HTML webpage to a PHP file and then redirecting to the homepage

My goal is to post the data to datahouse.php, which will then submit the data to the server and redirect to the Index page without displaying the datahouse.php file on the page. register.html <form method="POST" action="DataHouse.php"> <input ty ...