꾸준히 성장하는 개발자

[React] styled-reset _2 ( createGlobalStyle 전역 스타일로 관리 ) 본문

React

[React] styled-reset _2 ( createGlobalStyle 전역 스타일로 관리 )

ahleum 2022. 6. 7. 17:09

createGlobalStyle

- 한 컴포넌트를 만들 수 있게 해 주는데 렌더링 될 때, 그 컴포넌트는 전역 스코프에 스타일을 올려준다

 

 

import { createGlobalStyle } from 'styled-components'
const GlobalStyle = createGlobalStyle`
 body {
   	fontSize: 16px;
  }
`;

function App() {
  return (
    <>
      <GlobalStyle />
      <Router />
    </>
  );
}

export default App;

GlobalStyle이라는 전역적으로 넣어줄수 있는 컴포넌트를 생성하고

component들 가장 위에 넣어주면 된다.

 

위처럼 넣어주게 되면 전역적으로 스타일을 넣어준것이다.

모든 body의 폰트 사이즈는 16픽셀이 된다.

 

 

 

 

이걸 이용해서 초기에 styled-reset을 해줄 수 있다.

 

import { createGlobalStyle } from 'styled-components'

const GlobalStyle = 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;
}
*{
  box-sizing:border-box;
}

body{
  font-family: 'Source Sans Pro', sans-serif;
}
a{
  text-decoration: none;
  color: inherit;
}
`;

function App() {
  return (
    <>
      <GlobalStyle />
      <Router />
    </>
  );
}

export default App;

'React' 카테고리의 다른 글

[React] vite 사용해보기  (0) 2022.06.13
[React Hook] useParams  (0) 2022.06.08
[React] styled-reset _1 (npm i styled-reset)  (0) 2022.06.07
[React] 컨디셔널 렌더링 ??  (0) 2022.05.31
[React with TypeScript]  (1) 2022.05.27