웹사이트의 레이아웃을 구성하는 버튼, Box, 텍스트 등의 컴포넌트에 색깔, 크기, 투명도, 등을 설정하는 방법
Cascading Style Sheets
계단식 스타일 시트
각각의 스타일에 적용 규칙을 부여하고, 이 조건에 부합하는 Element에 스타일을 적용하는 것
h1 {
color: green;
font-size: 16px;
}
어떤 Element에 적용할지 결정하는 문법
// Universal Selector
* {...}
// 모든 <h1>에 대한 스타일
h1 {...}
// ID가 header인 Element에 대한 스타일
#header {...}
// Class가 TodoListItem인 Element에 대한 스타일
.TodoListItem {...}
// Class가 medium인 <p>의 스타일
p.medium {...}
// h1, h2, p를 모두 같은 스타일로 설정
h1, h2, p {...}
// button이 hover 상태일 때
button:hover {...}
display
div {
display: none | block | inline | flex;
}
visibility
div {
visibility: visible | hidden;
}
position
div {
position: static | fixed | relative | absolute;
}
크기 관련
div {
width: auto | value;
height: auto | value;
min-width: auto | value;
min-height: auto | value;
max-width: auto | value;
max-height: auto | value;
}
Flexbox
레이아웃을 더 자유롭게 설정할 수 있게 해주는 속성
다음과 같은 Flex Container의 아이템들이 있을 때, 이 아이템들을 어떤 방향과 순서로 배치할지 정의할 수 있다.div { display: flex; // 필수 flex-direction: row | column | row-reverse | column-reverse; align-items: stretch | flex-start | center | flex-end | baseline; justify-content: flex-start | center | flex-end | space-between | space-around; }
font-family
// 기본
#text {
font-family: "(실제 사용할 글꼴의 이름)"
}
// farback: 지정된 글꼴을 찾지 못했을 경우를 방자한 대비책
#text {
font-family: "familyName", fameilyVar, (serif | sans-serif | monospace | cursive | fnatasy);
}
* 맨 뒤에 있는 글꼴 분류 속성 (대체로 여기 5개 중 하나임)
font-size
단위
font-weight (=두께)
font-style
background-color
div {
background-color: color | transparent;
}
transparent = 투명한
색상 값
border
div {
border: border-width border-style border-color;
}
CSS 문법을 그대로 사용하면서, 결과물을 Styling된 컴포넌트로 만들어주는 오픈소스 라이브러리
# npm을 사용하는 경우
npm install --save styled-components
# yarn을 사용하는 경우
yarn add styled-components
literal
소스코드 상에 명시된 상수
let num = 20; // 20이 literal
template literal
백틱스(`)를 사용하여 문자열을 작성하고, 그 안에 대체 가능한 Expression을 넣는 방식
// Untagged Template Literal
// 단순한 문자열
`string text`;
// 여러 줄에 걸친 문자열
`string text line 1
string text line 2`;
// 대체 가능한 expression이 들어있는 문자열
`string text ${expression} string text`;
// Tagged Template Literal
// myFunc의 파라미터로 expression으로,
// 구분된 문자열 배열과 expression이 순서대로 들어간 형태로 호출됨
myFunc`string text ${expression} string text`;
import React from "react";
import sytled from 'styled-components';
const Warpper = styled.div`
padding: 1em;
background: grey;
`;
const Title = styled.h1`
font-sise: 1.5em;
color: white;
text-align: center;
`;
function MainPage(props) {
return (
<Warpper>
<Title>
안녕, 리액트!
</Title>
</Warpper>
);
}
export default MainPage;
import React from "react";
import sytled from 'styled-components';
const Button = styled.button`
color: ${props => props.dart ? "white" : "dart"};
bacground: ${props => props.dart ? "black" : "white"};
border: 1px solid black;
`;
function Sample(props) {
return(
<div>
<Button>Normal</Button>
<Button dark>Dark</Button>
</div>
);
}
export default Sample
import React from "react";
import sytled from 'styled-components';
const Button = styled.button`
color: grey;
border: 2px solid palevioletred;
`;
// Button에 style을 추가(확장)한 style 컴포넌트
const RoundedButton = styled(Button)`
border-radius: 16px;
`;
function Sample(props) {
return(
<div>
<Button>Normal</Button>
<RoundedButton>Rounded</RoundedButton>
</div>
);
}
export default Sample