Giter Site home page Giter Site logo

Comments (4)

samwyzz avatar samwyzz commented on September 21, 2024 1

For this, one way is the bottom padding technique. This will of course require knowing before hand the width and height of the image to calculate the images aspect ratio in percent. That is, how much percent is the images height compared to its width. For example, say, an image Is a "tall" image, then its height would be maybe 150% more than its width. This is just an example, and the dimensions will of course vary from image to image.

We apply a padding bottom to a container div which contains the image element, calculated from the images width and height . So while the image has not loaded the height of the container div is proportionate to the actual image when it has loaded.

function myImageComponent({imageWidth, imageHeight}){
   const aspectRatioPercent = (imageHeight/imageWidth)*100;
   return (
   <div style={{
      paddingBottom: `${ aspectRatioPercent }%`,
      backgroundColor: "red"
     }}
   >
      <img src={...} />
   </div>
   )
}

The following articles explain this concept in more depth:
https://www.andyshora.com/css-image-container-padding-hack.html
https://css-tricks.com/aspect-ratio-boxes/

A little short of time atm, this example is a little bit rushed and im sure there are things that can be improved. If this is useful and need any help, feel free to ask.

Edit: in this example the img element must be positioned absolutely otherwise it will add to the height of the cell once loaded

from react-masonry-css.

vishwakarmanikhil avatar vishwakarmanikhil commented on September 21, 2024 1

@samwyzz Thank you for an update. I have tried your solution but It was not what I am looking for. Because if for small things we start using the libraries then it increases the build size anyway. I have found this on stack overflow and use that. But anyway you have elaborate your solution problem clearly. which helps me to found the solution.

from react-masonry-css.

vishwakarmanikhil avatar vishwakarmanikhil commented on September 21, 2024

Thank you @samwyzz , I will try this.

from react-masonry-css.

samwyzz avatar samwyzz commented on September 21, 2024

@vishwakarmanikhil
As it showed in the example above before editing, I have clearly never been a math expert :p. And to add I was severely burned-out when writing my comment. Now that I look at it, the hook I had created above had a redundant calculation of the image ratio. The calculate aspect ratio package is not necessary since the width and height of the image are the aspect ratio.

The padding percentage can simply be calculated using the following formula (Image Height / Image Width) * 100. It is still required to know the width and height before hand.

I have updated my answer with the corrections.

Cheers!

from react-masonry-css.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.