Showing a <div> element sandwiched between two elements from different hierarchies

Consider this snippet of HTML code:

<div style="background-color: blue">
    <div style="color: red">
        some content some content some content some content 
    </div>
</div>

<div id="highlight" style="top: 0px; width: 200px; height: 40px; 
                           background-color: yellow; position: fixed">
</div>

View on jsfiddle


This is how it appears:

https://i.sstatic.net/bHyaW.png

The objective is to visually place the #highlight <div> between the blue background and the text. The desired outcome is shown below:

https://i.sstatic.net/hPjQQ.png


In a real-life scenario, JQuery is used to manipulate the position of #highlight based on mouse/keyboard events. The hierarchy of the containing <div>s cannot be altered.

The #highlight's position must either be fixed or absolute.

Is there a way to achieve the desired result without modifying the HTML structure? Attempts have been made with z-index, but the hierarchical structure takes precedence. Any unconventional methods involving JavaScript/JQuery are welcome.

Answer №1

To style the div content, apply the CSS properties z-index:1 and position:relative.

<div style="background-color: blue;">
  <div style="color: red; z-index: 1; position:relative;">
    some content some content some content some content
  </div>
</div>

<div id="highlight" style="top: 0px; width: 200px; height: 40px; background-color: yellow; position: fixed;">
</div>

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

Error: The program encountered a TypeError because it was unable to read the property 'top' of an undefined element when attempting to click the send

I have been searching for solutions, but none seem to match the requirements of my code. My website has various categories. I encountered an issue while attempting to add a new ad post here. form.find('.info-tooltip-container').addClass(&ap ...

Is it possible to utilize CSS to generate a full-width slider while maintaining the aspect ratio of the content?

I am currently utilizing the unslider plugin to design a full-width slider header on a webpage (). Although it functions well at the dimensions I set it up for (1920x450), issues arise when viewed on different screen resolutions. See below: The background ...

When employing useEffect, clearInterval cannot halt the execution of SetInterval

My attempt to create a function that initiates a countdown when the isPlaying variable is true and stops when it's false has not been successful. Instead of working as intended, it starts multiple intervals concurrently. The value of isPlaying changes ...

Tap on the key within the input field

Completing a login form programmatically: document.getElementById('i0116').value = email; document.getElementById('i0118').value = password; document.getElementById('idSIButton9').click(); A challenge arises w ...

React and Express: Encounter a proxy error when trying to proxy the request for /total from localhost_errors

This question is a continuation of my previous inquiry on React.js: Axois Post stay on pending(but i am getting data) Below is the content of my package.json file { "name": "my-app", "version": "0.1.0", "private": true, "dependencies": { "ax ...

Presenting the correct error notification if the JSON data fails to load in an HTML webpage

Here is a sample JSON data: [ { "componentid": 4, "displayImageUrl": "https://via.placeholder.com/350x200", "title": "theme", "shortdesc": "to set theme for different applications" }, { "componentid": ...

Can an HTML DOM object be converted to a JSON string using JSON.stringify in JavaScript?

Trying to fetch an external HTML file and convert its body content into a string has been giving me unexpected results. Is there a way to achieve this successfully? var xhr = new XMLHttpRequest(); function loadFile(){ xhr.open("GET", 'index.html ...

Returning to the initial state after clicking on an element within a repeated set in AngularJS?

I'm currently facing a challenge, mainly due to my lack of understanding in basic AngularJs concepts. The issue arises when I interact with a list of clickable words. When I click on any of these words, their color changes individually thanks to the i ...

Adjusting the height of a CSS div to be 0 while also modifying the

I have encountered a unique dilemma <div class="parent"> <p>stuff stuff stuff</p> </div> Using CSS, I set the width and height of parent = 0; because I intend to modify it when a button is clicked. However, for this purpose, I ...

Styling list items (li) on hover without using the li:

I have a menu where I am using the :after element. When I hover over a li in the menu, the hover effect also includes the :after element. HTML: <ul class="main-menu"> <li class="active"><a href="">Menu</a></li> <l ...

Implementing basic authentication in Socket.IO on a Node.js server

Currently, I am attempting to develop a basic websocket client for establishing a connection with a device. However, the device requires both a username and password for authentication purposes, posing a challenge for me as I struggle to figure out how to ...

What is the easiest method for incorporating a cache manifest into a Meteor application?

I'm looking for a way to make the Meteor application from meteor.com load offline without connecting to the Meteor server using a cache manifest. While I found a similar question on adding a cache manifest to a Meteor app on Stack Overflow, I had tro ...

Creating a Dynamic Navigation Bar with HTML and CSS

I've been exploring the code on w3schools.com, and while there are some great examples, I'm struggling to understand it all. Unfortunately, there aren't any instructions on customizing the code for a third level of menus. Can someone simplif ...

The backtick is not functioning correctly when trying to append the result in the Internet Explorer browser

I am using the .html method to append HTML content to a specific div ID within my file. <html> <head> Title <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> ...

Make sure to align images and comments seamlessly on your website just like in the comment section of YouTube

Looking for help aligning an image and two spans in one row <img src="../images/profile/profile.png" class="img" /> <span class="name">First Name</span> <span class="comment"> This is simply dumm ...

Element within an element should be centered

I've been attempting to center a block element, specifically the WordPress caption box with an image, and I'm encountering difficulties. Here is what I have tried: .imagecenter { margin-left: auto; margin-right: auto; display: block; } ...

The clearfix feature is ineffective when using AngularJS

<ul class="dropdown-menu wm_search_div" ng-show="searchDivShow"> <li ng-repeat="user in searchUserList"> <a href="javascript:void(0);" class="wm_clearfix3"> <img ng-src="{{user.faceIcon}}" class="pull-left wm_se ...

Implementing a Bootstrap 4 modal in Webpack 2

Utilizing only the bootstrap modal functionality, I manually included the required libs (modal.js & util.js) in the vendor section of webpack.config.js If I directly add the vendor.js file, everything works fine. However, since it is bundled, I prefer ...

Maintain visibility of adjacent elements while one is being hovered over

Take a look at this demonstration: https://jsfiddle.net/ptss94gw/ I'm curious if it can be achieved using only CSS to maintain visibility of the .more-icons division when hovering outside of the .h-icon. ...

Failure in SystemJS during ahead-of-time compilation due to missing NgZone provider

Previously, I have successfully used Angular's ahead-of-time compilation. However, after adding routing and lazy loading to my app, I am facing difficulties in making it work again. Upon updating my code to the latest 2.0 release, it functions well w ...