在index.js的jsx部分加入标签,然后在其头部引用,最后去styled.js中写相关的样式:
index.js完整代码:
import React, { Component } from 'react';
import {
HeadrerWrapper,
Logo,
Nav,
NavItem,
NavSearch,
Addition,
Button,
} from './style'
class Header extends Component{
render() {
return (
<HeadrerWrapper>
<Logo href='/' />
<Nav>
<NavItem className='left active'>首页</NavItem>
<NavItem className='left'>下载App</NavItem>
<NavItem className='right'>登录</NavItem>
<NavItem className='right'>Aa</NavItem>
<NavSearch></NavSearch>
</Nav>
<Addition>
<Button className='writting'>写文章</Button>
<Button className='reg'>注册</Button>
</Addition>
</HeadrerWrapper>
)
}
}
export default Header;
styled.js完整代码:
import styled from 'styled-components'
import logoPic from '../../statics/logo.png'
export const HeadrerWrapper = styled.div`
position:relative;
height:56px;
border-bottom:solid 1px #f0f0f0;
`;
export const Logo = styled.a`
height:56px;
width:100px;
position:absolute;
top:0;
left:0;
display:block;
background:url(${logoPic});
background-size:contain;
`;
export const Nav = styled.div`
width:960px;
height:100%;
margin:0 auto;
padding-right:60px;
/*保证宽度不变*/
box-sizing:border-box;
`;
export const NavItem = styled.div`
line-height:56px;
padding: 0 15px;
font-size:17px;
color:#333;
&.left {
float:left;
}
&.right {
float:right;
color:#969696;
}
&.active {
color:#ea6f5a;
}
`;
export const NavSearch = styled.input.attrs({
placeholder: '搜索'
})`
width:160px;
height:38px;
border:none;
outline:none;
border-radius:19px;
background:#eee;
margin-top:9px;
margin-left:20px;
padding:0 20px;
box-sizing:border-box;
font-size:14px;
/*当前组件下的placeholder*/
&::placeholder{
color:#999;
}
`;
export const Addition = styled.div`
position:absolute;
right:0;
top: 0;
height:56px;
` ;
export const Button = styled.button`
float:right;
margin-top:9px;
margin-right:20px;
padding:0 20px;
line-height:38px;
border-radius:19px;
border:1px solid #ec6149;
font-size:14px;
&.reg{
color:#ec6149;
background:white;
}
&.writting{
color:#fff;
background:#ec6149;
}
`
效果: