Resize an image using calc() css

Resize an image using calc() css



I'm trying to resize any image with existing height and width properties by a percentage (and keep proportions). I know that transform: scale(1.05) would take an image that's 100px by 100px and make it 105px by 105px, but it would still only occupy the original 100x100 space in page flow.



How would I do something like:


<img src="an.svg" width="100" height="100" alt="bigger please" class="resize" data-width="100" data-height="100"/>

img.resize
height: auto;
width: calc(original width * 5%);



So that the browser renders an image that's 105px by 105px and that occupies the full 105x105?



I'm using simplified numbers for this question, but the images could have any value for either dimension.



Additionally, I can not use a wrapper or a background image, but I do have access to data-height and data-width attributes present on the images. Codepen is here: https://codepen.io/spicedham/pen/qMKLYq






A 100px by 100px image transformed with transform: scale(1.05) will occupy 105px by 105px in page flow by default. If your image doesn't, you have additional constraints such as max-width: 100%. Please ensure you have provided a minimal, complete, and verifiable example of the problem.

– Obsidian Age
Sep 13 '18 at 22:28



100px


100px


transform: scale(1.05)


105px


105px


max-width: 100%






@ObsidianAge scale is a visual transform so the layout/flow will not change and content will not get pushed when the image will grow. Also max-width will have not effect on scaling

– Temani Afif
Sep 13 '18 at 22:31







@Obsidian Age Added a codepen here: codepen.io/spicedham/pen/qMKLYq that shows how scaling doesn't work for me - but very possible I'm not using it correctly.

– spicedham
Sep 14 '18 at 1:53





4 Answers
4



I don't think you can use the width/height attribute of an image to define new width/height. You can probably consider inline styles and CSS variable like this:




img
width:calc((var(--width) * 5/100 + var(--width))*1px);
height:auto;


<img src="https://picsum.photos/100/100?image=1069" height="100" >
<br>
<img src="https://picsum.photos/100/100?image=1069" height="100" style="--width:100">






I can't use inline styles, but I do have access to data-height and data-width attributes. Could those be used as variables? codepen.io/spicedham/pen/qMKLYq

– spicedham
Sep 14 '18 at 1:55






@spicedham unfortunatly you cannot with pure CSS, the only CSS solution I see here is inline syle+ CSS variable ... I think you will need to rely on JS, it should be easy in this case

– Temani Afif
Sep 14 '18 at 7:49



If it's not totally necessary for the img tag to be utilized (and the image is always the same aspect ratio if it's dynamic) then you could use a div with a background image, and give it height: 0; and padding-bottom: 100% (or whatever percentage that would create the appropriate aspect ratio) and modify it that way with css transforms


img


height: 0;


padding-bottom: 100%



Assuming you have a container available that the image is placed in, you can easily use calc to get the height and width for the image



check out this pen to see an example.
https://codepen.io/calebswank11/pen/gdKBRE


.container
width: 300px;
height: 300px;

img
display: block;
width: calc(100% + 5%);
height: calc(100% + 5%);
left: -2.5%;
top:-2.5%;
position: relative;






it's 5% not 5px

– Temani Afif
Sep 13 '18 at 22:37






So use 5% instead of 5px. Will be the same concept there. Takes 5% of it’s parent. And change the -2.5 to percent as well to center

– Caleb Swank
Sep 13 '18 at 22:39







it's not for me .. and you should probably focus on the fact that you are using SASS and not pure CSS

– Temani Afif
Sep 13 '18 at 22:44







fair enough, updated answer and pen to reflect pure css.

– Caleb Swank
Sep 13 '18 at 23:15






I can't use a container. Codepen here: codepen.io/spicedham/pen/qMKLYq

– spicedham
Sep 14 '18 at 1:52



After exhaustively trying every permutation I could think of, I do have a solution of sorts. It requires two things I was trying to avoid (a container & inline styles), but it works as part of a system. Some additional background: I work on a web app that lets users set a base font size (think 12, 14, 16, or 18pt) and then also handles zooming at on top of that with a range from a 10% to 300% for low vision users. We have some images (mostly math expressions) that are embedded within the surrounding page content as SVGs. At default print and zoom levels an SVG with the number '3' in it is the same size as plain text number 3 next to it. But things get mismatched as the other variables start to change. The technique described below, once in place, will let us have fine-grained control over how these images match up with surrounding text regardless of print size or zoom level.



Here's a link to solution in codepen https://codepen.io/spicedham/pen/pxzYYe and a variation using... variables https://codepen.io/spicedham/pen/MPgxxo.



Here's the CSS:


.container
display: inline-flex;
vertical-align: middle;
border-left: solid .05em transparent;
border-right: solid .05em transparent;

.scaleMe
transform: scale(1.1);
margin: .05em 0;



And here's the what the images and containers look like:


<span class="container" style="font-size: 300px;"><img class="scaleMe" src="svg.svg" width="300" height="100" alt="" style="font-size: 100px"/></span>

<span class="container" style="font-size: 100px"><img class="scaleMe" src="svg.svg" width="100" height="200" alt="" style="font-size: 200px"/></span>



The problem I ran into whether I used calc() or transform:scale() was that I could not get content to reflow around the resized image consistently - an image scaled up would overlap adjacent content. It was possible if all the images were the same size or they all had the same proportions, but that's not something I can count on in our system.



The other problem was that using percentages as units, while the logical choice, does not work as you'd expect.



So the solution was to create a stand-in relative unit of em. I took the width of the image (say 300px) and set it as the font size for the container. I then took the height of the image (say 100px) and set it as the font size of the image. This allows me to prop open the container to occupy the same space as the scaled image. Gets around the limitation of not being able to use percentages for border widths and accurately set the equivalent of a percentage for top and bottom margins on the image.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)