프론트엔드 개발에서 사용자의 경험을 최우선으로 고려하는 것은 매우 중요합니다. 특히, 웹사이트의 네비게이션 바는 사용자가 사이트를 탐색하는 데 있어 핵심적인 역할을 합니다. 이번 글에서는 Tailwind CSS를 활용하여 쉽고 빠르게 반응형 네비게이션 바를 만드는 방법에 대해 알아보겠습니다.

Tailwind CSS란?

Tailwind CSS는 유틸리티 우선 CSS 프레임워크로, 클래스 기반으로 스타일을 적용할 수 있게 해줍니다. 이러한 접근 방식은 개발자가 더 직관적이고 빠르게 디자인할 수 있도록 도와줍니다. 반응형 디자인을 쉽게 구현할 수 있는 다양한 유틸리티 클래스가 제공되기 때문에, 복잡한 CSS를 작성할 필요 없이 간단하게 스타일링할 수 있습니다.

프로젝트 설정

먼저 Tailwind CSS를 사용하기 위해 프로젝트를 설정해야 합니다. 아래는 Tailwind CSS를 CDN 방식으로 간편하게 포함하는 방법입니다.

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>반응형 네비게이션 바</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
    <!-- 네비게이션 바가 이곳에 위치합니다 -->
</body>
</html>

기본 네비게이션 바 구조 만들기

네비게이션 바는 일반적으로 로고와 메뉴 항목으로 구성됩니다. 아래는 Tailwind CSS를 이용한 기본적인 네비게이션 바의 구조입니다.

<nav class="bg-white shadow">
    <div class="max-w-7xl mx-auto px-2 sm:px-6 lg:px-8">
        <div class="flex justify-between h-16">
            <div class="flex">
                <div class="flex-shrink-0">
                    <a href="#" class="text-xl font-bold text-gray-800">로고</a>
                </div>
                <div class="hidden md:flex space-x-4">
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">홈</a>
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">소개</a>
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">서비스</a>
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">연락처</a>
                </div>
            </div>
        </div>
    </div>
</nav>

반응형 네비게이션 바 구현하기

위의 코드에서는 기본적인 네비게이션 바를 만들었습니다. 이제 반응형 기능을 추가해 보겠습니다. 작은 화면에서는 메뉴를 숨기고 햄버거 아이콘을 통해 메뉴를 제어할 수 있도록 하겠습니다.

<div class="md:hidden">
    <button class="text-gray-500 hover:text-gray-700 focus:outline-none focus:text-gray-700" id="menu-button">
        <svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16m-7 6h7"></path>
        </svg>
    </button>
</div>
<div class="hidden md:flex space-x-4" id="menu">
    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">홈</a>
    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">소개</a>
    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">서비스</a>
    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">연락처</a>
</div>

이제 JavaScript를 사용하여 햄버거 버튼 클릭 시 메뉴를 토글할 수 있도록 추가하겠습니다.

<script>
    const menuButton = document.getElementById('menu-button');
    const menu = document.getElementById('menu');

    menuButton.addEventListener('click', () => {
        menu.classList.toggle('hidden');
    });
</script>

최종 코드

모든 코드를 통합하면 최종적으로 아래와 같은 구조가 됩니다.

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>반응형 네비게이션 바</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
<nav class="bg-white shadow">
    <div class="max-w-7xl mx-auto px-2 sm:px-6 lg:px-8">
        <div class="flex justify-between h-16">
            <div class="flex">
                <div class="flex-shrink-0">
                    <a href="#" class="text-xl font-bold text-gray-800">로고</a>
                </div>
                <div class="md:hidden">
                    <button class="text-gray-500 hover:text-gray-700 focus:outline-none focus:text-gray-700" id="menu-button">
                        <svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16m-7 6h7"></path>
                        </svg>
                    </button>
                </div>
                <div class="hidden md:flex space-x-4" id="menu">
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">홈</a>
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">소개</a>
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">서비스</a>
                    <a href="#" class="text-gray-700 hover:bg-gray-200 px-3 py-2 rounded-md text-sm font-medium">연락처</a>
                </div>
            </div>
        </div>
    </div>
</nav>
<script>
    const menuButton = document.getElementById('menu-button');
    const menu = document.getElementById('menu');

    menuButton.addEventListener('click', () => {
        menu.classList.toggle('hidden');
    });
</script>
</body>
</html>

마무리

이번 포스트에서는 Tailwind CSS를 사용하여 반응형 네비게이션 바를 만드는 방법을 살펴보았습니다. Tailwind의 유틸리티 클래스를 활용하면 복잡한 CSS 코드를 작성할 필요 없이 쉽게 스타일링할 수 있습니다. 여러분의 프로젝트에서 Tailwind CSS를 활용하여 훌륭한 사용자 경험을 만들어 보시기 바랍니다!