为什么Tailwindcss的“mb-3”类不支持next/link?

rqmkfv5c  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(98)

在这个Next.js组件中,在高亮显示的行中,Tailwindcss的“mb-3”类在Y方向上没有效果,但如果我们使用“m-3”,它在Y方向上也没有效果,但在X方向上有效果。

import Image from "next/image";
import Link from "next/link";

const Header = () => {
    return (
        <div className="bg-blue-200 w-1/5 h-screen fixed px-12 py-9">
            <Link href="/" className="mb-3">
                <Image
                    src="/logo.png"
                    alt="Twizlr"
                    width={100}
                    height={0}
                    priority
                    className="h-auto"
                />
            </Link>

            <nav className="flex items-center justify-center h-full bg-yellow-200"></nav>
        </div>
    );
};

export default Header;

字符串
我不明白为什么会这样?
我不知道该怎么办。

jaxagkaj

jaxagkaj1#

这里的<Link>将呈现<a>元素。<a>元素默认为内联元素。这意味着垂直margin不会在它们上显示任何行为。要使垂直margin工作,请考虑覆盖它们的默认值display: inline。例如,您可以将display: inline-block应用为display: inline的近似等效值。

const Image = props => <img {...props}/>
const Link = props => <a {...props}/>

const Header = () => {
  return (
    <div className="bg-blue-200 w-1/5 h-screen fixed px-12 py-9">
      <Link href="/" className="mb-3 inline-block">
        <Image
            src="https://picsum.photos/100/100"
            alt="Twizlr"
            width={100}
            height={0}
            priority
            className="h-auto"
        />
      </Link>
      <nav className="flex items-center justify-center h-full bg-yellow-200"></nav>
    </div>
  );
};

ReactDOM.createRoot(document.getElementById('app')).render(<Header/>);

个字符

gfttwv5a

gfttwv5a2#

如果我也将父div设置为display: flex,就可以了。谢谢!

import Image from "next/image";
import Link from "next/link";

const Header = () => {
    return (
        <div className="flex bg-blue-200 w-1/5 h-screen fixed px-12 py-9">
            <Link href="/" className="mb-3">
                <Image
                    src="/logo.png"
                    alt="Twizlr"
                    width={100}
                    height={0}
                    priority
                    className="h-auto"
                />
            </Link>

            <nav className="flex items-center justify-center h-full bg-yellow-200"></nav>
        </div>
    );
};

export default Header;

字符串

相关问题