Dekso
Dekso

Reputation: 561

How to center flex child based on parent width

I am trying to center the nav menu horizontally based on header tags width using tailwind. Problem is that it only centers based on the nav tags width.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <script src="https://cdn.tailwindcss.com"></script>
  </head>
  <body>
    <header class="flex justify-between items-center h-16 bg-gray-400 px-2">
      <div class="flex space-x-4 items-center">
        <img src="https://picsum.photos/500/100" alt="" class="h-12 w-auto" />
        <span>Company Name</span>
      </div>
      <nav
        class="flex space-x-4 border border-blue-400 flex-grow justify-center"
      >
        <a href="#">Home</a>
        <a href="#">Search</a>
      </nav>
      <div>
        Profile
      </div>
    </header>
  </body>
</html>

Upvotes: 1

Views: 121

Answers (1)

Jean Will
Jean Will

Reputation: 553

Like this? I added relative on <header> and absolute w-full on <nav>.

<header class="flex justify-between items-center h-16 bg-gray-400 px-2 relative">
  <div class="flex space-x-4 items-center">
    <img src="https://picsum.photos/500/100" alt="" class="h-12 w-auto" />
    <span>Company Name</span>
  </div>
  <nav class="flex space-x-4 border border-blue-400 flex-grow justify-center absolute w-full">
    <a href="#">Home</a>
    <a href="#">Search</a>
  </nav>
  <div>
    Profile
  </div>
</header>

Upvotes: 2

Related Questions