美文网首页
React style/css样式 styling compon

React style/css样式 styling compon

作者: 33jubi | 来源:发表于2020-05-23 13:18 被阅读0次
Styling react components & elements
Setting styles dynamically 动态改变css

(Radium)--支持:hover和media queries
(styled component)--支持:hover和media queries( regular css)
1.用html style属性时:js在处理变化的时候同时改变style值
2.用className:

  • 常用push
const classes=[]
if(this.state.persons.length <= 2){
  classes.push('red');
}
if(this.state.persons.length <= 1){
  classes.push('bold');
}
...
<div className={classes.join(' ')}></div>
Radium
  • using Radium
    npm install --save radium
import Radium from 'radium'

render(){
  const style={
   backgroundColor:'#ccc';
   font:'inherit';
   border:'1px solid blue;
   cursor:'pointer'
   ':hover':{
     background:'lightgreen',
     color:'black'
   }
  };
  if(this.state.show){
    ...
    style[':hover']={
      background:'salmon',
      color:'black'
    }
  }
  return(<div>
    <button  style={style}></button>
  </div>)
}

export default radium(App);
  • using Radium for media queries
import Radium,{StyleRoot} from 'radium'

const style={
  '@media (min-width:500px):{
    width:500px;
  }
}

...
<StyleRoot>
  <div style={style}>
    ...
   </div>
</StyleRoot>//这个需要包装在整个app之外,而包装class的radium是包装在相应的class外


...
export default radium(App);
  • styled component library
    remove all radium /
import styled from 'styled-components'
const StyledDiv = styled.div``//!Upper case start
  width:60%;
  margin:16px auto;
  border:1px solid #eee;
  @media (min-width:500px){
    .Person{
      width:450px;
    }
  }
...
return(
  <StyledDiv></StyledDiv>
)  

  • styled component & dynamic style
import styled from 'styled-components'
const StyledDiv = styled.div``//!Upper case start
  background-color:${props=>props.alt?'red':'blue';
  width:60%;
  margin:16px auto;
  border:1px solid #eee;
  @media (min-width:500px){
    .Person{
      width:450px;
    }
  }
...
return(
  <StyledDiv alt={this.state.show} onClick={...}></StyledDiv>
)  
  • CSS Modules
/* style.css */
.className {
  color: green;
}


...
import styles from "./style.css";
// import { className } from "./style.css";

element.innerHTML = '<div class="' + styles.className + '">';

In Post.css File

1.  .Post  {
2.  color: red;
3.  }

In Post Component File

1.  import classes from  './Post.css';

3.  const post =  ()  =>  (
4.  <div className={classes.Post}>...</div>
5.  );

Here, classes.Post refers to an automatically generated Post property on the imported classes object. That property will in the end simply hold a value like Post__Post__ah5_1 .
CSS Modules are a relatively new concept (you can dive super-deep into them here: https://github.com/css-modules/css-modules).
Link

相关文章

  • React style/css样式 styling compon

    Styling react components & elements Setting styles dynami...

  • HTML CSS

    Styling HTML with CSS CSS stands for Cascading Style Shee...

  • css in react

    在react设置css样式有两种方式,style和外部css引入 style 以{}包裹一个css对象 css引入...

  • 10.React中CSS动画19-04-30

    一.React中实现CSS过渡动画 App.js style.css 一.React中使用CSS动画 style....

  • CSS学习之CSS基础

    标签: 前端 css 样式 CSS样式 css全称为"层叠样式表(cascading style sheets)...

  • CSS知识点总结

    CSS 层叠样式表(表示层) 一、CSS引入方式 1.CSS行内样式 直接使用style属性 style=”wid...

  • 4C引用·选择器·伪类·css颜色·溢出·尺寸·倒角·边框阴影·

    CSS概述 CSS的作用 引入CSS的几种方式1.标签内引用style:style="样式名:样式值;..."2....

  • css

    引入css样式的四种方式: 内联样式(style属性) style 标签 外部文件 (CSS link) @imp...

  • CSS 基础

    1、认识 CSS 样式 1.1、css 样式的优势 css 全称为“层叠样式表(cascading style ...

  • CSS 基础(一)

    1、认识 CSS 样式 1.1、css 样式的优势 css 全称为“层叠样式表(cascading style ...

网友评论

      本文标题:React style/css样式 styling compon

      本文链接:https://www.haomeiwen.com/subject/gvikahtx.html