Issues with zoom functionality on absolutely positioned image

My goal is to zoom in on an image that is contained within a div element.

Upon page load, I display a 300x300 pixel image inside a 400x400 pixel div.

To center the image within the div, I am using the following CSS:

#img1{
  width:300px;
  height:300px;
  position:absolute;
  margin:auto;
  top:0;
  bottom:0;
  left:0;
  right:0;
}

With this CSS code, the image displays at the center of the div successfully.

However, when a user clicks the zoom button, I increase the dimensions of the image. If it becomes 600x600 pixels, I need to enable a scroll bar so the user can navigate the enlarged image within the div.

To implement this scrolling functionality, I set overflow:auto for the div.

The issue arises when trying to view the entire expanded image by scrolling through the div. This problem could be related to the position:absolute property applied to the image. How can I address this?

I have also created a fiddle displaying two divs with the image before and after zooming. You can view it here:

http://jsfiddle.net/codingsolver/L4qdL/1/

Answer №1

Could you please give this a shot?

HTML

<div class="outer">
    <div class="inner">
        <img id="img1" src="http://siliconangle.com/files/2012/04/HTML5_Wallpaper_1680x1050.png" />
    </div>
</div>
<button class="zoomout" data-zoom="out">Zoom Out</button > 
<button class="zoomin" data-zoom="in">Zoom In</button >

CSS

.outer{
    height:400px;
    width:400px;
    overflow:auto;
    border:1px solid black;
    float:left;
    margin:30px;
    text-align:center;
}

.inner {
    height:400px;
    width:400px;
    display:table-cell;
    vertical-align:middle;
}

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

http://jsfiddle.net/L4qdL/7/

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

Difficulty arises in designing a tableless layout due to the issue of auto

Wondering why my page is not expanding with its content? Check out my demo on Fiddle http://jsfiddle.net/msas3/. The light blue area should determine the overall height of the page. ...

The !important rule in CSS seems to be malfunctioning

I've been struggling to change the background color of my table rows. Here's what I attempted: <tr style="background-color:#000099 !important;"> Unfortunately, this resulted in no change: After hours of trying and searching online for so ...

adjust the @page rule size for different classname matches during printing

I am attempting to adjust the @page rule size based on matching classnames, however, my research has only led me to pseudo-classes which do not seem to solve my issue. Here is a snippet of my CSS: @media print { @page { margin: 13.2mm 0mm 12.9mm; ...

Ways to incorporate HTML elements into a forthcoming HTML element (React or JavaScript recommended)

As I work on enhancing the accessibility of some HTML content that is loaded through a third-party application, I find myself faced with the challenge of adding accessible elements to dynamically spawned list items with anchor tags. My attempt to achieve ...

Choose a sibling element using CSS styling

As I'm developing an AngularJS component, I am on the quest to identify ANY sibling of a particular tag, irrespective of whether they are div, span, or p. I'm hoping for a CSS-native solution that resembles something along the lines of div:siblin ...

The ajax method for loading an xml file results in displaying the undefined message

When attempting to fetch content from an xml file using ajax, the page initially displays "undefined". However, upon refreshing the page, the content loads successfully. Take a look at my code snippet below: $.ajax({ type: "GET", url: "xm ...

Dropdown sidebar with collapsible sections

I am looking to create a unique navigation system for my website. I have a standard HTML list with menu items that will serve as anchor links on a long-scrolling page. My idea is to design it as a minimal vertical side navigation bar, where each item initi ...

The absence of jQuery is causing an error in the Twitter Bootstrap code

I am encountering an issue where I am receiving an error message stating that jQuery is not defined. My intention is to utilize both twitter bootstrap and jQuery-UI in my project. In the <head> section, I have included all the necessary CSS and JS fi ...

How to position two divs in a row using Bootstrap

Looking at the output below: https://i.stack.imgur.com/pQM8F.png The blue circle is placed in one div, while the text is in another. I'm perplexed by why the first div appears on top and the second doesn't seem to align properly. Here's th ...

IE11 causing overflow issues with flex item set to 0% basis

This straightforward example can be viewed in the following codepen link: http://codepen.io/anon/pen/Mazjyv. It showcases a button that acts as a flex item with a flex-basis set to 0%. While other browsers handle the content within the button container wi ...

Using absolute positioning for child elements within a table cell division in CSS

Similar Question: Is it possible to position items relatively or absolutely within a display: table-cell? I am working with a display: table layout example. You can view the demo here. In the second panel, there is a #pos div that has a position: abs ...

What is the best way to align the text in the center of my h1 header?

Can anyone assist me with centering the contents of my h1 tag? h1{ width: 100%; vertical-align: middle; color: rgb(5, 5, 5); font-size:25px; letter-spacing: 0px; top: 0; text-align: left; padding: 10; }h1:after { content:' '; display: ...

The hamburger icon shifts position upon being clicked

My hamburger button is causing me some trouble. Every time I click on it, the position shifts. I need it to stay in place when clicked. I have shared all my code on jsfiddle. Click here for code const nav = document.querySelector('nav&a ...

implementing a delay after hovering over a CSS hover effect before activating it

I'm trying to achieve a specific effect using JavaScript or jQuery, but I'm struggling to figure it out. I have created a simple CSS box with a hover effect that changes the color. What I want is for the hover effect to persist for a set amount o ...

How come my dimensions are not returning correctly when I use offset().top and scrollTop() inside a scrolling element?

Currently in the process of developing a web app at , I am looking to implement a feature that will fade elements in and out as they enter and leave the visible part of a scrolling parent element. Taking inspiration from myfunkyside's response on this ...

Tips for transferring a value from a function in ASPX to a CS file

I seem to be overlooking a small detail. I have a dropdown list (DDL) and a function in my C# file. I want to pass the 'value attribute' of a selected DDL option to my function, but I can't seem to retrieve the value attribute. I checked the ...

A tutorial on implementing a "Back to Top" button that appears dynamically while scrolling in Angular

I've developed a scroll-to-top feature for my Angular project, but I'm facing an issue. The scroll icon appears immediately upon page load instead of only showing after the user scrolls down. Any ideas or suggestions on how to achieve this? Here ...

Issue with flex-grow property not functioning as expected in Internet Explorer 11

Hello! I'm running into an issue with flexbox in Internet Explorer: http://jsfiddle.net/EvvBH/ I've noticed that the #two element has flex: auto, which is meant to make it expand to fill the container, even if there's limited content. Ho ...

How does the use of <ol> with list-style-type: disc; differ from that of <ul>?

Why create a separate <ul> when visually both unordered lists and ordered lists look the same? <head> <title>Changing Numbering Type in an HTML Unordered List Using CSS</title> <style> ol { list-s ...

Current polyfill available for requestAnimationFrame

Recently, I read on that requestAnimationFrame in Chrome 20 now has a new sub-millisecond precision timer. It looks like I need to update my code to accommodate this change. After checking out various polyfills, it seems like they were created before thi ...