欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > css >内容正文

css

style 放入css文件失效-凯发k8官方网

发布时间:2024/10/8 css 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 style 放入css文件失效_react中使用css的7种方式 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

来源 | http://www.fly63.com/article/detial/1961

1、在组件中直接使用style

不需要组件从外部约会css文件,直接在组件中书写。import react, { component } from "react";const div1 = { width: "300px", margin: "30px auto", backgroundcolor: "#44014c", //驼峰法 minheight: "200px", boxsizing: "border-box"};class test extends component { constructor(props, context) { super(props); } render() { return ( 123 ); }}export default test;注意事项:在正常的css中,设置background-color,box-sizing等属性,在style对象div 1中的属性中,必须转换成驼峰法,backgroundcolor,boxsizing。而没有连字符的属性,如margin,width等,则在style对象中不变。在正常的css中,css的值不需要用双引好(“”),如.app-header { background-color: #282c34; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(10px 2vmin); color: white;}而在react中使用style对象的方式时。值必须用双引号包裹起来。这种方式的反应样式,只作用于当前组件。

2、在组件中约会[name].css文件

需要在当前组件开头使用import♡ css文件。import react, { component } from "react";import testchidren from "./testchidren";import "@/assets/css/index.scss";class test extends component { constructor(props, context) { super(props); } render() { return ( 123 测试子组件的样式 ); }}export default test;

这种方式约会的css样式,会作用于当前组件及其所有后代组件

3、在组件中约会[name] .s css文件

引入反应内部已经支持了后缀为scss的文件,所以只需要安装节点萨斯即可,因为有个节点萨斯scss文件才能在节点环境上编译成的css文件。

>yarn add node-sass然后编写scss文件//index.scss.app{ background-color: #282c34; .header{ min-height: 100vh; color: white; }}

关于如何详细的使用sass,请查看sass凯发k8官方网官网:

这种方式约会的css样式,同样会作用于当前组件及其所有后代组件

4、在组件中约会[name] .module.css文件

将css文件作为一个模块约会,这个模块中的所有css,只作用于电流组件。不会影响电流组件的后代组件。

import react, { component } from "react";import testchild from "./testchild";import modulecss from "./test.module.css";class test extends component { constructor(props, context) { super(props); } render() { return ( 321321 ); }}export default test;

这种方式可以看做是前面第一种在组件中使用style的升级版。完全将css和组件分离开,又不会影响其他组件。

5、在组件中约会[name] .module.scss文件

某种第四种,区别是第四种约会css模块,而这种是约会scss模块而已。

import react, { component } from "react";import testchild from "./testchild";import modulecss from "./test.module.scss";class test extends component { constructor(props, context) { super(props); } render() { return ( 321321 ); }}export default test;同样这种方式可以看做是前面第一种在组件中使用style的升级版。

6、使用styled-components

需要先安装>yarn add styled-components然后创建一个js文件(注意是js文件,不是css文件)//style.jsimport styled, { createglobalstyle } from "styled-components";export const selflink = styled.div` height: 50px; border: 1px solid red; color: yellow;`;export const selfbutton = styled.div` height: 150px; width: 150px; color: ${props => props.color}; background-image: ; background-size: 150px 150px;`;

组件中使用styled-components样式

import react, { component } from "react";import { selflink, selfbutton } from "./style";class test extends component { constructor(props, context) { super(props); } render() { return ( app.js selfbutton ); }}export default test;

这种方式是将整个的css样式,和html节点整体合并成一个组件。引入这个组件的html和css都有了。它的好处在于可以随时通过往组件上传入属性,来动态的改变样式。对于处理变量,媒体查询,伪类等较方便的。

这种方式的css也只对当前组件有效。

具体用法,请查看styled-components凯发k8官方网官网:https://styled-components.com/

7、使用radium

需要先安装

>yarn add radium然后在react组件中直接约会使用import react, { component } from "react";import radium from 'radium';let styles = { base: { color: '#fff', ':hover': { background: '#0074d9' } }, primary: { background: '#0074d9' }, warning: { background: '#ff4136' }};class test extends component { constructor(props, context) { super(props); } render() { return ( this is a primary button ); }}export default radium(test);对于处理变量,媒体查询,伪类等是不方便的。使用radium可以直接处理变量,媒体查询,伪类等,并且可以直接使用js中的数学,连接,正则表达式,条件,函数等。具体用法请查看radium凯发k8官方网官网:https://formidable.com/open-source/radium/注意:在export之前,必须用radium包裹。

总结

以上是凯发k8官方网为你收集整理的style 放入css文件失效_react中使用css的7种方式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图