Tips on keeping an HTML element from shifting when resizing the browser window (how to lock an element in a specific position on a webpage)

As a newcomer to front-end web development, I recently attempted creating a basic responsive page. While working on this project, I encountered an issue with the candle holder image. This image is size-responsive, meaning it adjusts in size as the browser window is resized. However, I noticed that the position of the image changes as well, causing the candle holder to shift relative to its original position on the full-screen layout.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>form</title>
    <style>
        body{margin: 0px;}
        #main{
            background-color: hsl(308, 62%, 39%);
            width: 100vw;
            height: 100vh;
            position: relative;
        }
        #blendaText{
            color: white;
            position: absolute;
            font-size: 3.5vw;
            top:50px;
            left: 25px;
        }

        #form-container{
            background-color: white;
            height: 100vh;
            width: 70vw;
            float: right;
            border-radius: 20px 0px 0px 20px;
        }
        #candleImage{
            position: relative;
            top:200px;
            left: 200px;
            width:30%;
            height: auto;
        }
        #form{
            margin-left: 100px;
            margin-top: 100px;
        }
        input{width: 90%;}
        input[type="submit"]{margin-left: 5px;}
    </style>
</head>
<body>
        <div id="main">
            <p id="blendaText"> Blenda allows <br> you to imagine 3D </p>
            <img id="candleImage" src="candle.png" alt="candle holder">
            <div id="form-container">
                <form id="form">
                    <label for="fname">First name:</label><br>
                    <input type="text" id="fname" name="fname" value=""><br>
                    <label for="lname">Last name:</label><br>
                    <input type="text" id="lname" name="lname" value=""><br><br>
                    <input type="submit" value="Submit">
                  </form> 
            </div>
        </div>
</body>
</html>

https://i.sstatic.net/epaGQ.png https://i.sstatic.net/Yjnxt.jpg

After searching on Stackoverflow, I came across these 1 and 2 related questions. Unfortunately, the solutions provided did not resolve my issue. I also experimented with different positioning techniques such as absolute, relative, fixed, and sticky, but none of them proved successful. Here's the candle image for reference.

Answer №1

If you want to position the image using dynamic units, you can update the #candleImage styles as follows:

Here is the revised CSS:

#candleImage {
                position: relative;
                top: 20vh;
                left: 15vw;
                width: 30%;
                height: auto;
            }

Answer №2

To apply relative units in a similar manner as you did for the left purple sidebar, please refer to the updated CSS for the #candleImage selector provided below:

<!DOCTYPE html>
<html lang="en>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>form</title>
    <style>
      body {
        margin: 0px;
      }
      #main {
        background-color: hsl(308, 62%, 39%);
        width: 100vw;
        height: 100vh;
        position: relative;
      }
      #blendaText {
        color: white;
        position: absolute;
        font-size: 3.5vw;
        top: 50px;
        left: 25px;
      }
      #form-container {
        background-color: white;
        height: 100vh;
        width: 70vw;
        float: right;
        border-radius: 20px 0px 0px 20px;
      }
      #candleImage {
        position: absolute;
        bottom: 10vh;
        left: 30vw;
        width: 30%;
        height: auto;
        transform: translateX(-50%);
      }
      #form {
        margin-left: 100px;
        margin-top: 100px;
      }
      input {
        width: 90%;
      }
      input[type='submit'] {
        margin-left: 5px;
      }
    </style>
  </head>
  <body>
    <div id="main">
      <p id="blendaText">
        Blenda allows <br />
        you to envision 3D
      </p>
      <img
        id="candleImage"
        src="https://i.ibb.co/ZxJcXP5/candle.png"
        alt="candle holder"
      />
      <div id="form-container">
        <form id="form">
          <label for="fname">First name:</label><br />
          <input type="text" id="fname" name="fname" value="" /><br />
          <label for="lname">Last name:</label><br />
          <input type="text" id="lname" name="lname" value="" /><br /><br />
          <input type="submit" value="Submit" />
        </form>
      </div>
    </div>
  </body>
</html>

Answer №3

If you're experiencing issues with zooming in and out on your webpage, consider implementing Flexbox for a smoother layout. Flexbox can be a lifesaver in various future scenarios. Alternatively, you can opt to use a table to structure your website.

Here's a simple example using Flexbox:

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>form</title>
        <style>
            body{margin: 0px;}
            #main{
                display: flex; 
                flex-direction: row;
                align-items:stretch ;
                align-content:stretch;
                background-color: hsl(308, 62%, 39%);
                width: 100vw;
                height: 100vh;
            }
            #blendaText{
                color: white;
                font-size: 3.5vw;
                top:50px;
                left: 25px;
            }
            #form-container{
                flex-shrink: 3;
                background-color: white;
                height: 100vh;
                width: 70%;
                border-radius: 20px 0px 0px 20px;
            }
            #column{
                width: 30%;
                flex-shrink: 3;
            }
            #candleImage{
                position: relative;
                margin-left:1vmin;
                height: 25vw;
            }
            #form{
                margin-left: 100px;
                margin-top: 100px;
            }
            input{width: 90%;}
            input[type="submit"]{margin-left: 5px;}
        </style>
    </head>
    <body>
            <div id="main">
                <div id="column">  
                    <p id="blendaText"> Blenda allows <br> you to imagine 3D </p>
                    <img id="candleImage" src="https://i.ibb.co/ZxJcXP5/candle.png" alt="candle holder">
                </div>  
                <div id="form-container">
                    <form id="form">
                        <label for="fname">First name:</label><br>
                        <input type="text" id="fname" name="fname" value=""><br>
                        <label for="lname">Last name:</label><br>
                        <input type="text" id="lname" name="lname" value=""><br><br>
                        <input type="submit" value="Submit">
                      </form> 
                </div>
            </div>
    </body>
    </html>

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

