【问题标题】:How do I place an image in an area/shape by CSS using ReactJS?如何使用 ReactJS 通过 CSS 将图像放置在区域/形状中?
【发布时间】:2022-01-24 11:43:05
【问题描述】:

因此,我正在尝试为《守望先锋》构建一个团队选择器,并且我有一个网站,其中卡片横向排列。它们是空白的灰色卡片。它们使用以下 JSX 代码放置:

import React from 'react';
import './heroes.css';

const heroes = () => {
  return (
      <div>
          <div className='hero-container'> {
            displayHeroes()
          }
          </div>
      </div>
  )
};

const displayHeroes = () => {
  const row = [];
  const heroes = ['Dva','Orisa','Rein','Hog','Sigma',
                  'Winston','Ball','Zar','Ashe,','Bastion',
                  'Cassidy','Doom','Echo','Genji',
                  'Hanzo','Junkrat','Mei','Pharah',
                  'Reaper','Soldier','Sombra','Sym',
                  'Torb','Tracer','Widow','Ana',
                  'Bap','Brig','Lucio','Mercy',
                  'Moira','Zen'];
  for (let i =0; i<32; i++){
      row.push(<div className='hero scale-up-ver-bottom '>
        <img src="" alt={heroes[i]} />
      </div>)
  }
  return row;
};

heroes() 只是给出一个 div 来放入所有卡片, displayHeroes() 循环遍历英雄数组并将 alt 文本分配给每张卡片,并返回要渲染的 div 数组。 当我将文件路径放入src="" 时,什么都没有发生。

.hero {
    background-color: gray;
    width: 30px;
    height: 50px;
    margin-top: 10px;
    margin-right: 2.5px;
    margin-left: 2.5px;
    padding: 20px;
    border-radius: 10px;
    flex-shrink: 0;
    box-shadow: 0px 5px 10px gray;
    outline-style: solid;
    outline-width: 2.5px;
    outline-color: black;
}

.hero img {
    width: 100%;
    height: 100%;
}

如何将图像放入由 css 属性定义的形状内?我想不通。

【问题讨论】:

    标签: javascript html css reactjs


    【解决方案1】:

    我会将图像设置为背景并使用

    background-size: contain
    

    在此处了解有关background-size 的更多信息:https://www.w3schools.com/cssref/css3_pr_background-size.asp

    【讨论】:

    • 问题是每张卡片都有不同的图像,所以我不能将css文件中的样式更改为一个图像,尽管我想我可以找出一些内联css来做到这一点.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 2021-12-06
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多