material-ui makeStyles:如何通过标签名称设置样式? [英] material-ui makeStyles: how to style by tag name?

查看:129
本文介绍了material-ui makeStyles:如何通过标签名称设置样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为当前组件中的所有< p> 添加规则.我在任何地方都找不到有关如何通过标记名添加CSS规则的信息(material-ui文档,Stack Overflow等).

I want to add a rule for all <p> in the current component. I couldn't find information anywhere (material-ui documentation, Stack Overflow, etc.) on how to add CSS rules by tag name.

不支持吗?

我正在尝试做这样的事情:

I'm trying to do something like this:

const useStyles = makeStyles((theme: Theme) =>
    createStyles({
        'p': {
            margin: 0,
        },
        someClass: {
            fontSize: 14,
        },
    })
);

使用Ryan的解决方案有效,但会产生一个新问题:

Using Ryan's solution works, but creates a new problem:

import React from 'react';
import { makeStyles, Theme, createStyles } from '@material-ui/core';

const useStyles = makeStyles((theme: Theme) =>
    createStyles({
        root: {
            '& p': {
                margin: 0,
            },
        },
        // I want this rule to override the rule above. It's not possible in the current configuration, because `.root p` is more specific than `.title`
        // This problem originates from the fact that I had to nest the rule for `<p>` inside a CSS class
        title: {
            margin: '0 0 16px',
        },
    })
);

export default () => {
    const classes = useStyles({});

    return (
        <div className={classes.root}>
            <p className={classes.title}>My title</p>
            <p>A paragraph</p>
            <p>Another paragraph</p>
        </div>
    );
};

推荐答案

由于您希望将此范围限定于组件,因此需要一个类来应用于您的组件(例如,示例中的 classes.root 以下).然后,您可以使用&定位其中的所有 p 元素.p .如果您随后需要为组件中的另一个CSS类覆盖 p 标记样式,则可以使用另一个嵌套规则来定位也具有该类的 p 标记(例如<示例中的code> classes.title ).

Since you want this scoped to your component, you need a class to apply to your component (e.g. classes.root in the example below). Then you can target all p elements within that using & p. If you then need to override the p tag styling for another CSS class within your component, you can use another nested rule to target p tags that also have that class (e.g. classes.title in the example).

import React from "react";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  root: {
    "& p": {
      margin: 0,
      "&$title": {
        margin: "0 0 16px"
      }
    }
  },
  title: {}
}));
export default function App() {
  const classes = useStyles();
  return (
    <div className="App">
      <p>This paragraph isn't affected.</p>
      <p>This paragraph isn't affected.</p>
      <div className={classes.root}>
        <p className={classes.title}>My title</p>
        <p>A paragraph</p>
        <p>Another paragraph</p>
      </div>
    </div>
  );
}

相关文档: https://cssinjs.org/jss-plugin-nested?v=v10.1.1#use--to-reference-selector-the-parent-rule

这篇关于material-ui makeStyles:如何通过标签名称设置样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