Image with fluid dynamics centered in a div - resize simultaneously with div

I'm working on a fully fluid web page with a parent DIV set to width: 100%. Inside this div, there's an image with the following style:

<div>
     <img src="image.png" alt="" />
</div>

<style>
    div { width: 100%; }
    img {
      display: block;
      margin: auto;
      max-width: 100%;
    }
</style>

The image is centered within the div, but there is white space between the borders of the image and the div since the image is smaller than the parent DIV. When I resize the browser window, the image does not resize until the parent DIV reaches the image borders.

If I were to set the image's width to 100%, it would stretch beyond its actual size...

I want the image to scale immediately with the div when the browser window is resized. If the image canvas were set to 100% of the parent div, I could achieve this desired behavior. However, I am unable to set images up in that way...

Answer №1

Using pure CSS alone may not achieve this effect, but you can incorporate jQuery for the desired outcome - http://jsfiddle.net/VcJ6s/

var aspectRatio = $("img").parent().width() / $("img").width();

$(window).resize(function() {
    $("img").width( $("img").parent().width() / aspectRatio + "px" );
});
​

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

Toggle the visibility of table rows based on a specific class using a checkbox

I am currently working on a code that displays the results of a query in a table format. I would like to have the ability to click on certain elements to show or hide them. I know this can be achieved using toggle and CSS, but I keep encountering obstacles ...

Getting the css property scaleX with JQuery is as simple as executing the

I am struggling to print out the properties of a specific div element using jQuery. My goal is to have the different css properties displayed in an information panel on the screen. Currently, I am facing difficulties trying to show scaleX. Here is my curr ...

Tips for ensuring Accordion menu closes upon clicking a second time

I created a basic Accordion menu using just HTML and CSS. The functionality works well, but I'm facing an issue where clicking on an open accordion doesn't close it. Can this be achieved with CSS alone or would I need to use JavaScript? .midd ...

Troubleshooting a CSS layout issue: Creating a list with images, text, and animated image scaling

I am looking to create a two-column list that consists of an image on the left and a headline and text on the right. The images have a CSS scale transformation set on onMouseEnter in ReactJS code. The issue arises when using float:left for the images to a ...

Utilizing transitions to control the visibility of div elements

This is what I have achieved so far: function getOffsetRect(elem) { var box = elem.getBoundingClientRect() var body = document.body var docElem = document.documentElement var scrollTop = window.pageYOffset || docElem.scrollTop || body.sc ...

Tips for obtaining the sources of every image on a webpage and consolidating them in a single section

My goal is to preload all images on a webpage into a single division before the page loads. For example, if there are 5 images on the page (eg1.png, eg2.jpg, eg3.bmp, eg4.jpg, eg5.png), I want them to be contained within a div with the id 'pre'. ...

Adjusting the body element's width and background hue

My goal is to adjust the background for the body element. Although I have modified the width of body, I anticipated the background to align with this width, yet it is expanding to fill the entire screen. How can I rectify this issue? body { border: 1px ...

Emotion (Mui) in Next.js project does not properly apply inline stylesheets in Chrome

Having trouble applying classes from Inline stylesheets generated by emotion(MUI) in the latest Chrome version (v129). The issue is only present in this specific version of Chrome, as it works fine in other browsers and even in Chrome versions <=128. I ...

hover-related problems when using bootstrap

I am experiencing an issue where the functionality works properly without using the link to bootsrap.css. <link rel="stylesheet" type="text/css" href="css/bootsrap.css"/> However, when I include this bootstrap link, the mouseover feature stops work ...

Expanding Width and Height Using CSS3 Flexbox

Looking to create a fullscreen calendar app using the CSS3 Flex model. While I've managed to fit the calendar nicely horizontally, I'm struggling to find a way for the weeks to stretch equally in height too. Check out this jsFiddle for a visual ...

Adjusting the header background color and inserting a logo once a specific threshold is reached

Currently, I am designing a website where I need the background color to transition from transparent to black and display a logo once the user scrolls down to a certain point. I am a beginner in javascript and I am facing some difficulties with this task. ...

Slide the toggle icon to activate the add-to-cart button on click!

Is it possible to achieve the functionality of sliding a plus icon to an add to cart button on click, instead of hover? I have attempted using CSS without success. Would JQuery be able to accomplish this task? codepen.io HTML <div class="socialIcons" ...

Is there a way to keep my fixed button at a consistent size while zooming on mobile devices?

There are no definitive answers to the questions regarding this issue. Some suggest stopping zoom altogether, while others recommend setting the width, which may not always solve the problem. I am working on a web application designed for mobile use with ...

What methods can I employ to dynamically style SVG elements that have been generated programmatically?

Utilizing a js library called mermaid to create svg on a webpage, I am in need of dynamically applying styling to specific parts of the svg when users activate different commands through keyboard shortcuts. Specifically, I aim to highlight the element in t ...

Using the "checked" attribute in an HTML input to hide content with CSS when blocks are positioned in different

Is there a way to connect input with buttons similar to the code below, or is it impossible without changing the HTML (unless using JS code)? Please don't critique the HTML code, this is just an example from a website, I'm trying to det ...

Modify the style of .asp files (CSS)

I need to change the orientation of an .html page using a .css file, but I only have access to the .aspx file and not its code. The challenge is that the .css file is loaded dynamically from an .axd file. How can I go about changing the .css file in this ...

Element with a fixed position within an iScroll container

Has anyone encountered an issue with fixing an element with position: fixed inside a big Iscroll page? It seems like the element scrolls along with the content due to the transition imposed by Iscroll. I'm trying to fix the "FIXTHIS" element, but it ...

What is causing my HTML wrapper to malfunction?

Below is the HTML code for my website: <div id="wrapper"> <div id="header"> <img src="images/MyBannerFinished.jpg" alt="Header" width="803" class="headerimage" /> </div> <!--- End Header ---> <div id="navbar"> <ul ...

Expanding the flexbox container to accommodate additional divs when they are added

I'm encountering an issue with a div not extending properly, causing two other divs to overlap. I've managed to position the divs correctly, but now I need the "100% all beef weenies" text to appear below the items. Any suggestions on how to achi ...

Mastering the art of selecting checkboxes and Font Awesome icons with JQuery

I find selecting elements using JQuery by their ID or classname quite challenging and it's a bit outside of my expertise. When a function is called, I would like to apply the following styles: .classname input[type="checkbox"] ~ i.fa.fa-circle-o { ...