The issue of an HTML button positioned on top of an image not remaining fixed when the browser window is resized

After mastering the art of placing an html form element on top of an image, I am now facing the challenge of making sure that the button remains intact even when I resize the window.

For a reference, please visit:

Note: Despite its simplicity, I acknowledge that this question may seem trivial to some.

Answer №1

Although it may appear basic, the question at hand is far from banal. Many individuals struggle with this issue on a regular basis if they are not well-versed in handling it. The manner in which you position a form above an image can greatly affect the outcome.

  • Are you setting the image as the background of the Form?
  • Are you utilizing css absolute or relative positioning?
  • Are you employing javascript/jquery to position the element?

One crucial point to consider when layering one element over another is to ensure that it has been taken out of the document flow, typically achieved by using a position:absolute; or position:relative; declaration in your css. Once this is done, the element will be placed absolutely or relatively in relation to the nearest non-statically positioned element containing it. In the example snippet provided, you will observe that clicking inside the red box results in all elements moving to that location while maintaining their spatial relationship.

This response aims to address your query thoroughly.

var add = 20;
var leftCt = $("div#left_ct");
var cnt = $('div#container');
var bkg = $('div#background_relative');

cnt.on("click", function(e) {
  var left = e.pageX - cnt.position().left - 10;
  var top = e.pageY - cnt.position().top - 30;
  bkg.css({
    left: left,
    top: top
  });
  leftCt.html(left);
})
div#container {
  padding:10px;
  position: relative;
  background-color: salmon;
  width: 600px;
  height: 600px;
}
div#background_relative {
  display: block;
  margin: 10px;
  background-color: lightgreen;
  position: relative;
  width: 300px;
  height: 300px;
}
div#background_relative>* {
  left: 0;
  background-color: lightblue;
  position: absolute;
}
img {
  top: 20px;
}
form#form_over_image {
  background-color: yellow;
  z-index: 1;
  left: 10%;
  top: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='left_ct'></div>
<div id='container'>
  Container has position:relative
  <div id='background_relative'>
    Background has position:relative
    <img src='' alt='image has position:absolute' width=200 height=200 />
    <form id='form_over_image'>
      <div>form has position: absolute</div>
      <input type='text' value='' />
    </form>
  </div>
</div>

Answer №2

Instead of positioning the button absolutely relative to its containing div, you are currently placing it with respect to the entire window. This approach is not ideal when using an image as a background/layout in your scenario, making it difficult to achieve your desired outcome.

For more information, refer to this resource:

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 is the best way to shorten a string with jquery?

If the name of the string surpasses a certain limit, indicated by character count, three dots will be displayed. To achieve this, replace the last 3 characters with …. For example, if a name has 30 characters but only 15 can fit in the screen label field ...

Grid X Transformation 3: Dynamically alter grid cell background based on value (no need for CSS classes)

While working with GXT2, it was possible to dynamically change a cell's background color using the GridCellRenderer's render method. However, in GXT3, this feature no longer exists. The suggested approach is to utilize a GridViewConfig and overri ...

Load the entire AngularJS webpage using AJAX requests

I'm facing a challenge where I need to integrate an entire legacy page, along with some AngularJS elements, into our new page using AJAX. Initially, all I could see was the raw AngularJS markup: Rank ID {{$index+1}} {{player.playerid}} Afte ...

Align two DIVs in the correct layout: One as a sidebar that remains fixed, and the other as an expanding element