"Selecting the hue of an image on an input

Is it possible to easily incorporate color images into a design? I am struggling to find the property that will allow me to do so. There is a button that contains an image. .button { background: url('../image/arrow.png') 189px no-repeat ...

Adjusting Size Dynamically for Tooltips Based on Content Length

In my angular app using ng-bootstrap v4.2.1 on an older codebase, I have successfully created a tooltip with some very long sentences as content. I am trying to make the tooltip 800px wide, but when I set the width, only the first few words fill the space. ...

What is the best way to enable my search function to filter out specific items from a table?

Currently, I have created a table populated with data fetched from a JSON file. Now, my focus is on implementing a search functionality that filters out items based on user input and displays only those table rows matching the search criteria. The code sni ...

Is there a way to target a sibling element of another element by using its identifier in Cypress?

My current task involves clicking a checkbox within a list of table rows. The only way I can think of reaching this level is by targeting the span tag along with its corresponding name. cy.get('tr > td > span').contains('newCypressTes ...

Ways to run a template multiple times within a single handler function

I'm working on implementing a progress bar that dynamically updates based on the progression of a command line argument within my Go code. I've been using a template to update the progress value every second by calling template.Execute(w, data) i ...

What is the process for updating the CSS style of every other piece of data (such as an image) retrieved

Is it possible to utilize PHP code when retrieving data from a MySQL database in order to have every other record display with unique CSS formatting? For instance, I am incorporating images similar to this in my comment system: http://prntscr.com/3hpiep ...

Tips for applying unique CSS classes to individual templates in Joomla

I manage a website at www.kadamjay.kg using an RT template system where each language version has its own unique template. However, when I add a class to a specific element, it only changes on one template and there are no additional templates in the pat ...

Creating an additional webpage other than index.html

I recently set up my own VPS and launched a web server using NGINX. Right now, I have an index.html page live on my website. However, I'm unsure of how to go about creating additional pages like an About page that can be accessed at www.my-domain-name ...

Adding a new element with Jquery when a dropdown option is selected

What is causing this issue to not function properly? <script> $(document).ready(function(){ $('#custom_field option').click(function(){ $('#custom_field_input').append('<tr><td></td> ...

elements overlap when styled differently

Hello all, I'm in need of some assistance with arranging the items on my webpage. As a beginner, I require detailed explanations as some things may be obvious to you but not to me :) Specifically, I am looking to enhance my header design. My goal is t ...

Pedaling back and forth along a sequence

Is there a way to implement forward and backward buttons for a clickable list without using arrays, as the list will be expanding over time? I have already achieved changing color of the listed items to red, but need a solution to navigate through the list ...

Transferring Data Between Tables in ASP.NET Core

I'm in the process of creating a new online store with ASP.NET Core. I have a requirement to transfer data from the Products table to the Items table upon clicking a button, so that the item information can be displayed on the shopping cart page. Any ...

Is there a way to organize nested tabs in R markdown so they are displayed separately instead of all being shown side by side?

Hello, I have a question regarding separating sub tabs and not displaying them all side by side. I have tried multiple codes to create two subtabs, each containing a different graph, and then displaying the other two sub tabs below them with different type ...

What is the best way to format a pseudo-element to serve as the header for a paragraph?

We are utilizing Markdown (Kramdown) for generating a static website. When incorporating infoboxes, we can enhance paragraphs and achieve the following outcomes: {:.note .icon-check title="This is a title"} Lorem, ipsum dolor sit amet consectetur adipisici ...

The settings of the button return to their default state once it is clicked on

I've been working on a small project and I added a button with hover and CSS effects. However, the issue is that once the button is clicked, it reverts back to its basic state without any of the CSS properties applied. I attempted to troubleshoot if ...

Tips for synchronizing JavaScript rendering time with Python requests?

My goal is to retrieve the HTML content of a JavaScript-generated website so that I can extract information using BeautifulSoup. However, when I attempt to request the HTML, the data I receive is from before the page fully loads. Below is the code snippet ...

On my website, two elements are stacked on top of each other

The CSS framework I am currently utilizing is Materialize Whenever I try to add a new element, it inexplicably appears below the ones above it. I did not specifically instruct it to be positioned underneath or below the cover container. Adding a z-index c ...

What are the different ways to customize the indicator color of React Material UI Tabs using hex

Just got my hands on this amazing Material UI component <Tabs textColor="primary" indicatorColor="primary" > <Tab label="All Issues"/> </Tabs> The documentation states that indicatorColor and textColor can only be set to ' ...

Guide to transferring text to clipboard using PHP and JS

Forgive me if this sounds a bit silly. I've been trying to figure this out for a while now, and it seems like the only solution I found involves clicking some sort of button. I have a form that generates license keys, and after generating one, I wan ...

Utilizing fab-icons with CDN in Next.js version 13.4

Currently, I am working with the latest version of Next.js (13.4) and I would like to incorporate a single Icon into my project using Font Awesome CDN to avoid increasing the overall app size. However, when I include the following code snippet in my layou ...