【问题标题】:how to style svg in reactjs with styled components如何使用样式化组件在 reactjs 中设置 svg 样式
【发布时间】:2021-07-04 05:13:48
【问题描述】:
import { FaRegHeart } from 'react-icons/fa';


const IconHeart = styled(FaRegHeart)`

width:21px;
height:18px;
position: absolute;
left: 28%;
top:30%;
cursor: pointer;
&:hover{
 background-color:red;
}
`


function Heart() {
  return (
    <HeartBox>

      <IconHeart />

    </HeartBox>

  )
}

猜测:

我完全控制了 svg 图标,悬停在此图标上不起作用。喜欢在图标周围有边框,但是如果我设置边框,他会在图标周围放置边框,而不是在其本身上

【问题讨论】:

    标签: reactjs svg styled-components react-icons


    【解决方案1】:

    你需要使用 SVG 的填充属性

    const IconHeart = styled(FaRegHeart)`
      width:21px;
      height:18px;
      position: absolute;
      left: 28%;
      top:30%;
      cursor: pointer;
    
      :hover{
        fill:red;
        }
    `
    

    代码沙盒示例:
    https://codesandbox.io/s/styled-hover-fill-svg-iks37

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2021-10-21
      • 2021-04-21
      • 1970-01-01
      • 2018-11-20
      • 2018-06-03
      • 2016-07-30
      • 2014-01-02
      • 2022-01-01
      相关资源
      最近更新 更多