css - 我想在 Emotion/styled 组件中重用某些 CSS 属性

标签 css reactjs properties styled-components emotion

import styled from '@emotion/styled';

type ColorProps = {
    Coloured: boolean,
}

const BoxStyled = styled.div`
    ${(props:ColorProps) => 
    props.Coloured ? {
    
    background: "#304f8f",
    border: "1.85px solid #304f8f",
    color: "white",
    width: "4rem",
    height: "2.5rem",
    padding:"0 11px" 
} :
{
    
    border: "1.98px solid #2c8090",
    width: "4rem",
    height: "2.5rem",
    padding:"0 11px" 
}
}`

在 BoxStyled 中,我不想写两次 width: "4rem"、height: "2.5rem"、padding:"0 11px"我该如何实现这一点?

最佳答案

使用css制作样式变量。

import { css } from 'styled-components'

const reuse = css`
  width: 4rem;
  height: 2.5rem;
  padding: 0 11px;
`

const StyleA = styled.div`
  background-color: black;
  ${reuse}
`

const StyleB = styled.div`
  background-color: red;
  ${reuse}
`

检查this了解更多信息。

关于css - 我想在 Emotion/styled 组件中重用某些 CSS 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70648183/

相关文章:

Java 未将 "\u"写入属性文件

python - 在 __init__ 中使用 python 属性?

css - 无法设置 Wordpress wp_list_categories 的样式

javascript - 通过替换 Divs(笨拙的)向下钻取导航菜单

reactjs - 原型(prototype)污染——npm漏洞无法修复?

javascript - React 中如何制作有焦点或有内容时可以向上移动的 float 标签?

javascript - 使用垂直滚动条水平滚动内容

javascript - 如何为 extjs hbox 布局设置自动高度?

javascript - "You require appropriate loaders",使用 react native 和 expo。应用程序无法在 web 上编译,但在 android 上运行

分配相同的值时不调用 c# 属性 setter