Using z-index to arrange elements in front of each other is not effective

To demonstrate my issue, I have created a fiddle: http://jsfiddle.net/AVxbd/

The problem lies in a menu (#container) with links (#text1, #text2). An element called #highlighter is used to highlight the link of the current page. Here is the HTML:

<div id="container">
    <div id="highlighter"></div>
    <div id="text1">Text 1</div><div id="text2">Text 2</div>
</div>

The JavaScript code is as follows:

$('#highlighter')
    .offset($('#text1').offset())
    .width($('#text1').width()
);

$('#text1, #text2').click(function(){
    $('#highlighter').animate({
        top: $(this).offset().top,
        left: $(this).offset().left,
        width: $(this).width() + "px"
    },300);
});

The issue I am facing is that the #highlighter appears on top of the #text1 and #text2 elements, which is not the intended behavior.

I attempted to resolve this using z-index:

#container {     z-index: 1; }
#highlighter {   z-index: 2; }
#text1, #text2 { z-index: 3; }

However, this solution did not work as expected, as shown in the fiddle.

How can I rectify this issue? Ideally, I would like the #text1 and #text2 elements to appear with an orange background while still allowing the highlighter to animate in width and position without affecting them.

Answer №2

#block1, #block2 {
cursor: pointer;
display: inline-block;
font-size: 30px;
height: 50px;
line-height: 50px;
text-align: center;
z-index: 3;
*position:relative;*

}

Include position in this specific class. Z-index functionality requires a defined position.

For more information on position and z-index, visit this link.

Answer №3

To make #text1 and #text2 relative, simply add position:relative;

VIEW DEMO

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

ng-switch not refreshing on its own

I've encountered a strange issue with ng-switch that I can't seem to solve. On the top of my page, I have: #content{"ng-switch" => "root.showAlert"} %div.alert.ng-cloak#alert{"ng-switch-when" => "true"} {{root.alert.message}} %di ...

Issue with horizontal scrolling in ng-scrollbars occurs when scrolling from right to left

We are currently developing a single page application that supports two languages, one being right to left and the other left to right. For scrolling functionality, we have implemented ng-scrollbars, an Angularjs wrapper for the malihu-custom-scrollbar-pl ...

Buttons are not aligned with the rest of the row's height

I'm currently working on designing an upload layout that consists of a container with a row inside. Within the row, there are two buttons (each containing input elements) and one h3. For some reason, I am having difficulty getting them to align at the ...

What is the method for confirming the returned response string value in Vue js?

I am having trouble checking the response values of a `Vue` API request. I attempted to use `resp.data == "-1"`, but it is not yielding the desired outcome. Can someone assist me in determining how to properly check the response value in `Vue.js`? Vue.js ...

Identify the element with the specified ID following a specific element, or generate it if it is not present

<div id="bar"> BAR </div> <div id="nextbar"></div> Is there a way to target #nextbar using the $('#bar').each()... method, and if it isn't present, generate it dynamically? ...

Go to a distant web page, complete the form, and send it in

As the creator of Gearsbook.net, a social network for Gears of War players, I am constantly striving to improve the site's functionality. Currently, it is quite basic and in need of updates. One highly requested feature by users is the ability to lin ...

Having issues with socket.io connectivity on my Node server

I am setting up my node server to update all connected clients with real-time information. However, when I run the code provided below, the io.sockets.on('connection') callback keeps firing constantly, flooding the console with the message Client ...

Utilizing moment.js to showcase the current time of day

Is there a way to show "Sunday Morning" if it's before noon or "Sunday Afternoon" if it's after noon? This code snippet below is what I am currently using to retrieve the current day: var now = moment().format("dddd"); $("#date").append(now); & ...

Steer clear of accessing cache data within web browsers

Is there a way to prevent the browser cache from affecting how I view a document? I would appreciate any help in solving this issue, whether it be through using JavaScript or another method. Thank you, Arun ...

Setting a static width for a specific cell in HTML5 - What's the best way to go about it?

When working with HTML5, how do I define a specific width for a table cell? Many of the <col> properties that were present in HTML4 are no longer applicable in HTML5. Just to clarify, I am aware that I can use <td style="width:150px">, but thi ...

Is it feasible to transmit telemetry through a Web API when utilizing ApplicationInsights-JS from a client with no internet connectivity?

My Angular application is running on clients without internet access. As a result, no telemetry is being sent to Azure. :( I am wondering if there is a way to configure ApplicationInsights-JS to call my .Net Core WebApi, which can then forward the inform ...

Challenges with jQuery Form validation and submitting entries

I am having some issues with my form validation using jQuery. The current setup is working well, but I need to make a specific change that I am struggling with due to my limited knowledge of jQuery. I want to ensure that text boxes, such as first name an ...

Retrieve the response text from a jQuery.get() call and return it

Attempting to achieve the following: var msg = $.get("my_script.php"); Expecting msg to be assigned to the text returned by my_script.php, which should be the responseText of the jqXHR object. However, it seems that msg is consistently set to "[object XM ...

The click event handler in a basic Vue application is failing to work with a button

In my Vue.js project, I have implemented a button that toggles the showProduct property between true and false when clicked. Here is the HTML code: <div id="app"> <button v-on:click="showCheckout">Click Me</bu ...

Send a string query using AJAX along with the user's username and password

I am looking to include the command=login& string with the username and password obtained from a form, like login.php?command=login&name=something&password=main <form id="login" action="https//login.php" method="GET"> <fieldset&g ...

dynamically import a variety of components in vue.js and nuxt depending on certain conditions

Is there a way to implement dynamic imports for all 3 components in this scenario? Each component has different props, so using the switch option in the computed method is not feasible. I have come across solutions for using a single component dynamically ...

handling null data in a jquery ajax callback

I've been working with knockout.js to bind data and making jQuery ajax calls to a restful service that returns JSON data. I've tried multiple approaches but can't seem to figure out the correct syntax for the call. Every time the callback fi ...

"Attempting to dynamically include Components for SSR bundle in React can result in the error message 'Functions are invalid as a React child'. Be cautious of this

When working with my express route, I encountered an issue trying to pass a component for use in a render function that handles Server-Side Rendering (SSR). Express Route: import SettingsConnected from '../../../client/components/settings/settings-c ...

Tips on updating Repeater control from the client side

I have a repeater control in my page A that I populate during page load. Afterwards, I utilize a jQuery popup to add data to the database through AJAX calls. Following the addition of data to the database, I redirect the user to a new page page B (accompli ...

Guide on aligning icons to the right within Bootstrap 4 dropdown elements with the help of CSS flexbox

Is there a way to create the menu below using Bootstrap 4 so that the icon on the right always stays visible, while the text is automatically truncated with an ellipsis (...) when it overflows? <li class="nav-item dropdown"> <a href='#&a ...