关于 style-components
回顾React回顾React
style-components的基本使用
官方文档:https://www.styled-components.com/docs/basics
安装:npm install --save styled-components || yarn add styled-components
我的这个都是在一个组件中操作的,分页面创建style-components需要导出( export const 自定义名 = 准备创建style的名字 )在需要页面解构赋值引入
基础用法:
style-components最基础的用法就是以组件的形式编写样式
import styled from 'styled-components'; const HomeWrapper = styled.div ` width: 960px; margin: 0 auto; overflow: hidden; `; const HomeLeft = styled.div ` float: left; width: 625px; margin-left: 15px; padding-top: 30px; .bannder-img { width: 625px; height: 270px; } `; const HomeRight = styled.div ` float: right; width: 280px; margin-left: 15px; padding-top: 30px; `; render () { return ( <HomeWrapper> <HomeLeft> left </HomeLeft> <HomeRight> right </HomeRight> </HomeWrapper> ) }
上面的代码定义了三个组件,分别为HomeWrapper 、HomeLeft 、HomeRight
,这样每一个组件对应唯一的样式,不在出现样式污染的情况。
如果在其他组件中使用,以解构赋值的方式引入需要的样式,然后当做标签使用与上方使用相同
定义全局样式
import { createGlobalStyle } from 'styled-components';
const GrobalStyle = createGlobalStyle `
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
@font-face {
font-family: 'iconfont'; /* project id 897264 */
src: url('//at.alicdn.com/t/font_897264_7ma62sn10m3.eot');
src: url('//at.alicdn.com/t/font_897264_7ma62sn10m3.eot?#iefix') format('embedded-opentype'),
url('//at.alicdn.com/t/font_897264_7ma62sn10m3.woff') format('woff'),
url('//at.alicdn.com/t/font_897264_7ma62sn10m3.ttf') format('truetype'),
url('//at.alicdn.com/t/font_897264_7ma62sn10m3.svg#iconfont') format('svg');
}
.iconfont {
font-family:"iconfont" !important;
font-size:16px;
font-style:normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.clearfix:after {visibility: hidden;display: block;font-size: 0;content: ".";clear: both;height: 0;}
.clearfix {zoom: 1;}
`;
上面的代码GrobalStyle是全局样式组件,只需在React组件的最外层引入即可。
引入图片
需要图片引入,如果像css一样的引入方式,会报错。正确的引入方式是import导入,再以变量的方式引入,如下:
import styled from 'styled-components';
import logPic from '../../statics/images/logo.png';
export const Logo = styled.div `
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 56px;
background-image: url(${logPic});
background-size: contain;
`;
上面的代码logPic是存放logo图片地址的变量,只需使用${logPic}的方式引入即可