CSS-IN-JS

article/2025/10/19 8:13:39

集成css代码在js中

一、为什么会有 CSS-IN-JS

CSS-IN-JS 是 WEB 项⽬中将 CSS 代码捆绑在 JavaScript 代码中的解决⽅案.这种⽅案旨在解决 CSS 的局限性, 例如缺乏动态功能, 作⽤域和可移植性.

二、CSS-IN-JS 介绍

1、CSS-IN-JS ⽅案的优点:

  1. 让 CSS 代码拥有独⽴的作⽤域, 阻⽌ CSS 代码泄露到组件外部, 防⽌样式冲突.
  2. 让组件更具可移植性, 实现开箱即⽤, 轻松创建松耦合的应⽤程序
  3. 让组件更具可重⽤性, 只需编写⼀次即可, 可以在任何地⽅运⾏. 不仅可以在同⼀应⽤程序中重⽤组件, ⽽且可以在使⽤相同框架构建的其他应⽤程序中重⽤组件.
  4. 让样式具有动态功能, 可以将复杂的逻辑应⽤于样式规则, 如果要创建需要动态功能的复杂UI, 它是理想的解决⽅案.

2、CSS-IN-JS ⽅案的缺点:

  1. 为项⽬增加了额外的复杂性.
  2. ⾃动⽣成的选择器⼤⼤降低了代码的可读性

三、 Emotion 库

Emotion 是⼀个旨在使⽤ JavaScript 编写 CSS 样式的库.

1、css 属性⽀持

1. JSX Pragma

npm install @emotion/core @emotion/styled

通知 babel, 不再需要将 jsx 语法转换为 React.createElement ⽅法, ⽽是需要转换为 jsx ⽅法.

在这里插入图片描述

import React from 'react';
/** @jsx jsx */
import {jsx} from '@emotion/core'
function App() {return <div css={{width: 200, height: 200,background: red}}></div>
}

在这里插入图片描述

2. Babel Preset

  1. npm run eject 弹射出底层配置
    在这里插入图片描述
    添加git之后才能弹射

  2. 在 package.json ⽂件中找到 babel 属性, 加⼊如下内容

在这里插入图片描述
接下来就可以去掉注释了

import React from 'react';
function App() {return <div css={{width: 200, height: 200,background: red}}></div>
}

四、css的使用方法

1. String Styles

在这里插入图片描述

2. Object Styles

在这里插入图片描述

五、css 属性优先级

props 对象中的 css 属性优先级⾼于组件内部的 css 属性. 在调⽤组件时可以在覆盖组件默认样式.

css.js

在这里插入图片描述
app.js

在这里插入图片描述

六、Styled Components 样式化组件

样式化组件就是⽤来构建⽤户界⾯的,是 emotion 库提供的另⼀种为元素添加样式的⽅式。

在这里插入图片描述

import react from 'react';
import styled from '@emotion/styled'const Button = styled.button`width: 100px; height: 30px; background: blue; 
`
const Container = styled.div({width: 1000,background: pink,margin: '0 auto',
})function App() {return <div><Container><Button>我是按钮</Button></Container></div>
}export default App;

在这里插入图片描述

七、根据 props 属性覆盖样式

方式一

在这里插入图片描述


import react from 'react';
import styled from '@emotion/styled'const Button = styled.button`width: 100px; height: 30px; background: ${props => props.bgColor || 'yellow'}; 
`
const Container = styled.div({width: props.w || 1000,background: pink,margin: '0 auto',
})function App() {return <div><Container w={1600}><Button bgColor="blue">我是按钮</Button></Container></div>
}export default App;

方式二

根据 props 属性覆盖样式

在这里插入图片描述

import react from 'react';
import styled from '@emotion/styled'const Button = styled.button`width: 100px; height: 30px; background: ${props => props.bgColor || 'yellow'}; 
`
const Container = styled.div({width: 1000,background: 'pink',margin: '0 auto',
}, props =>({width: props.width, background: props.bgColor
}));function App() {return <div><Container w={1600} bgColor="red"><Button>我是按钮</Button></Container></div>
}export default App;

八、为任何组件添加样式

Styled Components 样式化组件

在这里插入图片描述

方式一、字符串

import React from 'react'
import styled from '@emotion/styled'function Demo({className}) {return <div className={className}>Demo</div>
}const Fancy = styled(Demo)`color: red;
`function App() {return <div><Fancy /></div>
}export default App;

在这里插入图片描述

方式二、对象


import React from 'react'
import styled from '@emotion/styled'function Demo({className}) {return <div className={className}>Demo</div>
}const Fancy = styled(Demo)`color: red;
`
const Fancy2 = styled(Demo)({background: 'red',color: 'white'
})function App() {return <div><Fancy /><Fancy2 /></div>
}export default App;

在这里插入图片描述

十、为特定父级下的子组件添加样式

通过⽗组件设置⼦组件样式
在这里插入图片描述

方式一、字符串

