본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Tailwind CSS Astro 시작하기 (Getting started with Astro)

원문 보기 위키 갱신

Astro는 컴포넌트 아일랜드(component islands), 서버 우선 API 설계, 엣지 레디 배포를 특징으로 하는 콘텐츠 중심 웹사이트용 풀스택 웹 프레임워크예요. Tailwind CSS, Flowbite, React, Vue, Svelte 등 수백 개의 기술과 통합을 지원합니다. Firebase, NordVPN, The Guardian, Trivago 같은 수천 개의 평판 좋은 기업·프로젝트가 사용하며, 2022년 초 700만 달러 시드 투자를 받아 기술의 지속적인 지원과 개발이 보장됩니다.

출처: 문서

본문

이 튜토리얼의 다음 단계를 따라 새 Astro 프로젝트를 만들고, Tailwind CSS를 설치하고, Flowbite의 UI 컴포넌트를 사용해 웹사이트를 더 빠르게 구축하는 방법을 배워 보세요.

사전 요구사항 (Requirements)

계속하기 전에 로컬 머신과 프로덕션 서버에 Node.js(v16.12.0 이상)가 설치되어 있는지 확인해 필수 의존성을 설치해야 해요. VS Code를 표준 편집기로 쓰고, 원저자가 출시한 VS Marketplace의 공식 Astro 언어 지원 확장도 설치하는 것을 권장합니다.

Astro 프로젝트 만들기 (Create an Astro project)

  • NPM으로 다음 명령을 실행해 새 Astro 프로젝트를 만들어요:
npm create astro@latest flowbite-project
cd flowbite-project

이 명령은 몇 가지 질문을 표시하고, 답변에 따라 로컬 프로젝트를 만들어요.

  • 다음 명령을 실행해 로컬 개발 서버를 시작해요:
npm run dev

이렇게 하면 브라우저에서 http://localhost:3000으로 프로젝트에 접근할 수 있어요.

  • 프로젝트의 프로덕션 빌드를 만들려면 터미널에서 다음 명령을 실행해요:
npm run build

Astro의 가장 큰 장점 중 하나는 build 명령으로 프로덕션에 배포했을 때 작은 빌드 크기예요 — 이 덕분에 기존 기술보다 훨씬 빠르게 웹사이트가 로드됩니다.

Tailwind CSS 설치 (Install Tailwind CSS)

Astro 프로젝트를 설치·구성했으니 이제 공식 패키지를 기반으로 Tailwind CSS 통합을 설치할 수 있어요.

  • NPX 명령으로 Tailwind CSS를 설치하고 구성 파일을 만들어요:
npx astro add tailwind

이 명령은 package.json 파일에 Tailwind CSS를 자동으로 설치하고, 컴파일 과정을 구성하며, 템플릿 경로를 구성하는 새 tailwind.config.cjs 파일을 만듭니다.

  • Layout.astro 파일에 global.css 파일을 가져와요:
---
import "../styles/global.css";
---

이제 어떤 템플릿 파일에서든 Tailwind CSS 클래스를 작성할 수 있고, 변경 사항은 global.css 파일을 생성해 모든 페이지에 포함하면서 적용됩니다.

Flowbite 설치 (Install Flowbite)

Astro와 Tailwind CSS를 모두 설치했다면 Flowbite의 무료 오픈소스 UI 컴포넌트를 사용해 내비바, 모달, 드롭다운 같은 인터랙티브 요소로 웹사이트·UI 개발을 더 빠르게 만들 수 있어요.

  • NPM을 사용해 Flowbite를 의존성으로 설치해요:
npm install flowbite --save
  • 메인 global.css CSS 파일 안에 Flowbite의 기본 테마 변수를 가져와요:
@import "flowbite/src/themes/default";
  • CSS에 Flowbite 플러그인 파일을 가져와요:
@plugin "flowbite/plugin";
  • global.css 파일에서 Flowbite의 소스 파일을 구성해요:
@source "../../node_modules/flowbite";

이제 Flowbite의 CSS 스타일을 구성했으니 이어서 JS를 설치하면 돼요.

Flowbite 컴포넌트 (Flowbite components)

인터랙티브 컴포넌트를 활성화하려면 Flowbite의 JavaScript 파일도 포함해야 해요. 메인 Layout.astro 파일에 CDN 파일로 포함하거나 Flowbite 모듈을 가져와서 할 수 있습니다.

CDN으로 포함 (Include via CDN)

Layout.astro 파일에서 </body> 태그가 끝나기 직전에 다음 script 태그를 추가해요:

<script is:inline src="https://cdn.jsdelivr.net/npm/flowbite@4.0.1/dist/flowbite.min.js"></script>

이렇게 하면 Flowbite 컴포넌트 예시의 데이터 속성(data attributes)을 사용할 수 있고, 커스텀 JavaScript를 작성하지 않아도 자동으로 인터랙티브하게 동작해요. Flowbite Docs에서 코드를 그대로 복사·붙여넣기하면 됩니다.

ESM/CJS 가져오기 (ESM/CJS imports)

