Search code examples
next.jsflexboxnavbartailwind-cssdaisyui

Tailwind and DaisyUI in a Next.js app, I can't get the navbar to stretch across the entire webpage


I have a next.js app that uses DaisyUI which is a tailwind based css component library. The problem is that I can't get any DaisyUI navbar to stretch all the way across the webpage horizontally. The navbar components stack up vertically crowding to the left side of the page.
enter image description here

This is the way that it's supposed to look
enter image description here

So I copied the JSX code from the DaisyUI example into my next.js app's Layout but as you can see from the first image it didn't work.

This is the relevant code from my app:"_app.tsx"

import '../styles/globals.css'
import type { AppProps } from 'next/app'
import Layout from '../components/Layout'

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  )
}

export default MyApp

"index.tsx"

import type { NextPage } from 'next'
import { useState } from 'react'
import styles from '../styles/Home.module.css'

const Home: NextPage = () => {
  return (
    <div className="hero min-h-screen bg-base-200">
      <div className="hero-content text-center">
        <div className="max-w-md">
          <h1 className="text-5xl font-bold">Hello visitor</h1>
          <p className="py-6">This is a placeholder for the landing page of my website</p>
          <button className="btn btn-primary">Get Started</button>
        </div>
      </div>
    </div>
  )
}

export default Home

"components/Layout.tsx"

import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'

const Layout = ({ children } : { children: any }) => {
    return (
        <div data-theme="synthwave" className={styles.container}>
            <Head>
                <title>Create Next App</title>
                <meta name="description" content="Generated by create next app" />
                <link rel="icon" href="/favicon.ico" />
            </Head>
            <div className="navbar bg-base-100">
                <div className="navbar-start">
                    <div className="dropdown">
                    <label tabIndex={0} className="btn btn-ghost btn-circle">
                        <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M4 6h16M4 12h16M4 18h7" /></svg>
                    </label>
                    <ul tabIndex={0} className="menu menu-compact dropdown-content mt-3 p-2 shadow bg-base-100 rounded-box w-52">
                        <li><a>Homepage</a></li>
                        <li><a>Portfolio</a></li>
                        <li><a>About</a></li>
                    </ul>
                    </div>
                </div>
                <div className="navbar-center">
                    <a className="btn btn-ghost normal-case text-xl">daisyUI</a>
                </div>
                <div className="navbar-end">
                    <button className="btn btn-ghost btn-circle">
                    <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" /></svg>
                    </button>
                    <button className="btn btn-ghost btn-circle">
                    <div className="indicator">
                        <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" /></svg>
                        <span className="badge badge-xs badge-primary indicator-item"></span>
                    </div>
                    </button>
                </div>
            </div>
            <main className={styles.main}>
                {children}
            </main>
            <footer className={styles.footer}>
                <a
                href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
                target="_blank"
                rel="noopener noreferrer"
                >
                Powered by{' '}
                <span className={styles.logo}>
                    <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
                </span>
                </a>
            </footer>
        </div>
    );
};

export default Layout

Also this is the tailwind.config.js file:

/** @type {import('tailwindcss').Config} */
module.exports = {
    //...
    content: ['./pages/**/*.{js,ts,jsx,tsx}'],
    plugins: [require("daisyui")],
    daisyui: {
      styled: true,
      themes: true,
      base: true,
      utils: true,
      logs: true,
      rtl: false
    }
  }

I don't understand why the navbar is getting scrunched together on the left side. As you can see, the synthwave theme is being loaded just fine. Also the Head, "Hello visitor" hero, Get Started button, etc... are all loading fine. The navbar just won't stretch. So why is the navbar not extending across the entire page horizontally?


Solution

  • The problem was with the tailwind.config.js file. I changed it to this:

    /** u/type {import('tailwindcss').Config} */
    module.exports = {
        //...
        content: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
        plugins: [require("daisyui")],
        daisyui: {
          styled: true,
          themes: true,
          base: true,
          utils: true,
          logs: true,
          rtl: false
        }
      }
    

    As you can see, I added './components/**/*.{js,ts,jsx,tsx}' to the content section of the file. This applies DaisyUI to the Layout.tsx file