What is causing the input element to not properly align with the image?

I am facing a challenge where I want to place two input elements inside an image element. However, after setting the position of the input elements to absolute, only the left input successfully entered inside the image element while the right one remained outside.

I am curious as to why this is happening and I am in need of a solution to ensure that both inputs are placed correctly inside the image.

.container-fluid {
  text-align: center;
  position: relative;
}

img {
  display: inline;
  max-height: 100%;
}

.myB {
  position: absolute;
  top: 50%;
}
<div class='container-fluid'>
  <input class='myB' type='button' value='<'>
  <img src="https://www.w3schools.com/w3css/img_mountains.jpg" class="img-fluid" alt="Responsive image">
  <input class='myB' type='button' value='>'>
</div>

Answer №1

There are a few issues that need to be addressed:

  1. To prevent layout inconsistencies in older browsers, specify the position of the buttons rather than relying on the browser's default behavior.
  2. Make sure to set the container to inline-block so it doesn't take up the entire width of the page.

.container-fluid {
      position: relative;
      display:inline-block;
      width:auto;
    }
    
    img {
      max-height: 100%;
    }
    
    .myB {
      position: absolute;
      top: 50%;
    }
    .myBRight{
      right:0px;
    }
    .myBLeft{
      left:0px;
    }
<div class='container-fluid'>
      <input class='myB myBLeft' type='button' value='<'>
      <img src="https://www.w3schools.com/w3css/img_mountains.jpg" class="img-fluid" alt="Responsive image">
      <input class='myB myBRight' type='button' value='>'>
</div> <!-- container-fluid -->

Answer №2

If you're looking for help with adjusting the position of a button or input relative to an image, here's a solution. By setting the position of the element to be within the dimensions of the image, you can ensure that it aligns correctly. Although the background of the button may overflow from the image, tweaking the positioning by reducing the pixel value can help place the arrow in the correct spot.

img{
  width:400px;
  height:300px;
}


#next,
#prev{
  position:absolute;
  top:inherit;
}

#next{
  left:400px;
}
  
      <button id="prev" type="input" > < </button>
      <button id="next" type="input" > > </button>
      <img src="https://www.w3schools.com/w3css/img_mountains.jpg" class="img-fluid" alt="Responsive image">


  

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

Utilizing plain HTML, the input type date feature can be customized to restrict the minimum date selection to January 1,

What is the proper way to define a minimum date for <input type="date"> in HTML? I attempted using the min attribute, but it did not produce the desired result. Appreciate any help on this matter. ...

When hovered over, the Dropdown Menu vanishes into thin air

I'm looking to implement a simple CSS dropdown menu on my website. Here is the code: #topNav { font-size: 12px; width: 970px; margin: 0 auto; height: 33px; background: url(images/menubg.png); /*border-bottom: solid 1px #cccccc;*/ } #topNav ul { margi ...

Choose characteristics based on attributes, where the value after the colons is undefined

Hey everyone, I've been exploring ways to select HTML elements like: <div attr:something="else">Random content</div> I discovered that you can target these by using: document.querySelectorAll('[attr\\3A something]') ...

Newbie inquiry: How to utilize classes in HTML for CSS implementation?

I'm a beginner in the world of HTML and I have what might be considered as a basic question... Here is the style definition for my header in the CSS file: h1.heading { color: indianred; } I attempted to link it to my HTML file like this, but unfo ...

Displaying a div based on the response after it is received using an if/else statement

In my form, each question is on a separate page (div), with the ability to show and hide pages. If a user receives a response from the API with a status of "accepted", they are redirected to a URL. I'm currently trying to display a div if their status ...

Restore the initial content of the div element

Currently, I am utilizing the .hide() and .show() functions to toggle the visibility of my page contents. Additionally, I am using the .HTML() function to change the elements inside a specific div. $('#wrap').html(' <span id="t-image"> ...

Activate a pointer cursor when hovering over a TextField in Material-ui design

Hey there! I'm new to Material-ui/ReactJS and I have a question. I'm trying to change the cursor to a pointer when hovering over a Material-ui TextField, but it's proving to be quite challenging. The default behavior is 'cursor: text&ap ...

Drag Bootstrap panels to the edge of the column

I need to position two panels in a column, with one on the left and the other on the right. Currently, they are not aligned properly, as shown in this image: https://i.stack.imgur.com/RU636.png The first panel should be moved to the left to align with the ...

The HTML modal validation feature on Spring is malfunctioning as it is vanishing unexpectedly

After setting up a login and registration feature for my blog website, I encountered an issue where the website would reload whenever a user tried to log in or register. This caused the modal to disappear, making it impossible to display any error messages ...

Troubleshooting: jQuery Drop event is not triggering

I am attempting to transfer links (.link) from one div (.folder) to another, but the drop event is not triggering. To make all .link divs droppable areas, I have disabled the default behavior in the dragenter and dragover events. Here is the code snippet: ...

Issue encountered while attempting to import three.js in static context

Trying to statically import three.js for a test website. I'm a bit rusty with html/js so something seems off here. Following static import from Code below (all in one html file): <script type="module"> // Find the latest v ...

Manipulating CSS animations using JavaScript

Recently, I've been working on a basic keyframe animation. The animation is set to blink truck lights: animation: blink-truck-lights .4s 8s 10s steps(2) 2 forwards; @keyframes blink-truck-lights { from{background-position: 0px 0;} to{background-pos ...

SOLVED: How to utilize CSS to confine scrollbars within nested div elements

As a novice in web programming, I am currently working on a userscript to add hotkeys to a website. I am using Firefox on Linux for this project, focusing solely on desktop functionality. Right now, my main focus is on enhancing the pop-up help view, which ...

Is it possible for Google to crawl links that are generated using JavaScript?

I have developed four websites using HTML, CSS, and JavaScript without utilizing any server-side languages like PHP. My main goal is to acquire backlinks with various anchor text. To achieve this, I have decided to use a single JavaScript file on all my w ...

Issue with bootstrap accordion not collapsing other open tabs automatically

I'm struggling to incorporate a Bootstrap accordion collapse feature into my project, specifically with the auto-collapsing functionality. In the scenario where there are 3 divs labeled A, B, and C, if A is open and I click on B, A should automaticall ...

Instructions for developing an HTML element slider using mouse dragging

I've come across plenty of slider plugins that either only allow clicking to view the next image, or if they do support mouse drag or touch capabilities, they are limited to images. Does anyone know of a plugin or method to create a mouse drag slider ...

Can you explain the meanings of <div class="masthead pdng-stn1"> and <div class="phone-box wrap push" id="home"> in more detail?

While working on styling my web pages with CSS and Bootstrap, I came across a few classes like "masthead pdng-stn1" and "phone-box" in the code. Despite searching through the bootstrap.css file and all other CSS files in my folders, I couldn't find a ...

Determine the total quantity of colored cells within a row of an HTML table and display the count in its own

I need assistance with a table that has 32 columns. From columns 1 to 31, I am able to fill in colors by clicking on the cells. However, I now want to calculate and display the number of cells that are not colored in the last column. This task must be co ...

Which specific scoped CSS selector should I use to modify the width of the mdDialog container?

Is there a way to override the max-width of 80vw set on .mat-dialog-container in an Angular Material dialog? I want to create a truly full-width dialog. The issue lies with scoping--Angular-CLI compiles component CSS using scope attribute selectors. This ...

Is it possible to utilize props within a computed property in order to apply a CSS class binding?

Can props be utilized within a computed property in an alternative manner? Upon attempting this, it seems to add the class 'foo' (the props name) instead of the intended 'fa-foo' (or a different value if props were configured). If I d ...