zl程序教程

您现在的位置是:首页 >  Javascript

当前栏目

CSS中in JS是什么意思

2023-03-15 23:13:15 时间

CSS中in

说明

1、CSS中in JS,意思就是使用js语言写css,完全不需要些单独的css文件,所有的css代码全部放在组件内部,以实现css的模块化。

2、CSS in JS其实是一种编写思想。

目前已经有超过40多种方案的实现,最出名的是 styled-components。

实例

import React from "react";
import styled from "styled-components";
 
// 创建一个带样式的 h1 标签
const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;
 
// 创建一个带样式的 section 标签
const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;
 
// 通过属性动态定义样式
const Button = styled.button`
  background: ${props => (props.primary ? "palevioletred" : "white")};
  color: ${props => (props.primary ? "white" : "palevioletred")};
 
  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;
 
// 样式复用
const TomatoButton = styled(Button)`
  color: tomato;
  border-color: tomato;
`;
 
<Wrapper>
  <Title>Hello World, this is my first styled component!</Title>
  <Button primary>Primary</Button>
</Wrapper>;

以上就是CSS中in JS的意思,希望对大家有所帮助。更多css学习指路:css教程

本文教程操作环境:windows7系统、css3版,DELL G3电脑。