I am currently working on a code snippet to build a basic website featuring two main sections: a sidebar and the main content: html, body { height: 100%; margin: 0; } .container { display: flex; flex-direction: row; height: 100%; } .sidebar { ...

Retrieve all information contained within the HTML tags <div align="center"></div> using Java programming

I am new to Java and feeling a bit overwhelmed since I have no experience with it. With Selenium, I was able to download the HTML of a page and store it in a string. Now, my goal is to extract all the data between <div> tags and populate an array wit ...

What is the best way to stack text boxes vertically in CSS/HTML?

How can I arrange these textboxes so that .textbox appears at the top, followed by textbox1 below? CSS .textbox { border: 1px solid #848484; -moz-border-radius-topleft: 30px; -webkit-border-top-left-radius: 30px; border-top-left-radius: ...

Retrieve Element By Class Name in JavaScript

How can I modify the border color at the bottom of the .arrow_box:after? Javascript Solution document.getElementsByClassName("arrow_box:after")[0].style.borderBottomColor = "blue"; Despite trying this solution, it seems to be ineffective! For a closer ...

What could be causing the uneven application of CSS padding when it is applied to a div that serves as the parent of an HTML form?

Padding has been added only to the top and bottom of a div. The padding attribute is displaying consistently for the form element, however, it is not behaving uniformly for the parent div of the form as illustrated in the image provided below. Output:- h ...

How can I ensure that the content within a div does not overflow and that horizontal scrolling is enabled? What is the correct way

After designing my profile page, I encountered a problem related to horizontal scrolling caused by overflow. Despite trying various solutions like adjusting the width of .profilePg, I haven't been able to resolve this issue without disrupting the over ...

Animating CSS Pixel Fragments

After applying a simple CSS animation that moves size and box shadows from one side of the screen to the other, I am noticing residual pixel fragments left behind. To see this issue in action on Chrome 66, check out the Code Pen: https://i.sstatic.net/Gl ...

Issues with CSS properties not functioning correctly within the class.col function

When attempting to apply specific CSS properties to a particular area (e.g., "Dashboard") by assigning the class name "main" to the div.col function in the HTML, I encountered an issue where the CSS property applied affected the entire page. The following ...

Can you point me in the right direction for declaring the ImageObject format in Angular?

To create an Image Slider similar to the one shown here, I need to load images from a source. However, I'm unsure about where exactly in the file (possibly in the component.ts?) I should declare them: imageObject: Array<object> = [{ ...

Flexslider doesn't adjust the height of the viewport when displaying a shorter image in the slideshow

Is Flexslider not resizing its viewport height according to the current image on your site? The fixed height seems to be causing blank white space under shorter images. Are you inadvertently overriding Flexslider's responsive height function? Is there ...

Unresponsive HTML button

I'm currently developing an interactive text-based game that relies on button functionality to change text content. Everything was functioning perfectly until just a few moments ago, but now it has inexplicably stopped working. Here is the section of ...

Angular: NaNa: Attempting to access a property of an undefined variable

I've encountered these errors although the values are displayed correctly in the browser. I'm unsure about how to resolve this issue. ERROR TypeError: Cannot read property 'length' of undefined ERROR TypeError: Cannot read property &ap ...

Creating an accordion using HTML and jQuery allows for dynamic and interactive content organization

I am trying to incorporate an "ACCORDION" feature into my application using HTML and jQuery. Within this implementation, I have nested accordions. Below is a snippet of my code: <div class="row-fluid"> <div id="filtersContainer" class="span1 ...

How to place a stylish font over an image and recreate hover effects

I am looking to place social media icons below an image next to the photo's title, including Facebook, Twitter, Soundcloud, and Instagram. I want these icons to rotate along with the image when it is hovered over. HTML <div class="polaroid-image ...

Is there a way to customize the selected option in the autocomplete feature of Material UI components?

Is it possible to customize the CSS of selected options in Material UI autocomplete? Can this be achieved by utilizing the theme? ...

When the JS function 'postMessage()' is invoked on an HTML object tag, what specific action does it perform?

Not too long ago, I found myself on a quest to figure out how to trigger the print function on a PDF that I was displaying in Adobe AIR. With a bit of guidance from a helpful individual and by utilizing postMessage, I successfully tackled this issue: // H ...

Tips for fixing the position without affecting the width

I'm trying to figure out how to keep my #header fixed in position without it expanding in width. Despite checking my code multiple times, I can't seem to identify the factor causing my header to become wider and shift downwards unexpectedly. I in ...