Is it possible to resize an image while also adjusting the size of its container using CSS?

I have a block of content with an image sandwiched between paragraphs.

<p class="bodytext">aslkjfsdlkfj f ldflskl</p>
<p class="image"><img src=1.png/></p>
<p class="bodytext">aslkjfsdlkfj f ldflskl</p>

The HTML does not specify the dimensions of the image, they are derived from the image itself. I am looking to resize the image using CSS:

img {
    transform: scale(0.7);
}

However, this only scales the image and not the container box, causing a large gap between the text and the image. I want to remove this gap.

  • Adjusting top/bottom margins has no effect
  • There is a 'transform-box:' property that seems like it should help, but changing the settings doesn't make any difference.

Scenario: Utilizing CSS Paged Media in Antennahouse Formatter.

Answer №1

Solution #1 using zoom

One effective method is utilizing the non-standard zoom CSS property, which provides the desired functionality. The zoom property will scale the entire element, including its contents, and can be applied to various types of HTML elements: block, inline, replaced elements (images), or text.

img {
  zoom: 75%;
}

Despite specifications recommending the use of "transform: scale() instead", it's worth noting that the two properties function differently, with "zoom impacting the layout size of the element" - precisely what is needed in this scenario.

The browser support for the zoom attribute is robust, as indicated by CanIUse's report of 94.5% support. It should be noted that while zoom is a non-standard feature, avoid employing it on production sites; nonetheless, major browsers (with the exception of Firefox) do support it. For situations like generating HTML materials for printing purposes (e.g., PDFs), this solution works well.

View the functioning JS Fiddle here: https://jsfiddle.net/FurloSK/8bhzkrw2/

EDIT (Refer to comment from OP below)

Solution #2 utilizing transform: scale() with fixed-size container

There exists a technique to enforce scaling on both the image element and its container, albeit with certain considerations:

  1. Primarily, it involves manipulating multiple CSS attributes, a practice that might not yield intended results when dealing with specific software (Antennahouse Formatter) rather than common browsers like Chrome.
  2. Secondly, specifying the container size manually is necessary, potentially contradicting the objective of automatic determination. Nonetheless, I'm outlining this approach for reference, acknowledging its applicability to questions regarding scaling images within containers although impractical in the current context.

HTML snippet:

<div class="fixedContainer">
  <img class="smallScale" src="https://via.placeholder.com/100"/>
</div>

CSS snippet:

.fixedContainer {
  display: table-cell;
  position: relative;
  overflow: hidden;
  width: 80px; /* 100px * scale(0.75) = 75px */
  height: 80px; /* 100px * scale(0.75) = 75px */
}
.fixedContainer .smallScale {
  position: absolute;
  top: -9999px;
  bottom: -9999px;
  left: -9999px;
  right: -9999px;
  margin: auto;
}

Take a look at the functional JS Fiddle here: https://jsfiddle.net/FurloSK/8bhzkrw2/

Solution #3 Applying width property with auto-fit-sized container

This alternative approach appears promising, free from cumbersome hacks. By resizing an image via CSS width using a percentage value and aligning the outer container's width to match its elements through fit-content, the container adapts based on the original image dimensions. Consequently, the nested image element gains a reference point for interpreting "100%," allowing proportional scaling.

Note that without fit-content, the div would lack individual sizing, causing the image's percentage width to refer to the entire page or the closest ancestor with a defined width.

HTML code:

<div class="fitContainer">
  <img class="smallWidth" src="https://via.placeholder.com/100" />
</div>

CSS code:

.fitContainer {
  width: fit-content;
}
.fitContainer .smallWidth {
  width: 75%;
}

For a demonstration, visit the operational JS Fiddle: https://jsfiddle.net/FurloSK/8bhzkrw2/

Answer №2

Here is a special instruction for Antennahouse Formatter that allows you to scale an image along with its container:

img {
-ah-content-width: 70%;
}

If needed, you can also utilize -ah-content-height, where the scaling will maintain proportionality by default.

This feature was intentionally crafted to replicate the behavior outlined in XSLFO: thereby resulting in the image being scaled to n% of its intrinsic size.

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

Various results can be produced based on the .load() and .resize() or .scroll() functions despite using the same calculation methods

I'm currently working on developing my own custom lightbox script, but I've hit a roadblock. For centering the wrapper div, I've utilized position: absolute and specified top / left positions by performing calculations... top: _center_ver ...

The transparent background causes the vertical menu to malfunction on IE8

This is the first website I am working on: www.olgoya.com The issue I am facing is that all major browsers, except IE8, display the submenu even when the mouse hovers over the 'portfolio' item. Upon investigation, I found that the problem lies w ...

