You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tailwind CSS:React社交图标单个Hover效果异常修复求助

解决方案

问题出在布局结构和样式作用逻辑上,调整后的代码如下:

import { FaLinkedin, FaGithub } from "react-icons/fa";

const SocialLinks = () => {
  return (
    <div className="fixed top-1/2 right-0 mt-48">
      <ul className="flex flex-col gap-2">
        <li className="hover:mr-4 transition-all duration-300">
          <a
            className="flex items-center justify-center h-10 w-10 rounded-full bg-blue-800 text-white"
            href="https://www.linkedin.com/in"
            target="_blank"
            rel="noopener noreferrer"
          >
            <FaLinkedin size={16} />
          </a>
        </li>
        <li className="hover:mr-4 transition-all duration-300">
          <a
            className="flex items-center justify-center h-10 w-10 rounded-full bg-gray-300 text-gray-800"
            href="https://github.com"
            target="_blank"
            rel="noopener noreferrer"
          >
            <FaGithub size={16} />
          </a>
        </li>
      </ul>
    </div>
  );
};

export default SocialLinks;

关键调整说明:

  • 给<ul>添加flex flex-col gap-2:让列表项垂直独立排列,用gap统一控制项间距,替代原有的mr-2,避免margin相互干扰。
  • 移除<li>默认的mr-2:只保留hover:mr-4,确保只有被hover的列表项才会触发右侧间距变化。
  • 添加transition-all duration-300:让hover效果平滑过渡,避免生硬的位置跳转。
  • 修正top-50%为top-1/2:Tailwind没有top-50%类,top-1/2是官方标准写法,实现垂直居中定位。

原代码问题原因:

原<ul>未设置flex布局,列表项默认是块级元素(占满父容器宽度),单个<li>的hover:mr-4会改变父容器宽度,导致另一个列表项被挤压移位,视觉上看起来像是两个都触发了样式。改成flex布局后,每个列表项尺寸独立,hover仅影响当前元素,不会干扰其他项。

内容的提问来源于stack exchange,提问作者kishore naidu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 00:20:57