npm i styled-components
1-1. 패키지가 설치되었는지 확인하는방법
package.json파일에서 확인
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"styled-components": "^5.3.3",
}
import styled from 'styled-components';
const Wrap = styled.div`
backgeound:#fff;
`;
return(
<Wrap></wrap>
)
<button className={`${buttonColor} ${fontColor}`}>버튼</button>
유의사항
- 컴포넌트 이름은 대문자로 시작
- styled뒤에 사용해야할 HTML 태그명 입력
- 백틱으로 감싸서 작성
const Box = styled.div`
background-color: ${(props) => props.bgColor};
/*background: ${props => props.color || 'black'};*/
/*props있다면 나오고 없으면 black색 노출*/
width: 100px;
height: 100px;
`;
const App = () => {
return (
<Wrapper>
<Box bgColor={"#cf6a87"} />
<Box bgColor={"#574b90"} />
</Wrapper>
);
};
const Circle = styled(Box)`
border-radius: 50%;
`
const App = () => {
return (
<Wrapper>
<Box bgColor={"#cf6a87"} />
<Box bgColor={"#574b90"} />
<Circle bgColor={"black"} />
</Wrapper>
);
};
<Box as="button" bgColor={"#574b90"} />
const Circle = styled.div`
background: ${props => props.color || 'black'};
${props =>
props.huge &&
css`
width: 10rem;
height: 10rem;
`}
`;
return <Circle color="red" huge />;
const Input = styled.input.attrs({ required:true, maxLength:10 })`
background-color: orange;
margin-right: 5px;
`;
const Box = styled.div`
span {
background-color: black;
&:hover {
background-color: white;
}
}
`;
const BoxWrap = styled.section`
display:flex;
width:100vw;
${Box} {
background-color:black;
color: white;
}
`;
//BoxWrap안에 위치한 Box 컴포넌트에만 적용하는 방법
import styles from "./App.module.css";
<div className={styles.wrap}>
test
</div>