styled-components 전역 스타일링 (Global style)

2023. 2. 24. 13:36·TECH

컴포넌트 별로 스타일을 적용할 수 있지만 
reset과 같은 공통 css는 전역으로 선언을 해야한다. 

npm으로 설치해서 사용하는 방법(https://github.com/zacanger/styled-reset)과 
직접 스타일을 만들어서 import하는 방법이 있다. 

아래는 직접 스타일 파일을 만들어서 import하는 방법이다.

styled-components 에서는 createGlobalStyle을 제공해 전역으로 스타일링 할 수 있게 도와준다. 

 

reset.css - https://github.com/zacanger/styled-reset/blob/master/src/index.ts

 

1. GlobalStyle 파일을 만들고 reset css를 선언한다.

// GlobalStyle.tsx

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, menu, 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,
  main, menu, nav, output, ruby, section, summary,
  time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
  }
`;

export default GlobalStyle;

2. 최상위 컴포넌트에 만든 GlobalStyle를 import한다. 

import GlobalStyle from "./GlobalStyle";
import Router from "./Router";

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

export default App;


전역 스타일에 구글 폰트 설정하기 
구글폰트사이트에서 원하는 폰트를 고르고 import 코드를 GlobalStyle에 넣어준다. 

import { createGlobalStyle } from "styled-components";

const GlobalStyle = createGlobalStyle`
  @import url('https://fonts.googleapis.com/css2?family=Noto+Sans:wght@300;400;600&family=Source+Sans+Pro:wght@300;400&display=swap');
   * {font-family: 'Noto Sans', sans-serif;}
`;

export default GlobalStyle;



 

 

'TECH' 카테고리의 다른 글

Recoil 개념  (0) 2023.03.02
React Hook Form  (0) 2023.03.01
styled-components로 theme적용하기 with Typescript  (0) 2023.02.22
styled-components 기본 문법  (0) 2023.02.22
styled-components, emotion 비교  (0) 2023.02.22
'TECH' 카테고리의 다른 글
  • Recoil 개념
  • React Hook Form
  • styled-components로 theme적용하기 with Typescript
  • styled-components 기본 문법
ssund
ssund
  • ssund
    ssund의 기술블로그
    ssund
  • 전체
    오늘
    어제
    • 분류 전체보기 (73)
      • TECH (22)
      • NOTE (40)
      • DAILY (7)
      • javascript (1)
      • 알고리즘 (0)
  • 블로그 메뉴

    • 홈
    • TECH
    • NOTE
    • DAILY
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    웹브라우저구성
    call signatures
    커머스프로젝트
    함수와 메서드차이
    redux
    reduxtoolkit
    TypeScript
    React
    JavaScript
    Array.sort()
    git배포
    theme-provider
    slidesPerGroup
    타입스크립트
    react state management
    reat-head
    styled-components
    d.ts
    global-style
    배열요소순서
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
ssund
styled-components 전역 스타일링 (Global style)
상단으로

티스토리툴바