如何在React JS的网格系统中以正确的尺寸(宽度和高度)呈现图像。

huangapple go评论65阅读模式
英文:

How to render images with proper dimension(width and height) in a grid system of React JS

问题

我正在制作一个电子商务网站,在这个网站上我需要放置产品的图片。所以我在React中使用了简单的CSS Grid系统,但在渲染图片时,它们呈现出不同的大小,因为我从网上获取了图片,所以它们没有以适当的方式调整。

请检查我的代码:

return (
    <>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gridGap: "10px",
      alignContent: "center", padding: "20px", margin: "20px" }}>
        
        {filteredClothing[0].products != null &&
          filteredClothing[0].products !== undefined &&
          filteredClothing[0].products.map((item) => (
            <>
              <div style={{}}>
                <Card
                  className="card_image"
                  key={item.id}
                  style={{ width: "70%", height: "100%", padding: "10px", margin: "10px" }}
                >
                  <div style={{ display: "flex", flexDirection: "column", flexWrap: "wrap" }}>
                    <div>
                      <CardMedia
                        component="img"
                        image={item.image}
                        alt={item.name}
                      ></CardMedia>
                    </div>
                    <div>
                      <p>Cost: {item.price}</p>
                      <p>{item.description}</p>
                    </div>
                  </div>
                </Card>
              </div>
            </>
          ))}
      </div>
    </>
);

在UI中,它看起来像这样:UI中的产品图片

所以我尝试在DOM中定位元素,但图片的大小没有改变,例如,帽子的图片根据其大小呈现,并且挂在顶部。但我希望内容适应卡片,并且图像大小在整个应用程序中保持不变。

我还需要一个建议,如果我使用来自Web的图片,所以为什么会出现这种情况,或者我应该如何获取图片(例如从本地)?请为我提供指导。

谢谢!

英文:

I am making a eCommerce site where I have to put images of products. So I am using simple CSS Grid system in React but while rendering images they are coming in different sizes , as I have taken images from Web , so they are not adjusting in proper way

Please check my code

return (
    &lt;&gt;
      &lt;div style={{ display: &quot;grid&quot;, gridTemplateColumns: &quot;repeat(4 ,1fr)&quot;, gridGap: &quot;10px&quot;,
      alignContent:&quot;center&quot;,padding:&quot;20px&quot;,margin:&quot;20px&quot; }}&gt;
        {/* &lt;h1 style={{ color: &quot;green&quot; }}&gt;This is a Clothing Page.&lt;/h1&gt; */}

        {filteredClothing[0].products != null &amp;&amp;
          filteredClothing[0].products !== undefined &amp;&amp;
          filteredClothing[0].products.map((item) =&gt; (
            &lt;&gt;
              &lt;div style={{}}&gt;
                &lt;Card
                  className=&quot;card_image&quot;
                  key={item.id}
                  style={{ width: &quot;70%&quot;, height: &quot;100%&quot;, padding: &quot;10px&quot;,margin:&quot;10px&quot; }}
                &gt;
                  &lt;div style={{display:&quot;flex&quot;,flexDirection:&quot;column&quot;,flexWrap:&quot;wrap&quot;}}&gt;
                    &lt;div&gt;
                      &lt;CardMedia
                        component=&quot;img&quot;
                        image={item.image}
                        alt={item.name}
                      &gt;&lt;/CardMedia&gt;
                    &lt;/div&gt;
                    &lt;div&gt;
                      &lt;p&gt;Cost: {item.price}&lt;/p&gt;
                      &lt;p&gt; {item.description}&lt;/p&gt;
                    &lt;/div&gt;

                  &lt;/div&gt;


                &lt;/Card&gt;
              &lt;/div&gt;
            &lt;/&gt;

and inUI it is looking like this
Products Image in UI

So I have tried to position elements in DOM but size of image is not changing , for example in Hat image it is rendered according to its size and it is hanging on top
But I am expecting to fit content in the Card and Image size should be constant throughout the application

and I need one more suggestion, if I am using images from Web so thats why it is happening like this or how I have to take images (like from local) ? Please guide me for this also

Thanks

答案1

得分: 0

This issue got solved by writing CSS property object-fit - "contain", please check below code:

  <CardMedia
    component="img"
    image={item.image}
    alt={item.name}
    style={{
      width: '300px',
      height: '400px',
      objectFit: 'contain',
      cursor: 'pointer',
    }}
    onClick={(event) => showProducts(event, item.id)}
  ></CardMedia>
英文:

This issue got solved by writing CSS property object-fit - "contain" , please check below code:

  &lt;CardMedia
    component=&quot;img&quot;
    image={item.image}
    alt={item.name}
    style={{
      width: &#39;300px&#39;,
      height: &#39;400px&#39;,
      objectFit: &#39;contain&#39;,
      cursor: &#39;pointer&#39;,
    }}
    onClick={(event) =&gt; showProducts(event, item.id)}
  &gt;&lt;/CardMedia&gt;

huangapple
  • 本文由 发表于 2023年6月29日 18:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定