Using z-index to position the toolbar in a unique way

I have a question regarding the proper positioning of one div over another in a specific scenario:

Currently, I am working on creating a toolbar for .col-9.h-100 from my code snippet. My goal is to have this toolbar appear over .col-9.h-100 when clicked by the mouse. Understanding that it's not feasible to assign a child element of .col-9.h-100 with a higher z-index than its parent, I am working on creating a sibling element with identical dimensions. The idea is for this new element to overlay .col-9.h-100 with a height approximately 20% of .col-9.h-100.

$('.col-9.h-100').click(function(){

$("\
  <div class='row' id='rowToolbar'>\ 
      <div class='col-9 h-100'>\  
        <div id='toolbar'>\     
          <button class='btn btn-primary controls'>addNode</button>\
        </div>
      </div>
    </div>
  ").appendTo($('body'))
})
html,
body {
  height: 100%;
}

.row{
  margin:0!important;
}

#rowToolbar{
}

.col-9.h-100{
  background-color:lightgray;
  padding:0;
}

.col-3.h-100{
  background-color:lightblue;
}

#toolbar{
  background-color:white;
  display:inline-block;
  width:100%;
  height:20%;
}

.controls{
  float:right!important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

<a class="navbar-brand" href="#">MAIN</a>

<div class="row h-100 w-100">
  <div class="col-9 h-100"></div>
  <div class="col-3 h-100"></div>
</div>

I've attempted various combinations involving different styles such as display and z-index, but none of them have yielded the desired outcome.

Upon running the snippet, you can observe that the toolbox appears appropriately sized but positioned incorrectly. What "game" with z-index and display should I employ to arrange them in the correct order?

If possible, could you please demonstrate the correct method for accomplishing this? Thank you in advance.

Answer №1

It's recommended to assign custom classes to the elements you plan on manipulating with JavaScript in order to prevent any potential issues down the line. Additionally, it's advisable to refrain from dynamically creating HTML elements as it could negatively impact performance. You can find a performance comparison here

$('.content').click(function(){
$("\
  <div class='row'>\
        <div class='toolbar'>\
          <button class='btn btn-primary controls'>addNode</button>\
      </div>\
    </div>\
  ").appendTo($('.content'));
});
$('.content').on('click', '.controls.btn', function(){
  return false;
});
html,
body {
  height: 100%;
}

.content {
  background-color:lightgray;
  padding:0;
}

.toolbar {
  margin: 10px;
  display:inline-block;
  width:100%;
  height:20%;
}

.sidebar {
  background-color:lightblue;
}

.controls{
  float:right;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

<body>
  <div class="navbar navbar-light bg-faded justify-content-between"><a class="navbar-brand" href="#">MAIN</a>
  </div>
  <div class="row h-100 w-100">
    <div class="content col-9 h-100"></div>
    <div class="sidebar col-3 h-100"></div>
  </div>
</body>

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

Exploring the magic of jQuery in menu design

I'm currently working on a menu feature where clicking on different tags triggers various effects and CSS changes. However, I've noticed that when I click on a new tag, the previously clicked one doesn't revert back to its original state. It ...

Can you answer this straightforward query about CSS positioning?

I am currently facing a challenge in creating a small div (header class) that overlays a main banner image div (banner class). When I maximize my browser window, the positioning is correct. However, upon resizing the browser, the header div maintains its m ...

Customized link routing / Deep linking

I need some help. I am managing a standard website and I want to redirect custom URLs to specific pages when visitors access the site. For instance: - index.html should point to custom_index.cfm?custom_id=1&page_id=1&inc=index - about.html shou ...

The webpage does not display the updated information from the controller after the Ajax call

Learning the ropes of MVC, I delved into creating a post method for showcasing fresh data post a datepicker selection in jquery. The jquery functionality is up and running smoothly, allowing me to iterate through the new data in the view. However, when i ...

Magic chest - triggered upon touch, disengaged upon tap elsewhere

Is there a way to create a div box that triggers a specific event when clicked, but only affects the time when we click somewhere else afterwards? It should be designed so that clicking multiple times in the box does not produce any additional effects. ...

Issues arise when incorporating background images with bootstrap 3

Currently working on designing a responsive layout for an online clothing store using Bootstrap 3. One of the requirements is to use background images inline styles for product containers. However, facing difficulty styling the containers and images to ach ...

Creating mobile-friendly websites for a variety of devices, including smartphones and tablets

Creating a prototype webpage for my friend, one crucial aspect is ensuring it looks good on different devices. Utilizing Bootstrap, I've implemented vertical collapsing for certain elements, but I'm struggling with how to adjust other components ...

Is it possible for me to use @import to selectively choose what I need and disregard the rest?

If I am utilizing SASS and wish to incorporate a significant portion of another existing stylesheet, whether it is in SASS or CSS format, I have the option to import that particular sheet using @import, then employ @extend to apply some of its rules. Is ...

Populate the input file field on page load

I have a simple HTML form that allows users to choose files. <input multiple type="file" name="file"/> <input type="submit" name="submit" value="Submit" /> When the user clicks on a specific button on the home page, they will be directed to ...

Modifying fields in a dynamic Oracle APEX report

Not a beginner or an expert in Apex here, But, I have limited knowledge of major client-side web languages, and now I need to tackle them. And soon. Here is an example here - username : test, password : test. I am using the same jQuery and JavaScript ...

Modify the class of a specific item using JQuery ID, then select the same item based on its ID with the updated class name

Attempting to toggle between 3 buttons on an HTML jukebox, where clicking the same button again should mute the sound. Each button has a specific ID and class name that changes when clicked. Here is the code snippet for initiating one of the buttons: $(" ...

Troubleshooting: Unable to retrieve search input data from SearchBar in React

Struggling with grabbing the value of the search input in my page that utilizes the search prop from bootstrap-table2-toolkit. I attempted to utilize the "afterSearch" function, but it returned an array of search results. I also experimented with "onChange ...

Prevent the change event from firing when the page loads in JavaScript - what can be done to avoid

Dealing with a form that contains chained select boxes can be quite challenging. Particularly when the Zend controller is involved in setting default values for these select boxes, which are pulled from the database. As someone who is new to jQuery, naviga ...

Halt hovering effect after a set duration using CSS or Vanilla JavaScript

Looking for a way to create a hover effect that lasts for a specific duration before stopping. I want the background to appear for just 1 second, even if the mouse remains hovering. Preferably using CSS or JavaScript only, without jQuery. To see my curren ...

Changing the Stacking Order of Full-Width Columns in Bootstrap v3

Is it feasible to rearrange the sequence of full width columns in Bootstrap v3? Take into account... -xs (A and B both assigned col-xs-12) [ A ] [ B ] -sm (A and B both designated col-sm-12) [ B ] [ A ] I am aware of the option to use hide/show meth ...

The animation in jQuery is not functioning properly after inserting HTML blocks via AJAX

After some investigation, it seems that by adding this block forcibly, everything functions correctly. I noticed that when clicking on an input field, the focused and is-focused classes are not applied to it. The first screenshot demonstrates how it operat ...

Integrating Vue.js with a Bootstrap navbar

Recently, I've been working on integrating Vue.js into a Bootstrap HTML template. However, I've encountered an issue with the navbar. Here's a snippet of my code: <router-link tag="li" to="#"> <a><i class="fa fa-book" a ...

The Hidden Div containing NicEdit is now shrunk down to a smaller size

Attempting to integrate the NicEdit editor for a hidden textarea stored within a div has presented some challenges. The goal is for the targeted textarea's parent div to be revealed upon the user clicking a button, with the textarea's width set t ...

What is the best method for purging a previously stored variable from the cache when making an Ajax call and simultaneously passing a

I am encountering an issue with variable loading during the ajax call. Upon clicking, I pass a variable to the ajax call which then sends the value to a PHP script. The data obtained is from: rnc.csv DLRNC01 DLRNC02 DLRNC03 DLRNC04 DLRNC05 DLRNC06 DLR ...

Do you have any recommendations to help me achieve the sticky footer hack?

Check out this awesome website: I've been working on incorporating a responsive design with a sticky footer, but I encountered some issues with incompatible CSS options. The fixed option wasn't meeting my needs either. For the homepage, I have ...