How to properly resize absolutely positioned elements using CSS

Can anyone advise on how to properly scale down elements with absolute positioning so they maintain their proportions when the screen size is reduced, without using media queries? I have included an image below showing the current desktop view.

https://i.sstatic.net/FaH4V.png https://i.sstatic.net/uONiS.png

Here is the JSX code:

     <div className={styles.relativeParent}>
          {/* <img src="/home_6.png" alt="lamp" className={styles.lamp} /> */}
          <div className={styles.obj_container}>
          <img src="/home_lamp.png" alt="lamp" className={styles.lamp} />
          <img src="/home_10.png" alt="plane" className={styles.plane} />
          {/* <img src="/home_7.png" alt="lamp-light" className={styles.lampLight} /> */}
          <p className={`${styles.homeSubHeading} ${styles.narrowHeading}`}>
            Finding it difficult to start? We’re here to{" "}
            <span className={styles.gradient}>help</span>{" "}
          </p>
          </div>
          <p className={`${styles.homeContent} ${styles.narrowText}`}>
            At Raisze, whether you’re a student, designer, young professional,
            restarting your career or a visionary and dreamer, as long as you’re
            a startup builder, you will find support. We help early-stage
            startups validate, raise funds and find first customers with a
            platform that enables reward based crowdfunding.
          </p>
        </div>

And the corresponding CSS code :

.obj_container{
  position: relative;
}

.lamp {
  position: absolute;
  top: -1.5rem;
  right: 22rem;
  width: 9rem;
}

.plane {
  position: absolute;
  width: 9rem;
  top: 2rem;
  left: 13rem;
}

.lampLight {
  position: absolute;
  width: 10rem;
  top: -2rem;
  right: 25rem;
}
.homeSubHeading {
  margin: 0;
  margin-bottom: 1rem;
  font-size: 3rem;
  font-weight: bold;
}

Answer №1

Follow these steps to resolve the issue:

  • To avoid image distortion, ensure the height of images is set to auto.

  • For proportional resizing of images in relation to the parent element (.obj-container) during screen resize, utilize percentage values for widths.

  • To prevent unpredictable positioning or displacement of images during screen resize, employ percentage values for positioning. (Positioning based on rem means aligning the <img>s with the font-size of the parent element or browser if the parent element does not have a specified font-size. While this method may work at certain screen sizes, it might not be sufficient. It's crucial to position elements relative to the parent element's dimensions using percentages.)

  • In order for points 2 and 3 to function correctly, you must assign a width and height to the parent element of the <img>s (.obj-container).

Here is an example (using ellipsis ... for brevity):

.lamp {
    ... 
    top: -1.5%;
    right: 22%;
    width: 10%;
    height: auto;
}

.plane {
    ... 
    top: 2%;
    left: 13%;
    width: 15%; 
    height: auto;
}

.lampLight {
    ... 
    top: -2%;
    right: 25%;
    width: 10%;
    height: auto;
} 

.obj_container {
    position: relative;
    width: 100vw;
    height: 60vh;
    max-height: 80vh;
} 

The values for top, left, right, and width given for the <img>s are examples; feel free to adjust them as needed.

Additionally, note that the .obj-container div has a width of 100vw, occupying the entire viewport width. The height is set to 60vh with a maximum height of 80vh to accommodate smaller screens. You can modify these values according to your requirements.

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

What are the best ways to clean up redundant code when creating a form with Material UI components?

When designing my form, I utilized Material UI for the task. While it functions perfectly fine, I can't help but ponder if there are alternative methods to streamline the code. As it stands, my output consists of roughly 800 lines of code. Below is ...

React and TypeScript are not in sync: Expecting 0 arguments, but receiving 1 in a useReducer function

Greetings! I'm currently facing some challenges while trying to implement a useReducer in a TypeScript application. I have encountered several errors (all related to the reducer), but one error stands out as the most common throughout the entire app. ...

Transmit form data via Ajax request

Thank you for your interest. I am looking to retrieve $_POST['Division'] from a dropdown list: <select name="Division" onchange="setImage2(this);"> <option value="1">Bronze</option> <option value="2">Silver</op ...

