How to position a new div within its parent element using CSS without causing the rest of the content to

I'm currently working on implementing a short pop-up feature in my web application that will appear when a user clicks on a code snippet to copy it. However, I'm facing difficulties with preventing the pop-up from causing a shift in the parent div.

The GIF below showcases the issue that occurs after numerous attempts and online searches to address this problem. My objective is to have the copied message bubble smoothly appear at the top right corner of the span containing the room code.

https://i.sstatic.net/d1XPJ.gif

For reference, I have provided a stripped-down version of the interaction in this fiddle. Despite exploring different display and positioning options, I am uncertain about the next steps to take. Thank you in advance for any assistance provided.

https://jsfiddle.net/k6ey1duc/36/

.container {
  background-color: #008afa;
  width: fit-content;
  margin: auto;
  padding: 20px
}

.text {
  display: inline;
}

.pop-up {
  display: none;
  background-color: #fe0c0d;
}

#show-hide {
  display: block;
  margin: auto;
}
<body>
  <script>
    $(document).ready(function() {

      var x = false;
      $('#show-hide').on('click', function() {
        if (!x) {
          $("#pop-up").css({
            "backgroundColor": "#fe0c0d",
            "display": "inline"
          });
          x = true;
        } else {
          $("#pop-up").hide();
          x = false;
        }
      });


    });

  </script>
  <div class='container'>
    <p class='text'>
      Hello there! <span>Here is a span.</span>
    </p>
    <div id='pop-up' class='pop-up'>
      Here is a pop-up
    </div>
    <button id='show-hide'>
      Click for pop up
    </button>
  </div>
</body>

Answer №1

By applying position: absolute; to .pop-up, you can prevent the container from allocating any space for the element, which is crucial in avoiding unwanted gaps. Moreover, including position: relative; to .container allows you the flexibility to position .pop-up anywhere in relation to the container.

Alternatively, you can swap out display: none; display: inline; with

visibility: visible; visibility: hidden;
. The distinction lies in the fact that while display completely eliminates the element from the layout, visibility merely conceals the element without affecting its allocated space. Although this approach resolves the issue of the resizing container, it does not offer the same stacking and positioning capabilities as position: absolute.

.container {
  position: relative;
  background-color: #008afa;
  width: fit-content;
  margin: auto;
  padding: 20px
}

.pop-up {
  position: absolute;
  display: none;
  background-color: #fe0c0d;
}

Answer №2

Try using position:relative along with postion:absolute.

.container {
    background-color: #008afa;
    width: fit-content;
    margin: auto;
    padding: 20px;
    position: relative;
}
.pop-up {
    display: none;
    background-color: #fe0c0d;
    position: absolute;
    left:100%;
    width:inherit;
}

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

Is it possible to locate and eliminate the apostrophe along with the preceding letter?

My objective is to tidy up a character string by removing elements that are not essential for the user and SEO, specifically the (letter before the apostrophes) in this case. I am looking for a regex solution or explanation of how to achieve this in PHP, a ...

implementing HtmlSpanner with an appended css stylesheet

Using HtmlSpanner with CSS for TextView I recently discovered a library called HtmlSpanner that claims to assist in adding an HTML string with CSS to a TextView. However, I am struggling to locate any detailed documentation on how to achieve this, except ...

What is the best way to incorporate both the left and right menus with the main content

My goal is to incorporate a left and right menu alongside a main feed in the center. However, I'm encountering an issue where the left and right menus are scrolling with the scroll bar. Ideally, I would like the main feed to scroll with the bar while ...

Contrast between utilizing filter_input and accessing $_POST directly following an asynchronous AJAX request

When I use filter_input(INPUT_POST, 'attribute') and $_POST['attribute'], I get different results and I can't figure out why. The Post-Request is sent by a JavaScript script built with JQuery and it looks like this: // type javaS ...

The CSS code functions properly in Firefox, however, it does not seem to be functioning in

The menu I created in Magento is functioning correctly in Firefox. When I hover over the menu in Firefox, it works fine, but I am not seeing any hover effects in Chrome. Here is how my hover style is defined: #nav { width:965px; margin ...

