I like this in the store, all have different heights, I want to set the height of each element to its width. When expanding a window, the following JQuery code works fine:

 function setHeight() { $('.product__img').height($('.product__img').width()); } $(window).resize(function () { setHeight(); }) 

enter image description here

However, when rendering a page, I can’t figure out how to set the height of an element. Render code of the React component:

  renderClothes(clothe, index) { return ( <div className="product__griditem" key={index}> <Link to={`clothes/${clothe.id}`} className="product "> <div className="product__img z-depth-1"><img src={clothe.image}/></div> <div className="product__info"> <span className="product__name">{clothe.name}</span> <span className="product__disc">{clothe.description}</span> {this.setRating(clothe.rating)} <span className="product__price">${clothe.price}</span> </div> </Link> </div> ) } render() { const data = this.props.clothes console.log('data', data); return ( <div className="content"> <div className="product__grid"> { data.map((clothe, index) => this.renderClothes(clothe, index)) } </div> </div> ); } 

    1 answer 1

    You don't need js here. Is there an opportunity to put a picture of the background? Make a block, through the padding, the desired height, then the background image (well, or through the image, but absolute). On media queries, set the appropriate proportions.

     body { background-color: gray; } .product__img__items { display: flex; flex-wrap: wrap; } .product__img__item { width: 23%; margin-left: 1%; margin-bottom: 25px; } .product__img { height: 0; width: 100%; padding-top: 100%; background-color: #fff; border-bottom: 3px solid green; background-size: contain; background-position: center center; margin-bottom: 5px; background-repeat: no-repeat; } 
     <div class="product__img__items"> <div class="product__img__item"> <div class="product__img z-depth-1" style="background-image: url(https://besplatka.ua/aws/15/99/56/78/app/b2835e5c5c7b.JPG)"></div> <div> Lorem ipsum dolor sit amet. </div> </div> <div class="product__img__item"> <div class="product__img z-depth-1" style="background-image: url(https://besplatka.ua/aws/15/99/56/78/app/b2835e5c5c7b.JPG)"></div> <div> Lorem ipsum dolor sit amet. </div> </div> <div class="product__img__item"> <div class="product__img z-depth-1" style="background-image: url(https://besplatka.ua/aws/15/99/56/78/app/b2835e5c5c7b.JPG)"></div> <div> Lorem ipsum dolor sit amet. </div> </div> <div class="product__img__item"> <div class="product__img z-depth-1" style="background-image: url(https://besplatka.ua/aws/15/99/56/78/app/b2835e5c5c7b.JPG)"></div> <div> Lorem ipsum dolor sit amet. </div> </div> <div class="product__img__item"> <div class="product__img z-depth-1" style="background-image: url(https://besplatka.ua/aws/15/99/56/78/app/b2835e5c5c7b.JPG)"></div> <div> Lorem ipsum dolor sit amet. </div> </div> </div> 

    • Thanks, good way. But it is interesting how this is done on the reactor - Alexander Alekseev
    • Well, what's the problem? put the same code in componentDidMount, or replace it with es6 - Yuri Kopot