Generate dynamic lines with evolving hues using CSS3

While working on a fresh web project, I came across the need to emphasize my headers (h1, h2, h3, h4) with underlines. My goal is to have these underlines grow and change color dynamically, similar to what can be seen here: . Is there a way to achieve thi ...

Steps for making a grid within a bootstrap 3 modal

I am new to HTML and CSS I'm working on creating a modal content similar to this: https://ibb.co/RvrhmRs Most of the work is done, but my modal currently looks like this: https://ibb.co/1zG0y2t I just need help arranging an icon next to the text. ...

What is the method for toggling background URLs?

Currently, I am utilizing flaunt.js by Todd Moto for my navigation system. My goal is to seamlessly switch the hamburger image with another image when the mobile-menu is shown and hidden. Check out the demo here. For a detailed tutorial, visit this link. ...

What could be the reason for JavaScript code successfully exporting to Excel using the old office extension .xls but encountering issues when trying to export

I am currently working on exporting an HTML table to Excel using JavaScript. I have encountered an issue where it does not export to the newer version of Excel with the xlsx extension. However, it works fine and exports to older versions of Excel with the ...

Having trouble loading NEXT JS images when the file path is stored in a variable?

I'm working with Next.js for my coding project and I have a array of images that I need to incorporate into my application. CODE1 <Image src={require("../assets/image1.png")} /> This code snippet works perfectly fine and displays the ...

The CSS show and hide feature is effective across all browsers except for Safari. What steps can be taken to address this issue

My toggle trigger works perfectly in Chrome, but I'm having trouble getting it to work in Safari. Any suggestions? <a href='#show'class='show'>see more --></a> <a href='#hide'class='hide&apos ...

The picture is not visible outside the parent container - why is it hidden?

Currently, I am working on project cards and using materialize css's image card to style them. One of the features that I particularly like is how it resizes the picture when included inside a container. This allows me to set a fixed height without wo ...

Having trouble positioning a specific element at the top right of the header navbar in Laravel Bootstrap 5

Is there a way to create a top navbar with the project name and menu on the left, and user management on the right? Currently, both options are aligned to the left. Here is an example of what I have: top navbar This navbar is placed in the header, and her ...

Conceal descendant of list item and reveal upon clicking

In my responsive side menu, there is a submenu structured like this: .navbar ul li ul I would like the child menus to be hidden and only shown when the parent menu is clicked. Although I attempted to achieve this with the following code, it was unsucces ...

Combining Sass and SCSS in webpack: A step-by-step guide

Just curious about something. I have some libraries I'm interested in using (for example, font-awesome) that use scss, but I personally prefer working with sass. How can I set up my project with webpack to accommodate this? Here's my current s ...

Expanding the width of the textfield is not possible in CSS styling

My current search bar design features an input area that does not match my expectations. The input length is restricted and starts from the center rather than the black border I had envisioned. If you'd like to see the code and design in action, chec ...

Utilizing CSS to create dynamic 3D cube transformations

Exploring the realm of CSS 3D transforms to showcase cubes and cuboids featuring cross-sections has been my current venture. In this endeavor, I'm aiming at compatibility with Chrome, Firefox, and MSIE 11. It has come to my attention that in order to ...

Troubleshooting: ngAnimate max-height failing to apply to specific element

Having integrated ngAnimate into a project to enhance animations, I've encountered some unusual behavior with a specific element. Let me provide some context: our website features a shop with categories and products within those categories. I am usin ...

I'm trying to determine which jQuery event would be more beneficial for my needs - should I go with

I'm facing a dilemma On my website, I need to capture the value of a span within a modal. The value changes when the modal is opened and reverts to the old value when closed. This particular value represents the cart total in my online store. Wheneve ...

Error Encountered in Laravel 5.2 FormBuilder.php

Encountering Laravel 5.2 ErrorException in FormBuilder.php on line 1235 stating that the method 'style' does not exist. This error has me perplexed. I have already made updates to my composer.json file by adding "laravelcollective/html": "^5.2" ...

Solving alignment issues by utilizing a clever negative margin-left technique for compact windows

I've managed to center-align a div using the following method: <div id="container" style="position:absolute; top:0px; left:50%; margin-left:-500px; width:1000px;"></div> After referring to this solution Trying to center div horizontally ...

Characters from Font Awesome are invisible on my screen

I am struggling with the font awesome plugin as I am unable to view the characters. Below is my code snippet: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <l ...

Clicking on the images will display full page versions

Apologies for not making an attempt just yet, but I'm struggling to locate what I need. Here's the menu structure in question: <div class="overlay-navigation"> <nav role="navigation"> <ul class="test"> & ...