如何在React中基于父标签添加子标签的CSS?

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

How to add css of Child tag based on parant tag in React?

问题

import React from 'react';
const style = {
    h2: {
        fontSize: '20px',
        color: 'black',
    },
    span: {
        color: 'white',
    }
}
const Main = () => {
    return (
        <h2 style={style}>你好<span>测试</span></h2>
   );
}
export default Main;
英文:
import React from &#39;react&#39;;
const style = {
    h2 : {
        fontSize: &#39;20px&#39;;
        color: &#39;black&#39;;
    } &amp; span: {
       color: &#39;white&#39;;
    }
}
const Main = () =&gt; {
    return (
        &lt;h2 style={style}&gt;Hello&lt;span&gt;Test&lt;/span&gt;&lt;/h2&gt; 
   );
}
export default Main;

Note: <span> can be dynamic.
H2 is the parent tag and I applied style in the same tag I want that it will also be applicable to the child tag (<span>)too.

答案1

得分: 0

JSX样式属性与HTML的样式属性类似。样式属性和属性都只接受CSS属性。因此,不允许使用选择器、伪元素或伪类。

使用样式属性

替换以下内容:

const style = {
    h2 : {
        fontSize: '20px';
        color: 'black';
    } & span: {
       color: 'white';
    }
}

const Main = () => {
    return (
        <h2 style={style}>Hello<span>Test</span></h2> 
   );
}

使用以下内容替换:

const h2Style = {
  fontSize: '20px';
  color: 'black';
}
const spanStyle = {
  color: 'white';
}

const Main = () => {
    return (
        <h2 style={h2Style}>Hello<span style={spanStyle}>Test</span></h2> 
   );
}

或更易读的方式:

const styles = {
  h2: {
    fontSize: '20px';
    color: 'black';
  },
  span: {
    color: 'white';
  }
}

const Main = () => {
    return (
        <h2 style={styles.h2}>Hello<span style={styles.span}>Test</span></h2> 
   );
}

但是,如果您只希望为h2元素定义样式,我们有更多选项:

CSS / SCSS

使用CSS,放在单独的文件中:

import "./your-css.css";

const Main = () => {
    return (
        <h2 className="title">Hello<span>Test</span></h2> 
   );
}

其中,.your-css.css 文件包含以下内容:

.title {
  fontSize: '20px';
  color: 'black';
}

.title span {
  color: 'white';
}

或者,嵌套(如果使用预处理器如.scss):

.title {
  fontSize: '20px';
  color: 'black';

  span {
    color: 'white';
  }
}

CSS-in-JS

考虑更“React”风格的解决方案,我们可以使用CSS-in-JS,例如 styled-components 阅读更多

import React from 'react';
import styled from 'styled-components';

const Title = styled.h2`
  fontSize: '20px';
  color: 'black';

  span {
    color: 'white';
  }
`;

const Main = () => {
    return (
        <Title>Hello<span>Test</span></Title> 
   );
}

export default Main;
英文:

The JSX style property parallels HTML's style attribute. Both style property and attribute only accepts CSS properties. Hence, no selectors, pseudo-elements or pseudo-classes are allowed.

Using the style property

Replace the following:

const style = {
    h2 : {
        fontSize: &#39;20px&#39;;
        color: &#39;black&#39;;
    } &amp; span: {
       color: &#39;white&#39;;
    }
}

const Main = () =&gt; {
    return (
        &lt;h2 style={style}&gt;Hello&lt;span&gt;Test&lt;/span&gt;&lt;/h2&gt; 
   );
}

With:

const h2Style = {
  fontSize: &#39;20px&#39;;
  color: &#39;black&#39;;
}
const spanStyle = {
  color: &#39;white&#39;;
}

const Main = () =&gt; {
    return (
        &lt;h2 style={h2Style}&gt;Hello&lt;span style={spanStyle}&gt;Test&lt;/span&gt;&lt;/h2&gt; 
   );
}

Or the more legible:

const styles = {
  h2: {
    fontSize: &#39;20px&#39;;
    color: &#39;black&#39;;
  },
  span: {
    color: &#39;white&#39;;
  }
}

const Main = () =&gt; {
    return (
        &lt;h2 style={styles.h2}&gt;Hello&lt;span style={styles.span}&gt;Test&lt;/span&gt;&lt;/h2&gt; 
   );
}

However, since you wish to only define the style for the h2 element, we have more options:

CSS / SCSS

Using CSS, on a separate file:

import &quot;./your-css.css&quot;;

const Main = () =&gt; {
    return (
        &lt;h2 className=&quot;title&quot;&gt;Hello&lt;span&gt;Test&lt;/span&gt;&lt;/h2&gt; 
   );
}

where, .your-css.css file contains

.title {
  fontSize: &#39;20px&#39;;
  color: &#39;black&#39;;
}

.title span {
  color: &#39;white&#39;;
}

Or even, nested (if you use a preprocessor like .scss)

.title {
  fontSize: &#39;20px&#39;;
  color: &#39;black&#39;;

  span {
    color: &#39;white&#39;;
  }
}

CSS-in-JS

Thinking of a more "React" solution, we could use CSS-in-JS, like styled-components Read More

import React from &#39;react&#39;;
import styled from &#39;styled-components&#39;;

const Title = styled.h2`
  fontSize: &#39;20px&#39;;
  color: &#39;black&#39;;

  span {
    color: &#39;white&#39;;
  }
`;

const Main = () =&gt; {
    return (
        &lt;Title&gt;Hello&lt;span&gt;Test&lt;/span&gt;&lt;/Title&gt; 
   );
}

export default Main;

huangapple
  • 本文由 发表于 2023年6月6日 13:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411674.html
匿名

发表评论

匿名网友

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

确定