英文:
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 'react';
const style = {
h2 : {
fontSize: '20px';
color: 'black';
} & span: {
color: 'white';
}
}
const Main = () => {
return (
<h2 style={style}>Hello<span>Test</span></h2>
);
}
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: '20px';
color: 'black';
} & span: {
color: 'white';
}
}
const Main = () => {
return (
<h2 style={style}>Hello<span>Test</span></h2>
);
}
With:
const h2Style = {
fontSize: '20px';
color: 'black';
}
const spanStyle = {
color: 'white';
}
const Main = () => {
return (
<h2 style={h2Style}>Hello<span style={spanStyle}>Test</span></h2>
);
}
Or the more legible:
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>
);
}
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 "./your-css.css";
const Main = () => {
return (
<h2 className="title">Hello<span>Test</span></h2>
);
}
where, .your-css.css
file contains
.title {
fontSize: '20px';
color: 'black';
}
.title span {
color: 'white';
}
Or even, nested (if you use a preprocessor like .scss)
.title {
fontSize: '20px';
color: 'black';
span {
color: 'white';
}
}
CSS-in-JS
Thinking of a more "React" solution, we could use CSS-in-JS, like styled-components
Read More
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论