Tips for maintaining the position of items with absolute positioning while resizing:

Hello, I am seeking help with an issue in my code.

My problem is as follows:

I have two items and one frame. The frame has a position relative to the two items (children) which have positions set to absolute. Ideally, these children should always remain at the same point inside my frame, but when I resize the window, their positions become different in relation to the frame. How can I ensure that the children always stay in the same position for responsive design? Is this even possible?

Feel free to experiment with changing both the width and height of the window in the provided example: https://codesandbox.io/s/adoring-jackson-c6fth?file=/index.html:0-900

.frame {
  width: 70vh;
  height: 90vh;
  border: 10px solid red;
  margin: 10px auto;
  position: relative;
}

.objA {
  width: 130%;
  position: absolute;
  height: 40%;
  bottom: -10%;
  left: -20;
  border: 2px solid green;
  background: rgba(10, 101, 10, 0.7);
  z-index: 2;
}

.objB {
  width: 10%;
  position: absolute;
  height: 20%;
  bottom: 20%;
  left: 30%;
  background: red;
  z-index: 1;
}
<div class="frame"></div>
<div class="objA"></div>
<div class="objB"></div>

To provide a better understanding of the issue, I have included an actual image of the scenario I am facing:

[![RealCase][1]][1]

All objects are positioned absolutely (Waves, stars, robot, etc.). Each wave should maintain the same position regardless of resizing. It is worth noting that each wave is independent.

Answer №1

Ensure that the positions and dimensions are defined in percentages, with a crucial emphasis on placing your items within the primary

<div class="frame"></div>
element. You can kickstart the process as follows:

* {
    box-sizing: border-box;
}

body {
    background: none #014653;
    margin: 0;
}

.frame {
    width: 70vw;
    height: 90vh;
    border: 10px solid #5cb9b8;
    background: none #5cb9b8;
    margin: 5vh auto;
    position: relative;
    border-radius: 6px;
}

.frame-stage {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    border: 5px solid #a2cece;
    background: none #014653;
    border-radius: 5px;
}

.objA {
    width: 10%;
    position: absolute;
    height: 20%;
    bottom: 10%;
    left: -20;
    border: 2px solid green;
    background: rgba(10, 101, 10, 0.7);
    z-index: 2;
}

.objB {
    width: 10%;
    position: absolute;
    height: 20%;
    bottom: 30%;
    left: 30%;
    background: red;
    z-index: 1;
}
<div class="frame">
  <div class="frame-stage">
    <div class="objA"></div>
    <div class="objB"></div>
  </div>
</div>

Check out the updated codesandbox here.

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

Tips for customizing the appearance of a specific cell in the first column of a Material UI table

Currently, I am modifying the material ui table found in this codesandbox link. My goal is to adjust the style of the first column's cell in the table when it is selected to match the image provided below. https://i.sstatic.net/DGubN.png It seems lik ...

I'm curious about the specific purpose of /1 in font styling at 16px

