回顧React回顧Reactcss
style-components的基本使用html
官方文檔:https://www.styled-components.com/docs/basicsweb
安裝:npm install --save styled-components || yarn add styled-components
shell
個人這個都是在一個組件中操做的,分頁面建立style-components須要導出( export const 自定義名 = 準備建立style的名字 )在須要頁面解構賦值引入npm
基礎用法:canvas
style-components最基礎的用法就是以組件的形式編寫樣式ruby
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
定義全局樣式app
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組件的最外層引入便可。
引入圖片ide
須要圖片引入,若是像css同樣的引入方式,會報錯。正確的引入方式是import導入,再以變量的方式引入,以下:svg
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}的方式引入便可