最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Navbar and Logo are not aligning on the same row in Next js - Stack Overflow

programmeradmin5浏览0评论

I'm building a Navbar component in my Next.js application with Tailwind CSS, but I'm having trouble getting the logo and navigation items to appear correctly in the same row with proper spacing (logo on left, nav items on right).

Current Code

// Navbar.tsx
import React from "react";
import Link from "next/link";
import Image from "next/image";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import LoginButton from "./NavbarButtons/LoginButton";
import UserNavMenu from "./NavbarButtons/UserNavMenu";

const Navbar = async () => {
    const session = await getServerSession(authOptions);
    
    return (
        <div className="w-full px-5 py-3 bg-white shadow-sm font-work-sans">
            <nav className="flex items-center justify-between max-w-7xl mx-auto">
                <Link href="/" className="flex-shrink-0">
                    <Image src="/logo.png" alt="logo" width={144} height={30} />
                </Link>
                
                <div className="flex items-center gap-5">
                    {session?.user && session.user.id ? (
                        <>
                            <Link href="/startup/create">Create</Link>
                            <UserNavMenu user={{
                                id: session.user.id,
                                name: session.user.name,
                                email: session.user.email,
                                image: session.user.image
                            }} />
                        </>
                    ) : (
                        <LoginButton />
                    )}
                </div>
            </nav>
        </div>
    );
};

export default Navbar;
// layout.tsx
import Provider from "./provider";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <Provider>{children}</Provider>
      </body>
    </html>
  );
}

Expected Behavior

  1. Logo should be on the left side
  2. Navigation items should be on the right side
  3. Both should be in the same row with proper alignment
  4. Full width of the navbar

What I've Tried

  1. Using flex with justify-between on the navbar
  2. Adding flex-shrink-0 to the logo
  3. Nesting multiple flex containers
  4. Adjusting width properties with w-full
  5. Using different combinations of flex alignment properties

I'd appreciate any help to fix this layout issue!

Environment

  1. Next.js 14
  2. Tailwind CSS
  3. Using App Router structure

Current Output:

Expected Output:

I'm building a Navbar component in my Next.js application with Tailwind CSS, but I'm having trouble getting the logo and navigation items to appear correctly in the same row with proper spacing (logo on left, nav items on right).

Current Code

// Navbar.tsx
import React from "react";
import Link from "next/link";
import Image from "next/image";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import LoginButton from "./NavbarButtons/LoginButton";
import UserNavMenu from "./NavbarButtons/UserNavMenu";

const Navbar = async () => {
    const session = await getServerSession(authOptions);
    
    return (
        <div className="w-full px-5 py-3 bg-white shadow-sm font-work-sans">
            <nav className="flex items-center justify-between max-w-7xl mx-auto">
                <Link href="/" className="flex-shrink-0">
                    <Image src="/logo.png" alt="logo" width={144} height={30} />
                </Link>
                
                <div className="flex items-center gap-5">
                    {session?.user && session.user.id ? (
                        <>
                            <Link href="/startup/create">Create</Link>
                            <UserNavMenu user={{
                                id: session.user.id,
                                name: session.user.name,
                                email: session.user.email,
                                image: session.user.image
                            }} />
                        </>
                    ) : (
                        <LoginButton />
                    )}
                </div>
            </nav>
        </div>
    );
};

export default Navbar;
// layout.tsx
import Provider from "./provider";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <Provider>{children}</Provider>
      </body>
    </html>
  );
}

Expected Behavior

  1. Logo should be on the left side
  2. Navigation items should be on the right side
  3. Both should be in the same row with proper alignment
  4. Full width of the navbar

What I've Tried

  1. Using flex with justify-between on the navbar
  2. Adding flex-shrink-0 to the logo
  3. Nesting multiple flex containers
  4. Adjusting width properties with w-full
  5. Using different combinations of flex alignment properties

I'd appreciate any help to fix this layout issue!

Environment

  1. Next.js 14
  2. Tailwind CSS
  3. Using App Router structure

Current Output:

Expected Output:

Share Improve this question edited Mar 25 at 12:51 Radical Rosh asked Mar 25 at 11:43 Radical RoshRadical Rosh 1046 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I tried removing the div after the tag and wrapped them in a div with className - justify-center it worked just right for me also upgrading to tailwind v4 and updating my global.css which I didn't provided solve the issue.

Navbar.tsx:

import React from "react";
import Link from "next/link";
import Image from "next/image";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import LoginButton from "./NavbarButtons/LoginButton";
import UserNavMenu from "./NavbarButtons/UserNavMenu";

const Navbar = async () => {
    const session = await getServerSession(authOptions);
    
    return (
        <div className="w-full px-5 py-3 bg-white shadow-sm font-work-sans">
            <nav className="flex items-center max-w-7xl mx-auto">

                <div className="flex items-center space-x-6 justify-between">
                {/* Logo */}
                <Link href="/" className="flex-shrink-0">
                    <Image src="/logo.png" alt="logo" width={144} height={30} />
                </Link>

                {/* Navigation Items */}
                    {session?.user && session.user.id ? (
                        <>
                            <Link href="/startup/create" className="text-gray-700 hover:text-gray-900">Create</Link>
                            <UserNavMenu user={{
                                id: session.user.id,
                                name: session.user.name,
                                email: session.user.email,
                                image: session.user.image
                            }} />
                        </>
                    ) : (
                        <LoginButton />
                    )}
                </div>
            </nav>
        </div>
    );
};

export default Navbar;

Fixed Navbar Component

import React from "react";
import Link from "next/link";
import Image from "next/image";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import LoginButton from "./NavbarButtons/LoginButton";
import UserNavMenu from "./NavbarButtons/UserNavMenu";

const Navbar = async () => {
  const session = await getServerSession(authOptions);

  return (
    <header className="w-full bg-white shadow-md">
      <div className="max-w-7xl mx-auto px-5 py-4 flex items-center justify-between">
        
       
        <Link href="/" className="flex items-center">
          <Image 
            src="/logo.png" 
            alt="logo" 
            width={144} 
            height={30} 
            className="h-auto w-auto" 
            priority
          />
        </Link>

        
        <nav className="flex items-center gap-6">
          {session?.user && session.user.id ? (
            <>
              <Link href="/startup/create" className="text-gray-700 hover:text-black transition">
                Create
              </Link>
              <UserNavMenu
                user={{
                  id: session.user.id,
                  name: session.user.name,
                  email: session.user.email,
                  image: session.user.image,
                }}
              />
            </>
          ) : (
            <LoginButton />
          )}
        </nav>
      </div>
    </header>
  );
};

export default Navbar;

Explanation of Changes

  1. Container Wrapping:

    • Added max-w-7xl mx-auto for centered content.
    • Applied padding px-5 py-4.
  2. Logo Section:

    • Wrapped the logo in a Link with flex items-center to ensure proper alignment.
    • Added className="h-auto w-auto" to maintain the image's natural proportions.
    • Used priority for faster loading.
  3. Navigation Section:

    • Wrapped the nav items in a <nav> with flex items-center gap-6 for spacing.
    • Added text-gray-700 hover:text-black with a transition effect for a smoother hover interaction.

Tips :

  • max-w-7xl: Keeps content centered and prevents stretching on large screens.
  • justify-between: Ensures the logo and nav items are on opposite sides.
  • gap-6: Adds consistent spacing between nav items.
发布评论

评论列表(0)

  1. 暂无评论