
垂直居中失效?tailwindcss中的line-height
在tailwindcss中,可以使用line-height-*或leading-*类来设置行高。然而,有时这些类可能无法正常工作。
原因
<nav class="w-full nav h-12">
<div class="container mx-auto flex">
<div class="w-24 leading-6 text-center h-12 hover:bg-black">首页</div>
<p class="w-24 leading-6 text-center h-12 hover:bg-black">首页</p>
<span class="w-24 leading-6 text-center h-12 hover:bg-black">首页</span>
</div>
</nav>在这种情况下,leading-6未能有效,原因在于元素的实际高度(3rem)大于line-height: 1.5rem。
立即学习“前端免费学习笔记(深入)”;
解决办法
有两种方法可以解决这个问题:
<nav class="nav h-10 w-full">
<div class="container mx-auto flex">
<div class="h-10 w-24 text-center leading-10 hover:bg-black hover:text-white">首页</div>
<p class="h-10 w-24 text-center leading-10 hover:bg-black hover:text-white">首页</p>
<span class="h-10 w-24 text-center leading-10 hover:bg-black hover:text-white">首页</span>
</div>
</nav><nav class="nav h-12 w-full">
<div class="container mx-auto flex">
<div class="flex h-12 w-24 items-center justify-center hover:bg-black hover:text-white">首页</div>
<p class="flex h-12 w-24 items-center justify-center hover:bg-black hover:text-white">首页</p>
<span class="flex h-12 w-24 items-center justify-center hover:bg-black hover:text-white">首页</span>
</div>
</nav>其他提示
以上就是TailwindCSS中line-height失效的原因是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号