css - 如何设置嵌套元素的样式以与样式组件 react ?

标签 css reactjs

我正在使用样式化组件,并且想通过嵌套 css 在 div 内设置子元素的样式。查看我的演示 here

const styles = theme => ({
  root: {
    ...theme.typography.button,
    backgroundColor: theme.palette.common.white,
    padding: theme.spacing.unit,
    ">span": {
      background: "red"
    }
  }
});

function TypographyTheme(props) {
  return (
    <div className={props.classes.root}>
      <h1>
        <span>{"This div's text looks like that of a button."}</span>
      </h1>
    </div>
  );
}

最佳答案

它看起来像 JSS 语法,而不是样式组件。

更改此:

const styles = theme => ({
  root: {
    ...theme.typography.button,
    backgroundColor: theme.palette.common.white,
    padding: theme.spacing.unit,
    ">span": {
      background: "red"
    }
  }
});

这样:

const styles = theme => ({
  root: {
    ...theme.typography.button,
    backgroundColor: theme.palette.common.white,
    padding: theme.spacing.unit,
    "& span": {
      background: "red"
    }
  }
});

或者如果您不改变所有嵌套跨度

const styles = theme => ({
  root: {
    ...theme.typography.button,
    backgroundColor: theme.palette.common.white,
    padding: theme.spacing.unit,
    "& > h1 > span": {
      background: "red"
    }
  }
});

试试这个codesandbox https://codesandbox.io/s/vm3qnmj75l

供引用: http://cssinjs.org/jss-nested/

关于css - 如何设置嵌套元素的样式以与样式组件 react ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022653/

相关文章:

javascript - jqGrid Pager 图标不显示

javascript - 网页代码的执行顺序是什么?我们如何更改顺序?

php - 图像没有占用 100% 的 div?

javascript - 路由器的 prop History.location 中的路径名不会更新

reactjs - 如何将使用钩子(Hook)的函数包装在 useEffect 中?

html - css 转换不适用于显示 : none

html - 仅通过水平滚动定位嵌套的 Div

html - react : How to make <td> collapse as column in a <tr> in mobile view

reactjs - React Query 似乎没有缓存

javascript - Prop `loadingElement` 在 `withScriptjs(withGoogleMap(Component)) 中被标记为必需