Tips for positioning a div alongside its parent div

I have a unique setup with two nested divs that are both draggable. When I move the parent div (moveablecontainer), the child div (box.opened) follows smoothly as programmed. Everything works well in this scenario.

However, when I resize the browser window, the child div ends up moving along with the body of the browser instead of staying next to its parent div (moveablecontainer). The parent div remains static unless dragged.

Here is the CSS for the two divs:

PARENT DIV

.moveablecontainer {
  position: absolute;
  left: 615px;
  top: 10px;
  width: 100px; 
  border: 0;
  height: 450px;
  max-height: 95px;
  background-color: transparent;
  z-index: 1000000;
}

CHILD DIV inside parent:

.box {
   max-height: 0px;
 }
.box.opened {
  position: absolute;
  max-height: 400px;
  padding-top: 45px;
  padding-right: 10px;
  padding-left: 10px;
  padding-bottom: 20px;
  margin-left: 0px;
  border-radius: 5px;
  top: 0;
  box-shadow: 0px 0px 15px black;
  -webkit-box-shadow: 0px 0px 15px black;
  background-color: #ffffff;
  overflow: hidden;
  height: auto;
  width: 325px; 
  float: right;
  left: 10px;
  z-index: 100000;
}
.box.closed {
  display: none;
  max-height: 0px;
}

A simplified version of the HTML structure for the two divs is as below:

<div class="moveablecontainer">
    <!--AVATAR GOES HERE-->  
    <div class="box opened">
    <!--CHAT WINDOW GOES HERE-->
    </div>
</div>

The positions can be visualized below:

INITIAL STATE:

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

BROWSER SMALLER:

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

EXPECTED BEHAVIOR:

My goal is to ensure that the chat window (child) stays with the avatar (parent) regardless of how the browser window is sized or resized.

Any insights on how to achieve this desired behavior?

Answer №1

In my opinion, the ideal approach in this situation would be to create a parent div encompassing both individual divs and then arrange them inside that single div. If the intention is for the two elements to be closely linked, it's best not to have one as a child of the other. This may require some adjustments to the css styling.

<div class="moveablecontainer">
    <div class="chatbox">
        <!--AVATAR WILL BE PLACED HERE-->  
    </div
    <div class="box opened">
        <!--CHAT WINDOW CONTENT GOES HERE-->
    </div>
</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

HTML tends to disregard the dimensions specified in the JavaScript file

I'm currently working on replicating an Etch-a-Sketch style drawing board where hovering over a single pixel with the mouse changes its color. However, I've hit a roadblock when it comes to drawing the board. The flexbox container doesn't se ...

Angular Material: Enable Window Dragging Across Multiple Monitors

Exploring the usage of Angular Material Dialog or any other Popup Window Component. The functionality is mostly working as expected, with the exception of the last point. a) The original screen should not have a grey overlay, b) Users should be able to i ...

Issue with video not displaying in EJS template on node.js server

I have successfully uploaded a video using a node.js server and saved the FilePath to MongoDB in my /public/uploads folder. Within my ejs file, I am trying to display videos using the following code snippet: //Videos is an array of objects retrieved from ...

Switching the background hue of the chat box after each message

Does anyone know how to change the colors of each message in a chat window using CSS and HTML? I'm trying to customize my stream but can't figure it out. Here is an example for reference. ...

Preserve the timestamp of when the radio query was chosen

I'm interested in finding a way to save the user's selected answer for a radio button question and track the time they saved it. Is there a way to achieve this using HTML alone? Or would I need to utilize another coding language or package? Just ...

Is there a way to access the body html element within a template in vue.js?

I'm struggling to add styles to the body element of my HTML page. It seems like I can't access it directly from the template because there's another body element nested inside the main body. Even when I try to change the style using JavaScri ...

Is it required for IDs in CSS to always correspond to a single element? How should IDs be handled if the same element is repeated multiple times on

Is it possible to have a menu that is visible at both the top and bottom of an extensive HTML page using the following code: <div id="menu"><ul><li>Home</li><li>About</li></ul></div> Would it be appropriate ...

Challenges with displaying content in Bootstrap tabs

I am currently working on the following code: HTML <div class="card"> <div class="card-header"> <ul class="nav nav-tabs card-header-tabs" role="tablist" id="tabs_list"> <li class="nav-item"> <a class="nav-li ...

"CanDeactivate Implementation Failure: Why isn't the Generic Function Being Called

I am currently working on implementing a guard to prevent users from navigating to the login page once they have authenticated themselves. This guard should apply to all page components in my app except for the login page. Below is the code snippet I am u ...

Guide on activating javascript code for form validation using php

How can I activate JavaScript code for form validation? I am currently implementing form validation on a combined login/register form where the login form is initially displayed and the register form becomes visible when a user clicks a button, triggering ...

Having trouble locating the Angular Material core theme within the asp.net core 2.0 template using Angular 5

CustomConfig.js const treeModules = [ '@angular/animations', '@angular/common', '@angular/compiler', '@angular/core', '@angular/forms', '@angular/http', '@angular ...

Ways to Implement Horizontal Scrolling in Dojo FilteringSelect Component

The select field on my form contains option values that are over 250 characters long, making it difficult for users to read them within the select box. Is there a solution to make the select field scroll horizontally or wrap the lengthy text? ...

What is the best way to automatically close a modal window after pressing the submit button

Having some trouble with my modal window using pure CSS. Can't seem to get it to disappear after clicking the submit button. Any suggestions on how to hide it properly? Here is the code snippet of both the HTML and CSS for reference. Open to all ideas ...

Transforming a Bootstrap 4 HTML project into Angular 9

After stumbling upon a beautiful HTML template that caught my eye, I realized it was built using Bootstrap. Luckily, I came across tutorials on how to convert such templates into Angular 6 projects, making the process seem quite straightforward (like this ...

Need help using JQuery to set the focus on the initial <select> element on a webpage?

Upon loading the page, I am looking to set focus on the initial element through JQuery. So far, I have managed to achieve this by using: $(":input:visible:first").focus(); or $(':input:enabled:visible:first').focus(); as described in a helpf ...

Ensuring the accuracy of HTML sliders

I am just starting out with programming and currently working on an online experiment using Django. In our experiment, sliders are being used extensively as input devices. We are trying to figure out a way to validate the sliders to ensure that participan ...

I have noticed that the brand section of the navigation bar changes to black when I hover over it, but I have been unable to locate any

I'm currently working on a Rails application that has a navbar-top with a brand element. However, I've encountered an issue where the background of the brand element turns black when I hover over it. Despite inspecting the browser console, I coul ...

Tips for creating a clickable title that directs to a URL

I am currently using the "Import All" WordPress plugin to bring in an Excel file containing numerous hyperlinks that point to specific Custom Post Types and a designated field. These hyperlinks are separated by commas from the corresponding title. For exam ...

The command "tsc" was not found in this bash session

Currently, I am using a MAC and attempting to set up TypeScript. I followed the installation process by running sudo npm install -g typescript and received the following output: Password: /Users/<myuserid>/node/bin/tsc -> /Users/<myuserid& ...

Unable to implement Bootstrap 5 Hamburger Animation - Looking for solutions

SOLVED I successfully resolved the issue :) The challenge was: $(function () { $('.navbar-toggler-button').on('click', function () { $('.animated-hamburger').toggleClass('open'); //Chrome expert mode ...