Can anyone explain the purpose of /1 in the font: 16px/1 style declaration? I removed the /1 from the code and noticed increased spacing between lines. What is the specific role of /1 in this situation? body { font: 16px/1 'Open Sans', sans ...

What is the best way to find a specific class within a CSS file using Visual Studio Code?

Currently, I am working with a bootstrap template on my webpage. I am interested in personalizing specific sections of the design, but unfortunately, I am having trouble identifying the relevant CSS rules within the extensive .css file. My research online ...

Customize the keyboard on your iPod by replacing the default one with a

Looking to customize the iPOD keyboard to only display numbers, similar to a telephone keypad: https://i.stack.imgur.com/X0L3y.png The current iPOD default keyboard looks like this: https://i.stack.imgur.com/VykjU.png ...

A useful tip for jQuery users: learn how to prevent the context menu from appearing when a text box is

Is there a way to prevent the context menu from appearing in jQuery when the text box is disabled? The right-click disable functionality works well in all browsers except for Firefox. Please note that the right-click disable functionality works when the t ...

The Ineffectiveness of Social Media Sharing in WordPress

I'm encountering an issue with adding social media sharing buttons to my website's product page. Although I've implemented the PHP code, clicking on the Facebook button only redirects to a page displaying a share button and URL, but it doesn ...

Media queries for aspect ratios across all browsers

I have been exploring ways to modify background-size from 'contain' to 'cover' using media queries when the browser width results in an aspect ratio of less than 4:3. This adjustment creates a background image that may not be fully vis ...

Utilizing float positioning in Responsive Web Design

Attempting to implement a two-column float CSS layout with specific width percentages. My CSS styles for the two columns are as follows: .div1 { width: 25%; float: left; } .div2 { width: 75%; float: right; } .container { width: 960px; } @media scr ...

displaying the local path when a hyperlink to a different website is clicked

fetch(www.gnewsapi.com/news/someID).then(response => newsurl.href = JSON.stringify(data.articles[0].url) fetch('https://gnews.io/api/v3/search?q=platformer&token=642h462loljk').then(function (response) { return response.json(); }).th ...

Showing XML content with JavaScript

Trying to parse a simple XML list using JavaScript, but having trouble formatting it the way I need. <TOURS> <MONTH> <TOUR> <NUMBER>1</NUMBER> <VENUE>City Name - Venue Name</VENUE> < ...

Issue with displaying YouTube videos in HTML causing them to be downloaded instead of playing

I'm currently facing an issue with loading a video on my HTML page using the following code: <video v-for="(data, key) in projectData.videos" :key="key" width="320" height="240" controls> <source :src="data.url"> </video> One ...

The CSS transition fails to function correctly when rendering a React element within an array

When rendering a React component within an array using CSS transitions, I noticed that the elements in the array re-order and change style. Surprisingly, only the elements moving up have transitions applied, while the ones moving down do not. I expect all ...

Use of number type input on mobile devices in HTML

My goal is to display the up/down arrows alongside an input element with type="number" in Chrome on Android. I've adjusted the CSS to set opacity=1, but unfortunately, they are not appearing. On desktop, however, they show up without any iss ...

What is the best way to loop through <div> elements that have various class names using Python 3.7 and the Selenium webdriver?

I am currently in the process of creating a Reddit bot that provides me with a detailed report about my profile. One task I need to accomplish is identifying which of my comments has received the most likes. Each comment on Reddit is wrapped in elements w ...

Display and conceal individual divs using jQuery

Despite my lack of experience with jQuery, I am struggling with even the simplest tasks. The goal is to display/hide specific messages when certain icons are clicked. Here is the HTML code: <div class="container"> <div class="r ...

What is the procedure for submitting all checkbox values through Google Apps Script?

My web app created using Google Apps Script can generate a custom table of data for users to select for calculations. I use Google Sheets to populate the table with values and checkboxes, but note that the table size may vary depending on the user. <fo ...

Show and hide HTML div elements dynamically using jQuery based on the selection from a dropdown

I am attempting to display or hide various divs based on user input from a select drop-down box. In fact, I initially tried to directly use the code from jQuery dropdown hide show div based on value, but I seem to be missing something simple that is preven ...

Eliminating Unnecessary Stylesheets in Vite and Vue Website

When working on the Vite-Vue application, I noticed that the styles I implemented for both index.html and Vue components/views are showing up as crossed out in the browser. Additionally, many of the styles I added to the components/views are also being cro ...

When the canvas is dragged, an item within it suddenly leaps or bounces

I've noticed that when I drag the canvas using -webkit-transform: translate(x,y), an element on the canvas jumps. Are there any suggestions for addressing this issue? ...

IE and Chrome are experiencing issues where the radio buttons are disappearing

Here is the style sheet code: select,input ,td a {border:1px solid green; width:25%;height:25px;font-size:20px;margin- left:.1em;} input.myradio {border:none;width:0%;height:0%;font-size:0%;} And here is the corresponding HTML code: <td><inpu ...