When using the executeScript() method, it unexpectedly gives a null result instead of allowing me to scroll

The following code snippet is used to scroll horizontally within a web page: WebElement element=driver.findElement(By.xpath("//div[@class='table-wrapper']")); JavascriptExecutor js=(JavascriptExecutor)driver; js.executeScript("arguments[0].scroll ...

Issue with JavaScript focus not functioning properly

Why is the Javascript focus not working in the following code snippet? HTML <div class="col-md-6" style="border:none; margin-top:2px; padding-right:5px"> <input type="text" id="edtunt" ng-model="Unit" class="form-control" placeholder="Edit ...

Angular - The argument provided is not compatible with the parameter

I encountered the following TypeScript errors in app.component.ts: Issue: Argument of type '(events: Event[]) => void' is not assignable to parameter of type '(value: Event[]) => void'. Description: Types of parameters 'e ...

Guide to setting a dynamic value for an input List property in Angular

How can I render multiple dropdowns in Angular based on the response from an API? Currently, when I retrieve data from the API, I am seeing the same information displayed in both dropdown controls. Is there a way to assign dynamic values to the "list" prop ...

Implementing JavaScript from dojo.xhrGet within a Symfony framework

Hey there! I'm diving into the world of dojo and symfony, but I've hit a snag. I'm trying to do an Ajax reload of a section on my page, but it involves executing a JavaScript function. While I've got this down in prototype and jQuery, I ...

Is there a way to utilize the child component's method?

I am looking to access a child component's method from the parent in Vue.js. To achieve this, I plan on using $refs. Code Example: <template> <div>Parent!</div> </template> Script: <script> Vue.component('c ...

Easily done! Using JavaScript to generate a blinking cursor on a table

Working on a project to develop a dynamic version of the classic game Tic-Tac-Toe... However... Every table cell is displaying an irritating flashing cursor, almost like it's behaving as an input field. Any insights into why this is happening...? O ...

Difficulty in clearing form values/content despite successfully submitting

Whenever I try to submit my form, the reset function doesn't clear it as expected. Can someone please advise me on how to properly clear my form? My HTML, PHP, and JavaScript codes are provided below. HTML code snippet: <form id="main-contact-for ...

Performing an API call to refresh the token every hour

In my current setup, I have implemented a refresh token api call within the 'refreshToken()' function, although it is not shown in the code below. The purpose of this call is to be executed every 60 minutes. Now, I am facing a dilemma on whether ...

Designing numerous vertical lists in HTML

I need help with displaying multiple lists vertically using HTML For example: +--------+---------------------------------------------+--+ | Global | Region Country Currency Account Type Entity | | +--------+---------------------------------------------+ ...

What could be causing my newsletter form to malfunction on Amazon CloudFront?

I'm currently working with HTML on an Amazon EC2 instance (Linux free tier). I want to integrate CloudFront into my setup, but I'm encountering issues with my newsletter functionality. Despite not being an expert in AWS, I am struggling to unders ...

Searching for the way to access the values of a nested object's ref in Vue JS?

Within my Vue data object, I store a reference to a structure called Plot. It has properties for length, width, and acreage. interface Plot { length: number, width: number, acreage: number } const model = { plot: ref<Plot[]>([]), }) When fe ...

Retrieving the value of a nested element buried within multiple layers of other elements

There is a scenario I'm facing: <div class='situation1'> <div>..</div> <div> <span>...</span> <span>important details</span> </div> </div> Is there a w ...

Obtain image URL from object properties using AngularJS

Just starting out with Angular JS and I have a question. I'm currently working on a project and I have a solution in mind, but I was wondering if there's a more "angular-esque" approach that I could take. The idea is for each videoid, there wou ...

Collaborating with SockJS connectivity

Currently, my Node.js backend is interacting with desktop clients using websockets. The communication from the server side is initiated from a web front-end and everything is functioning properly because I am storing the SockJS Connection instances in an ...