import React from 'react'
import styled from '@emotion/styled'const Child = styled.div`color: red;
`
const Parent = styled.div`${Child} {color: green;}
`function App() {return <div><Child>child</Child><Parent><Child>child parent</Child></Parent></div>
}export default App;

在这里插入图片描述

方式二、对象

import React from 'react'
import styled from '@emotion/styled'const Child = styled.div({color: 'red'
})
const Parent = styled.div({[Child]: {color: 'yellow'}
})function App() {return <div><Child>child</Child><Parent><Child>child parent</Child></Parent></div>
}export default App;

在这里插入图片描述

十一、css选择器&

在这里插入图片描述

import React from 'react'
import styled from '@emotion/styled'const Container = styled.div`width: 200px;height: 200px;color: pink;background: skyblue;&:hover {background: pink;}& > span {color: yellow;}
`function App() {return <div><Container>container<span>span</span></Container></div>
}export default App;

十二、样式化组件as属性

要使⽤组件中的样式, 但要更改呈现的元素, 可以使⽤ as 属性

在这里插入图片描述

import React from 'react'
import styled from '@emotion/styled'const Button = styled.button`color: red;
`function App() {return <div><Button as="a" href="#">button</Button></div>
}export default App;

在这里插入图片描述

十三、样式组合

在这里插入图片描述

十四、Global组件


import React from 'react'
import styled from '@emotion/styled'
import { css, Global } from '@emotion/core'const styles = css`body {margin: 0;}a {text-decoration: none;color: red;}
`
function App() {return <div><Global styles={styles} /><a href ="#">我是a</a><Demo /></div>
}export default App;

在这里插入图片描述

十五、keyframes关键帧动画

import React from 'react'
import styled from '@emotion/styled'
import { css, keyframes } from '@emotion/core'const move = keyframes`0% {background: skyblue;left: 0;top: 0;}100% {background: tomato;left: 600px;top: 300px;}
`
const box = css`width: 100px;height: 100px;position: absolute;animation: ${move} 2s ease infinite alternate;
`function App() {return <div css={box}>app</div>
}export default App;

十六、创建主题

npm install emotion-theming

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { ThemeProvider } from 'emotion-theming';const theme = {colors: {primary: 'tomato'}
};ReactDOM.render(<ThemeProvider theme={theme}><App /></ThemeProvider>,document.getElementById('root')
);

App.js

import React from 'react';
import { css } from '@emotion/core';
import { useTheme } from 'emotion-theming';const primaryColor = props => css`color: ${props.colors.primary}
`function App() {console.log(useTheme());return <div css={primaryColor}>App works</div>;
}export default App;

在这里插入图片描述
在这里插入图片描述


http://chatgpt.dhexx.cn/article/61HKuKaC.shtml

相关文章

vscode配置JSHint提示工具

vscode配置JSHint提示工具 本文介绍vscode配置JSHint提示工具,规范JavaScript的编码。 首先“Ctrl + P”输入“ext install jshint”,或者选择侧边栏“扩展”直接搜索“jshint”; 然后,下载jshint,如图: 安装第一个jshint,因为我已经安装了所以没有绿色“安装”按钮,…

JSHint介绍

为什么80%的码农都做不了架构师&#xff1f;>>> JSHint跟JSLint非常像&#xff0c;都是Javascript代码验证工具&#xff0c;这种工具可以检查你的代码并提供相关的代码改进意见。 对于你的代码&#xff0c;你可以选择多种方式来进行检验&#xff1a; 第一种方法&…

C++之ofstream::flush与ofstream::close

一.缓冲区知识 1.什么是缓冲区 缓冲区又称为缓存&#xff0c;它是内存空间的一部分。也就是说&#xff0c;在内存空间中预留了一定的存储空间&#xff0c;这些存储空间用来缓冲输入或输出的数据&#xff0c;这部分预留的空间就叫做缓冲区。 缓冲区根据其对应的是输入设备还是输…

超详细ofstream和ifstream详细用法

ofstream和ifstream详细用法 ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间(文章最末尾附上了MSDN中关于这两个函数的解释); 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O,stream这个类有两…

ifstream和ofstream的理解

ifstream和ofstream的理解 开发工具与关键技术&#xff1a;Visual Studio、C 作者&#xff1a;张国军 撰写时间&#xff1a;2019年06月04日各种计算机应用系统通常把一些相关信息组织起来保存在外存储器中&#xff0c;称为文件&#xff0c;并用一个名字&#xff08;称为文件名…

【Stream】

1、stream不存储数据&#xff0c;而是按照特定的规则对数据进行计算&#xff0c;一般会输出结果&#xff1b; 2、stream不会改变数据源&#xff0c;通常情况下会产生一个新的集合&#xff1b; 3、stream具有延迟执行特性&#xff0c;只有调用终端操作时&#xff0c;中间操作才会…

(转载)C++ ofstream和ifstream详细用法

原文出自【比特网】&#xff0c;转载请保留原文链接&#xff1a;http://soft.chinabyte.com/database/460/11433960.sh [导读] ofstream是从内存到硬盘&#xff0c;ifstream是从硬盘到内存&#xff0c;其实所谓的流缓冲就是内存空间 在C中&#xff0c;有一个stream这个类&…

