Resizing images to cover % of div by surface area - angularjs

I'm working on an Ionic app. I have a two column layout of squares, for an example let's say they're 100x100. I'd like to rescale and then center (horizontally and vertically) images within those squares.
The images to put in them, however, do not have identical aspect ratios. I'd like to resize images to cover the same surface area, regardless of dimensions.
Again, for example, let's say that I want each image to cover 25% of my squares in the grid. If I have a 1000x1000 image, it would be resized to 50x50 to cover 25% of the area within a square. If I had a 500x250 image, it would be resized to ~70x35.
Mathematically I understand at least one way how to do this: image dimension * sqrt((image width x image height) / (0.25)(div width x div height)). It's in code with Angular, however, that I'm struggling.
Any advice?

I would try to use CSS if possible. It will be much lighter for the browser to render this and much easier for you to implement.
To do this, create boxes in your template and each box will have an image as a background. You can set that using ng-style attribute on the box.
In your CSS you could do something like this:
.image-box {
width: 50px;
height: 50px;
background-size: cover;
}

Related

Image elements do not have explicit width and height

Lighthouse keeps telling me that "Image elements do not have explicit width and height" eventhough i've tried to add it both in css or in img but the problem persists:
img.medium {
max-width: 29rem;
width: 100%;
}
or
<img
src={user.seller.logo}
alt={user.seller.name}
width="100%"
height="auto"
/>
How can i tackle this?
Short Answer
Add the image's native width and height in pixels as attributes on the image. This lets the browser calculate the aspect ratio for the image.
<img width="600" height="400" src="some-image.webp"/>
Long Answer
width: 100% does not give an element an explicit width.
What it does is define the width relative to it's parent container.
height:auto also does not give an image an explicit height, you are instructing the browser to make the image height whatever it thinks is appropriate based on the image proportions or the size of the container.
The biggest problem with this is the height of an image (as although the width of 100% is not explicit, it will be easy to calculate the actual width purely from the CSS).
When the page is requested the browser does not know the proportions of the image until it starts downloading.
For this reason the page renders (assuming you have inlined your critical CSS) then it requests the image and finds out how tall the image is. The container for the image will then change size to accommodate this image and you will get a layout shift, contributing to cumulative layout shift.
How to fix it
Option 1 - define the width and height using attributes
Option one is to define the image height and width as integers representing pixels by using attributes:
<img width="600" height="400" src="some-image.webp"/>
In modern browsers this will then be used to calculate an aspect ratio for the image and sufficient space will then be allocated on the page before the image starts downloading.
You can then use width:100%; height: auto; as you do now and it will all work as expected.
Using width and height attributes is the recommended best practice.
please note - the width and height values only have to be the correct aspect ratio if you are using CSS to override the dimensions (so width=200 height=100 would give the same result as width=400 height=200 assuming you set the width in the CSS).
Option 2 - use "Aspect Ratio Boxes"
In this technique you define the image height as a proportion of the width within your CSS.
In essence we give the image a zero height and then use padding to allocate the correct amount of space.
.image-div {
overflow: hidden;
height: 0;
padding-top: 56.25%; /*aspect ratio of 16:9 is 100% width and 56.25% height*/
background: url(/images-one.webp);
}
If you don't need to support Internet Explorer (as it is a little temperamental) you can use calc().
padding-top: calc(900 / 1600 * 100%);
This padding technique is explained in detail in this article from css-tricks.
Although this is a bit of a hack, the advantage is no inline attributes on your images, so it is useful for people who like to keep their HTML clean (and for certain scenarios such as if you want make all images the same aspect ratio)
The problem with both
There is only one issue with both techniques, you need to know the image width and height before they are rendered in order to calculate the aspect ratio.
There isn't much you can do to avoid this unless you are willing to make a fixed height and width container for the image.
Then if the image is a different aspect ratio to your container you will have some white space around the image but the page will not have a layout shift (which would be preferable in most circumstances) - assuming you use overflow:hidden etc. on the container.
.container{
width:50vw;
height:28.125vw; /*fixed height, doesn't have to be aspect ratio correct, see example 2 */
overflow:hidden;
margin: 20px;
}
.container2{
width:50vw;
height:40vw; /*fixed height, but this time there is extra white space (shown as dark grey for the example) below the image.*/
overflow:hidden;
background: #333;
margin: 20px;
}
img{
width: 100%;
height: auto;
}
<div class="container">
<img src="https://placehold.it/1600x900"/>
</div>
<div class="container2">
<img src="https://placehold.it/1600x900"/>
</div>

