如何在unocss中自定义边距数值?

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

How do I customize margin values in unocss?

问题

如何自定义边距值

mrmtmlmb 的默认边距是 1em,如何将这些边距设置为使用 18px 或 20px?我还想类似地处理内边距。

my: margin : 20px 0
mx: margin : 0 20px
ml : margin-left: 20px
mt: margin-top: 20px
英文:

How do I customize the margin value

The default margins for mr mt ml mb is 1em,How can I set these margins to use 18px or 20px? I'd also like to do something similar with padding.

my: margin : 20px 0
mx: margin : 0 20px
ml : margin-left: 20px
mt: margin-top: 20px

答案1

得分: 1

你可以通过自定义主题来实现这一点。

你可以通过编辑 tailwind.config.js 文件中的 theme.spacingtheme.extend.spacing 来自定义你的间距比例。

module.exports = {
  theme: {
    extend: {
      spacing: {
        '20px': '20px',
      }
    }
  }
}

另外,你也可以通过编辑 tailwind.config.js 文件中的 theme.margintheme.extend.margin 来仅自定义边距比例。

module.exports = {
  theme: {
    extend: {
      margin: {
        '18px': '18px',
      }
    }
  }
}

此外,如果你不想修改主题,你可以使用任意值来设置实用类,例如 m-[20px]my-[18px]

资源链接:https://tailwindcss.com/docs/margin#customizing-your-theme

英文:

You can do that by customizing your theme.

You can customize your spacing scale by editing theme.spacing or theme.extend.spacing in your tailwind.config.js file.

module.exports = {
  theme: {
    extend: {
      spacing: {
        '20px': '20px',
      }
    }
  }
}

Alternatively, you can customize just the margin scale by editing theme.margin or theme.extend.margin in your tailwind.config.js file.

module.exports = {
  theme: {
    extend: {
      margin: {
        '18px': '18px',
      }
    }
  }
}

Also, if you don't want to modify your theme, you can use arbitrary values for the utility classes, such as m-[20px] or my-[18px].

Resource: https://tailwindcss.com/docs/margin#customizing-your-theme

答案2

得分: 0

你可以使用以下方式:

m-10px  m10px -> margin: 10px
my-20px  my20px -> margin-top: 20px; margin-bottom: 20px
mr-20px  mr20px -> margin-right: 20px 0

如果你不想在你的样式中使用 px,或者想将其他未提及的边设置为 0,可以使用自定义规则:

marginSideMap = {
  l: `0 0 0 ${value}px`,
  r: `0 ${value}px 0 0`,
  t: `${value}px 0 0`,
  b: `0 0 ${value}px`,
  y: `0 ${value}px`,
  x: `${value}px 0`,
  default: `${value}px`
}  
const formatMargin = (_, side, value) => ({
  margin: `${marginSideMap[side || 'default']}`
})

export default defineConfig({
  // ...
  rules: [
    [/^m([trblxy])?-?(\d+)$/, formatMargin],
  ],
})

它将匹配任何以下值:m-20, mx20, mr-20 ... 并应用此格式。

英文:

you may use next:

m-10px or m10px     -> margin: 10px
my-20px or my20px   -> margin-top: 20px; margin-bottom: 20px
mr-20px or mr20px   -> margin-right: 20px 0

if you don't want to use px in your styles
OR if you want to set other sides (that are not mentioned) as 0
as: mr-20px -> margin: 0 20px 0 0

then you may use custom rules:

marginSideMap = {
  l: `0 0 0 ${value}px`,
  r: `0 ${value}px 0 0`,
  t: `${value}px 0 0`,
  b: `0 0 ${value}px`,
  y: `0 ${value}px`,
  x: `${value}px 0`,
  default: `${value}px`
}  
const formatMargin = (_, side, value) => ({
  margin: `${marginSideMap[side || 'default']`,
})

export default defineConfig({
  // ...
  rules: [
    [/^m([trblxy])?-?(\d+)$/, formatMargin],
  ],
})

it will grab any of the next value: m-20, mx20, mr-20 ... and apply this formatter.

huangapple
  • 本文由 发表于 2023年2月8日 22:42:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387409.html
匿名

发表评论

匿名网友

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

确定