Stream流:

创建stream流&#xff1a; //单列集合 集合.stream//数组 stream.of(数组)//双列集合 map.entrySet().stream(); stream流的调试&#xff1a; 常用API: - map:相当于对数据进行一个操作&#xff0c;可以自定义返回值等 stream.map() - distinct:可以去除流中的相同元素&…

c++输出文件流ofstream用法详解

目录 一. 输入流 ofstream 用法 Public member functions (1-6) 1&#xff0c; (constructor) 2&#xff0c; ofstream::open 3&#xff0c; ofstream::is_open 4&#xff0c; ofstream::close 5&#xff0c; ofstream::rdbuf 6&#xff0c;ofstream::operator Public member …

C++文件和流

C文件和流 到目前为止&#xff0c;我们已经使用了 iostream 标准库&#xff0c;它提供了 cin 和 cout 方法分别用于从标准输入读取流和向标准输出写入流。 本教程介绍如何从文件读取流和向文件写入流。这就需要用到 C 中另一个标准库 fstream&#xff0c;它定义了三个新的数据…

什么是https

HTTPS&#xff08;全称&#xff1a;Hypertext Transfer Protocol over Secure Socket Layer&#xff09;&#xff0c;是以安全为目标的HTTP通道&#xff0c;简单讲是HTTP的安全版。即HTTP下加入SSL层&#xff0c;HTTPS的安全基础是SSL&#xff0c;因此加密的详细内容请看SSL。 …

http和https有什么区别 端口号多少

HTTP和HTTPS的基本概念 HTTP&#xff1a;超文本传输协议&#xff0c;是在互联网上应用最广泛的一种网络协议。是一个客户端和服务端请求和应答的标准&#xff08;TCP&#xff09;&#xff0c;用于从WWW&#xff08;超文本&#xff09;服务器传输超文本到本地浏览器的传输协议。…

https 请求的端口是443 注意

注意: 这里录制https的请求 端口号一定是443 才可以抓取到!!!!!! &#xff08;进坑多次&#xff09; 转载于:https://www.cnblogs.com/kaibindirver/p/9223595.html

IP地址,开放端口,http与https的区别

文章目录 一、IP地址的概述二、IP地址分类1、**共有地址**2、 **私有地址** 三、IPV4和V6四、子网掩码、网关、DNS1、 子网掩码2、网关3、DNS服务器 五、获取目标IP地址的方法1、 通过ping命令&#xff1a;2、 通过NSLOOKUP命令&#xff1a;1.使用资源监视器查看&#xff1a;2.…

Linux网络——图解HTTPS协议与端口号认识

Linux网络——图解HTTPS协议与端口号认识 一、确保HTTP安全的方式1.1 HTTP明文加密<1> 通信加密<2> 内容加密 1.2 验证通信方身份1.3 验证报文完整性&#xff0c;防止被篡改 二、HTTP加密认证完整性保护HTTPS2.1 SSL/TLS2.2 对称加密2.3 非对称加密2.4 混合加密 三…

什么是SSL端口?HTTPS配置技术指南

安全套接字层&#xff08;SSL&#xff09;是负责互联网连接的数据身份验证和加密的技术。它加密在两个系统之间&#xff08;通常在服务器和客户端之间&#xff09;之间通过互联网发送的数据&#xff0c;使其保持私密。随着在线隐私的重要性日益增加&#xff0c;您应该熟悉SSL端…

--端口--

目录 一、端口的读写 二、shl和shr指令 我们在之前所讲过&#xff0c;各种存储器都和CPU的地址线、数据线、控制线相连。CPU在操控它们的时候&#xff0c;把它们当做内存来看待&#xff0c;把它们总地看做一个由若干存储单元组成的逻辑存储器&#xff0c;这个逻辑存储器我们称…

服务器中如何检查端口是否开放

服务器中如何检查端口是否开放 端口对于一台服务器来说是至关重要的&#xff0c;它是服务器与外部网络设备的协议出口&#xff0c;它一共拥有65536个(0-65535)&#xff0c;其中一些端口已经是约定好什么协议在使用了的&#xff0c;像80端口就是web服务使用、3389端口是Windows远…

部署https证书的端口是什么意思

端口号能够觉得是机器设备与外部通信沟通交流的出入口。http的端口号为80&#xff0c;那么&#xff0c;你知不知道https证书的端口是什么吗&#xff1f;今日小编就来详细介绍下。 https证书的端口是什么 https证书布署安装&#xff0c;网络服务器必须关联端口号&#xff0c;一…

一分钟了解HTTP和HTTPS协议

很多人存在这样的疑惑就是http与https的区别&#xff0c;这篇文章就跟大家介绍一下。一句话总结HTTPS是身披SSL外壳的HTTP&#xff0c;HTTPS更安全&#xff0c;实际使用中绝大多数的网站现在都采用的是HTTPS协议&#xff0c;这也是未来互联网发展的趋势。 什么是协议&#xff1…