不需要组件从外部引入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 (
<div style={div1}>123</div>
<div style="background-color:red;">
);
}
}
export default Test;
注意事项:
.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对象的方式时。值必须用双引号包裹起来。
这种方式的react样式,只作用于当前组件。
需要在当前组件开头使用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 (
<div>
<div className="link-name">123</div>
<TestChidren>测试子组件的样式</TestChidren>
</div>
);
}
}
export default Test;
这种方式引入的css样式,会作用于当前组件及其所有后代组件。
引入react内部已经支持了后缀为scss的文件,所以只需要安装node-sass即可,因为有个node-sass,scss文件才能在node环境上编译成css文件。
>yarn add node-sass
然后编写scss文件
//index.scss
.App{
background-color: #282c34;
.header{
min-height: 100vh;
color: white;
}
}
关于如何详细的使用sass,请查看sass官网
这种方式引入的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 (
<div>
<div className={moduleCss.linkName}>321321</div>
<TestChild></TestChild>
</div>
);
}
}
export default Test;
这种方式可以看做是前面第一种在组件中使用style的升级版。完全将css和组件分离开,又不会影响其他组件。
类似于第四种,区别是第四种引入css module,而这种是引入 scss module而已。
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 (
<div>
<div className={moduleCss.linkName}>321321</div>
<TestChild></TestChild>
</div>
);
}
}
export default Test;
同样这种方式可以看做是前面第一种在组件中使用style的升级版。
需要先安装
>yarn add styled-components
//style.js
import 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: url(${props => props.src});
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 (
<div>
<SelfLink title="People's Republic of China">app.js</SelfLink>
<SelfButton color="palevioletred" style={{ color: "pink" }} src={fist}>
SelfButton
</SelfButton>
</div>
);
}
}
export default Test;
这种方式是将整个css样式,和html节点整体合并成一个组件。引入这个组件html和css都有了。
它的好处在于可以随时通过往组件上传入 属性,来动态的改变样式。对于处理变量、媒体查询、伪类等较方便的。
这种方式的css也只对当前组件有效。
具体用法,请查看styled-components官网
需要先安装
>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 (
<div>
<button style={[ styles.base, styles.primary ]}>
this is a primary button
</button>
</div>
);
}
}
export default Radium(Test);
对于处理变量、媒体查询、伪类等是不方便的。
使用Radium可以直接处理变量、媒体查询、伪类等,并且可以直接使用js中的数学,连接,正则表达式,条件,函数等。
具体用法请查看radium官网
注意:
在export之前,必须用Radium包裹。
说到网页打印,首先想到的便是@media查询(即网页css),通过使用媒体类型print即可解决实际应用的大多数问题
作为前端,我们每天都在与CSS打交道,那么CSS的原理是什么呢? 浏览器渲染过程分为了两条主线:其一,HTML Parser 生成的 DOM 树;其二,CSS Parser 生成的 Style Rules ;
CSS技术技巧法则:不要让你的代码脱离你的掌控,尽量简洁、掌握基础、保持代码的可复用性、面向对象的css、Css3了解他能做的以及你可以使用的部分、渐进增强与优雅降级、Css预处理工具...
之前在自己的个人公众号中提到了一篇利用 CSS 的方式进行 XSS 攻击,当时有朋友跟我说,让我去获取那个网站的 cookie,再然后进入那个网站的后台去玩。然而,技术能力实在有限,搞不了这些东西
当position设置为:absolute或者fixed时,元素的display会转换为block。(设置float也会产生这样的效应)正常情况下,div会被内容撑开,但是如果设置了1.的情况下,父元素就会产生塌陷,失去高度。
简单的实现方式,大致有两种:一是用图片做背景,横向平铺线条图片;二是给每一块刻度区域平铺一个元素,然后用边线实现。身为一个“环保主义者”,这两种方式都不能让我满意。在看了 Lea Verou 的 CSS SECRETS 后,我受到了启发——可以用渐变背景的方式去实现。
在html中内容中的多个空格一般会被视为一个,连续的多个空格符被自动合并了,同时内容前后的空格也会被清除。HTML空格保留的方式、CSS空格保留的方式。
为什么说能使用html/css解决的问题就不要使用JS呢?两个字,因为简单。简单就意味着更快的开发速度,更小的维护成本,同时往往具有更好的体验,下面介绍几个实例。
Ben Frain曾经说过,写css代码很容易,但是扩展和维护却很难。本文就介绍了一套方案来解决这个问题。OOCSS 意为面向对象的CSS。这种方法有两种主要 观点:结构与设计分离,容器和内容分离
我想每个人都应该归纳总结工作中的常见需求,编写一套适合自己的 CSS 框架。大多数的轻量级框架只是 CSS 框架,不涉及 JS 部分,主要用于网页的布局。
内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!