如何将导航栏向右移动并应用背景色?

How to move navbar to right and apply background color?

我正在使用 react + tailwind 制作一个简单的应用程序,我目前正在处理导航栏部分。

Navbar.js:

import React from "react";

export default function Navbar({ fixed }) {
  const [navbarOpen, setNavbarOpen] = React.useState(false);
  return (
    <>
      <nav className="relative flex flex-wrap items-center justify-between px-2 py-3 bg-pink-500 mb-3">
        <div className="container px-4 mx-auto flex flex-wrap items-center justify-between">
          <div className="w-full relative flex justify-between lg:w-auto lg:static lg:block lg:justify-start">
            <a
              className="text-sm font-bold leading-relaxed inline-block mr-4 py-2 whitespace-nowrap uppercase text-white"
              href="#pablo"
            >
              Logo 1
            </a>
            <button
              className="text-white cursor-pointer text-xl leading-none px-3 py-1 border border-solid border-transparent rounded bg-transparent block lg:hidden outline-none focus:outline-none"
              type="button"
              onClick={() => setNavbarOpen(!navbarOpen)}
            >
              <i className="fas fa-bars"></i>
            </button>
          </div>
          <div
            className={
              "lg:flex flex-grow items-center" +
              (navbarOpen ? " flex" : " hidden")
            }
            id="example-navbar-danger"
            style={{ flexDirection: "column" }} 
          >
            <ul className="flex flex-col lg:flex-row list-none">
              <li className="nav-item">
                <a className="px-3 py-2 flex items-center text-xs uppercase font-bold leading-snug text-white hover:opacity-75">
                  Row 1 - Menu 1
                </a>
              </li>
              <li className="nav-item">
                <a className="px-3 py-2 flex items-center text-xs uppercase font-bold leading-snug text-white hover:opacity-75">
                  Row 1 - Menu 2
                </a>
              </li>
              <li className="nav-item">
                <a className="px-3 py-2 flex items-center text-xs uppercase font-bold leading-snug text-white hover:opacity-75">
                  Row 1 - Menu 3
                </a>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    </>
  );
}

并在此处完成工作示例:

https://codesandbox.io/s/tailwind-css-and-react-forked-1y10y

问题和尝试过的事情:

要求:

要求是在响应式视图中,整个布局需要完全像这样,

|  -- Logo --  This bar needs to have bg-pink  -- Hamburger Icon -- |
|                                                                   |
                                   |     -- Row 1 - Menu 1 --       |
                                   |     -- Row 1 - Menu 2 --       | --> Only this area have bg-pink
                                   |     -- Row 1 - Menu 3 --       |
                                   |     -- Row 2 - Menu 1 --       |
                                   

Please click on this image to see the similar expectation

请帮助我实现 bg-pink 仅顶部导航栏和右侧菜单项栏的颜色。

当前UI可以在这个link中查看:https://1y10y.csb.app/(请切换到响应模式)

您可以通过将 position 用作 div 上的 fixed 来实现此目的,后者将用作侧边栏。但对于其他大屏幕,您需要删除位置 属性,它将显示在导航栏的中间。

试试下面的方法:-

import React from "react";

export default function Navbar({ fixed }) {
  const [navbarOpen, setNavbarOpen] = React.useState(false);
  return (
    <>
      <nav className="relative flex flex-wrap items-center justify-between px-2 py-3 bg-pink-500 mb-3">
        <div
          className="container px-4 mx-auto flex flex-wrap items-center justify-between"
          style={{ position: "relative" }}
        >
          <div className="w-full relative flex justify-between lg:w-auto lg:static lg:block lg:justify-start">
            <a
              className="text-sm font-bold leading-relaxed inline-block mr-4 py-2 whitespace-nowrap uppercase text-white"
              href="#pablo"
            >
              Logo 1
            </a>
            <button
              className="text-white cursor-pointer text-xl leading-none px-3 py-1 border border-solid border-transparent rounded bg-transparent block lg:hidden outline-none focus:outline-none"
              type="button"
              onClick={() => setNavbarOpen(!navbarOpen)}
            >
              <i className="fas fa-bars"></i>
            </button>
          </div>
          <div
            className={
              "lg:flex flex-grow items-center" +
              (navbarOpen ? " flex" : " hidden")
            }
            id="example-navbar-danger"
            style={{
              flexDirection: "column",
              position: "fixed",
              right: 0,
              top: "41px",
              zIndex: 99999,
              background: "#ed64a6",
              height: "100%",
              minWidth: "200px"
            }} // added flex direction column because I will have another navbar below this.
          >
            <ul className="flex flex-col lg:flex-row list-none">
              <li className="nav-item">
                <a className="px-3 py-2 flex items-center text-xs uppercase font-bold leading-snug text-white hover:opacity-75">
                  Row 1 - Menu 1
                </a>
              </li>
              <li className="nav-item">
                <a className="px-3 py-2 flex items-center text-xs uppercase font-bold leading-snug text-white hover:opacity-75">
                  Row 1 - Menu 2
                </a>
              </li>
              <li className="nav-item">
                <a className="px-3 py-2 flex items-center text-xs uppercase font-bold leading-snug text-white hover:opacity-75">
                  Row 1 - Menu 3
                </a>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    </>
  );
}