대안으로 Modal 같은 독립 컴포넌트를 가져와 Astro 파일의 로컬 <script> 태그 안에서 이벤트 리스너를 직접 설정할 수도 있어요. v1.6.0부터 Flowbite가 TypeScript의 타입 선언·인터페이스도 지원해서, TypeScript를 JavaScript보다 명확히 권장하는 Astro 생태계와 더 잘 통합할 수 있습니다.

예를 들어 script 태그 안에 다음 코드를 추가해 Flowbite JS API와 Astro를 활용할 수 있어요:

<Layout>
  <!-- markup source content and elements -->
</Layout>

<script>
  import { Modal } from 'flowbite'

  // select the two elements that we'll work with
  const $buttonElement: HTMLElement | null = document.querySelector('#button');
  const $modalElement: HTMLElement | null = document.querySelector('#defaultModal');

  // create a new modal component
  const modal = new Modal($modalElement);

  // toggle the visibility of the modal when clicking on the button
  if ($buttonElement) {
    $buttonElement.addEventListener('click', () => modal.toggle());
  }
</script>

위 예시에 설명된 이벤트 리스너·요소에 필요한 HTML 마크업이 Astro의 <Layout> 태그 안에 있는지 확인하세요:

<Layout>
    <!-- Modal toggle -->
    <button data-modal-target="default-modal" data-modal-toggle="default-modal" class="text-white bg-brand box-border border border-transparent hover:bg-brand-strong focus:ring-4 focus:ring-brand-medium shadow-xs font-medium leading-5 rounded-base text-sm px-4 py-2.5 focus:outline-none" type="button">
    Toggle modal
    </button>
    <!-- Main modal -->
    <div id="default-modal" tabindex="-1" aria-hidden="true" class="hidden overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 z-50 justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full">
        <div class="relative p-4 w-full max-w-2xl max-h-full">
            <!-- Modal content -->
            <div class="relative bg-neutral-primary-soft border border-default rounded-base shadow-sm p-4 md:p-6">
                <!-- Modal header -->
                <div class="flex items-center justify-between border-b border-default pb-4 md:pb-5">
                    <h3 class="text-lg font-medium text-heading">
                        Terms of Service
                    </h3>
                    <button type="button" class="text-body bg-transparent hover:bg-neutral-tertiary hover:text-heading rounded-base text-sm w-9 h-9 ms-auto inline-flex justify-center items-center" data-modal-hide="default-modal">
                        <svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18 17.94 6M18 18 6.06 6"/></svg>
                        <span class="sr-only">Close modal</span>
                    </button>
                </div>
                <!-- Modal body -->
                <div class="space-y-4 md:space-y-6 py-4 md:py-6">
                    <p class="leading-relaxed text-body">
                        With less than a month to go before the European Union enacts new consumer privacy laws for its citizens, companies around the world are updating their terms of service agreements to comply.
                    </p>
                    <p class="leading-relaxed text-body">
                        The European Union’s General Data Protection Regulation (G.D.P.R.) goes into effect on May 25 and is meant to ensure a common set of data rights in the European Union. It requires organizations to notify users as soon as possible of high-risk data breaches that could personally affect them.
                    </p>
                </div>
                <!-- Modal footer -->
                <div class="flex items-center border-t border-default space-x-4 pt-4 md:pt-5">
                    <button data-modal-hide="default-modal" type="button" class="text-white bg-brand box-border border border-transparent hover:bg-brand-strong focus:ring-4 focus:ring-brand-medium shadow-xs font-medium leading-5 rounded-base text-sm px-4 py-2.5 focus:outline-none">I accept</button>
                    <button data-modal-hide="default-modal" type="button" class="text-body bg-neutral-secondary-medium box-border border border-default-medium hover:bg-neutral-tertiary-medium hover:text-heading focus:ring-4 focus:ring-neutral-tertiary shadow-xs font-medium leading-5 rounded-base text-sm px-4 py-2.5 focus:outline-none">Decline</button>
                </div>
            </div>
        </div>
    </div>
</Layout>

이 예시에서 버튼을 클릭하면 모달이 표시되고, 제공한 옵션을 기반으로 모달 컴포넌트가 초기화됩니다. Flowbite 컴포넌트 전체 컬렉션을 둘러보고 각 페이지의 "JavaScript Behaviour" 섹션에서 활용할 수 있는 모든 옵션·메서드·객체를 확인해 보세요.

Astro 스타터 프로젝트 (Astro starter project)

Flowbite와 Astro의 무료 오픈소스 스타터 프로젝트도 GitHub에 구축해 두었어요. 이 통합 가이드를 바탕으로 한 참고·예시로 사용해 Astro, Tailwind CSS, Flowbite의 UI 컴포넌트로 더 빠르게 시작할 수 있습니다.

Astro 어드민 대시보드 (Astro admin dashboard)

GitHub의 오픈소스 Astro 어드민 대시보드 프로젝트를 확인해 Flowbite Library, Tailwind CSS 프레임워크, Astro 모범 사례·프레임워크 설정으로 미리 정의된 CRUD 레이아웃과 API 호출을 활용할 수 있어요.

더 알아보기 (Learn more)