React Material-UI Table - Universal and Group-Wide Application

I want to implement the functionality of "Apply To All" and "Apply to GroupBy" for the rows I'm working with using Material UI. I need to create a button for "Apply to All". This button will update all the columns on a row when the user makes chang ...

Can Next.js accommodate server-side redirection with internationalization?

I'm working on a small Next.js app that has pages accessible only to logged in users. To manage the authenticated routes, I created an HOC (withAuth) that handles redirection on the server side to prevent page flashing on the client side. Everything i ...

Storing user input in an existing text file using ASP.NET MVC

What steps should I take to store user form input from a view into an already existing text file using the ASP.NET MVC framework? ...

Aligning Content in the Header

Currently, I am attempting to place a logo on my WordPress site right at the top of the header above the menus and in the center. Even though I've positioned it at the top, I'm struggling to align it horizontally in the center. I've experime ...

Determining the height of a Bootstrap column in relation to another element

Utilizing the grid layout from bootstrap, I have the following structure: <div id="prof_cont_enclose"> <div class="row"> <div class="prof_cont_row"> <div class="col-xs-12 col-sm-4 col-md-2 col-lg-2 prof_elem"&g ...

Error TS2339: The type 'never' does not have the property 'getBoundingClientRect'

While optional chaining should suffice, I may have gone a bit overboard in attempting to satisfy TypeScript: const ref = useRef() if (ref !== undefined) { if(ref.hasOwnProperty('current')) { if (ref.current !== undefined ...

Is there a way to verify the existence of a document in Firebase?

Is there a way to verify if an ID is present in my Firebase collection? I'm trying to determine whether the item's ID exists within the "favorites" collection that includes multiple IDs. I want to set a value to true if it exists or false if no ...

Update the HTML body table by duplicating the element

On my HTML page, the body includes a table with data in each cell. I also have another similar page, but this one doesn't have any data in its table cells. Whenever I add a value to a cell on the first page, it sends a POST call to update the data in ...

JavaScript can be utilized to monitor outbound clicks effectively

I am currently working on implementing the code found at this link: Make a ping to a url without redirecting. The original poster is looking to ping a URL without opening multiple windows. My goal is to achieve the same functionality, but I also want to vi ...

Navigate to a fresh HTML page upon form submission

I'm completely new to the world of web apps and JavaScript, and I'm facing an issue where my form submission doesn't redirect me to a thank you page. Instead, it just leads to a blank page every time. I've tried researching solutions on ...

Elimination of encapsulating div in React

After using this.setState to update the object, I encountered a problem. It appears that my object is initially updated with the new one after changing state. However, when it returns to a function called by render which maps the component's "view," ...

Issue with rendering React route component on webpage

I am facing an issue with my search component in my React application. The component is supposed to redirect the user to the page of the Pokemon they searched for, but the route is not rendering the component as expected. I have also set up the value typed ...

Graphql-ws: a distinct websocket connection is established for every subscription made

As a newcomer to websockets, I am using urql and graphql-ws (transitioned from subscriptions-transport-ws) for graphql subscriptions. Below is the code snippet: export const useUrqlClient = () => { const headers = useHeaders(); const client = useMem ...

Challenges with the adjacent sibling combinator (+) in Internet Explorer

I've encountered an issue with the adjacent sibling combinator (+) in my CSS. It seems to function properly in Firefox but not in Internet Explorer. Here is the problematic code snippet: .mycheckbox:checked + label { // Implement a change on the la ...

Is there a way to center text that is aligned to the left within a div?

Is there a way to center text that is left aligned? I'm struggling to achieve the following effect: This text is left aligned. This text is left al ...

Unable to get spacing correct on loading page

My attempt at creating a loading page using CSS and HTML has hit a roadblock. I'm trying to show a loading bar that ranges from 0% to 100%. Despite my use of justify-content: space-between, I can't seem to get it right. I've searched through ...

store JSON data within an HTML list element

I've been working on a JavaScript script that allows me to generate list items in HTML. Right now, I have functionality to drag and remove the items using another script. However, I am interested in adding some additional information to each list item ...