Comments (4)
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.
@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.
Thank you @samwyzz , I will try this.
from react-masonry-css.
@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)
- [Masonry] The property "columnClassName" requires a string - Error shows in Chrome console HOT 2
- Publish react 17 peer dependency fix to npm HOT 5
- very different columns height HOT 1
- ie11 support HOT 2
- Add support for css modules HOT 3
- 'Masonry' cannot be used as a JSX component HOT 2
- breakpoint for column not working HOT 1
- Is there a way to draw a solid vertical line between columns? HOT 1
- Add a way to emulate `min-width` instead of `max-width` alike media queries for breakpoints checks HOT 4
- Does anybody use with next/image? it doesn't work. HOT 4
- Fixed width according to number of breakpoints
- Long item in first column causes gaps in other columns HOT 5
- Latest version of TypeScript causing errors HOT 1
- Changing column count calculation to depend on container width instead of window width HOT 2
- Add support for logic operator regarding responsive columns
- some elements can occupy more than one column
- Thank you
- Next.js Elements being in components breaks column render
- Balancing of total items height in each column is broken :-( HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-masonry-css.