How to make homepage slider image not scale mobile?

My current homepage banner scales down to mobile and the image & text shrinks to a size that's unreadable.
How can you make it so that after it scales to a certain point the image stops scaling and it just cuts off the width of the image?
My site: www.riotsociety.com
Example site: sunglass.la
Add this into your CSS files.
This will make your slider 70% of the viewport height. So it will take 70% of any device's height.
this will solve the image problem
.fullwidthbanner-container {
height: 70vh; //please work around this value
}
Now the thing is that the Slider's JS is calculating and giving to each element sizes and styling's.
The buttons content becomes 3px in size on mobiles.
Now I see 2 solutions for this.
Read the documentation of the slider library or plugin if there is any and try to make it not give heights and values for the responsive part of the slider and just do it by your own. You'll need a little bit of CSS.
Or, let it as it is and write on top CSS.
Do you have access to the code? Or it's just a shopify website? I can see that the slider is owl_carrousel

How to maintain scroll position on window resize while using scrollmagic

I have a fixed container of 100vh x 100vw that contains 6 absolutely positioned divs that are 100vh x 100vw.
I use a dummy element that is positioned relative w/ 6 divs that are 100vh to trigger each scene.
When I resize the window, the scene's animation moves forward and backward in time. Is there a way to prevent that from happening?
The issue is that when you resize the window, the position of the scroll bar remains fixed at an absolute number of pixels. However, because your content is based on view height, the height of the document changes dynamically as you resize the window. Thus, the window scroll position as a percentage of the document body height is also changing, and this is what ScrollMagic bases progress on.
To illustrate with simplified math:
A) Given a window height of 1000px, your document height will be 6vh * 1000px = 6000px tall. If your window scroll position is 1200px, you'll be 20% of the way through the overall ScrollMagic timeline.
B) If you resize the browser to 800px tall, that changes the document height to 6hv * 800px = 4800px tall. However the window scroll position will remain at 1200px, which is now 25% of the way through the ScrollMagic timeline.
The simplest solution is probably to keep your document a fixed height.

Widths of footer/ navigation at 100% not equal to widths of header/ container at 100%

I'm building a website (summer-band.com) and trying to tweak the mobile settings (browser at < 600px) with 100% width settings for several of the elements. Unfortunately, with what I've done so far, the header/ container and the navigation bar as well as the footer all seem to be shooting out different widths and I can't seem to fix this on my own, thus asking for some assistance.
First, the menu: #navigation li a and #navigation li.current_page_item a's width is 100% but has a padding, so the box model won't do what you want. The width should be auto. This is a recurring problem of yours.
Next, the header: You have #headerImg's width set to a constant 600px. In your media query, you'll probably want to make the width and height auto and make the actual img's width 100%.
Moving down, your main #box has a width of 100% and a padding. Due to the box model, this probably won't do what you want. Make the width auto.
Further down, your #footer has a display of inline-block and a width of 100%. You'll probably want to change its display to block and width to auto.
I think that's mostly it, but you might want to set article img's max-width to 100% and remove the explicit width on your Kickstarter screenshot. I don't really know how to deal with the iframe with the video. Sorry.
A few last comments: You seem to be overusing brs and empty p tags rather than using appropriate margins.

How to set max-height to image's original height in a responsive design?

I'm building a responsive site using WordPress, and images are automatically set to height:100%; width:auto; and the div resizes according to window size. But this often means some images have their height set to higher than their actual height causing pixelation. What i need is something like height: 100%; width:auto; max-height:"image height here". How, if this is possible, is this possible?
The page where this is an issue is here: http://wheretoimmigrate.com/onthemouse/?portfolio=atomic-clothings-2012-campaign
I resolved it by simply making the images larger by adding a transparent background of larger size and different proportions. Tricked the system into treating it like a larger image. Worked perfectly.

Resources