按钮hover/active状态下,同步修改SVG图标与文本颜色的问题
解决方案:按钮Hover/Active时同步修改SVG图标与文本颜色
问题根源
你当前的SVG样式是监听自身的hover/active状态,这会导致按钮的文本和SVG状态不同步(比如hover按钮文本时,SVG不会触发样式变化)。正确的做法是通过父按钮的状态来控制子元素SVG的样式。
修改步骤与代码
1. 修正SVG的状态选择器
将SVG的hover/active样式改为基于父按钮的嵌套选择器,确保按钮任何部分被交互时,SVG同步变化:
.fa-brands--whatsapp-square { display: inline-block; width: 2.63rem; height: 3rem; background-repeat: no-repeat; background-size: 100% 100%; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='green' d='M224 122.8c-72.7 0-131.8 59.1-131.9 131.8c0 24.9 7 49.2 20.2 70.1l3.1 5l-13.3 48.6l49.9-13.1l4.8 2.9c20.2 12 43.4 18.4 67.1 18.4h.1c72.6 0 133.3-59.1 133.3-131.8c0-35.2-15.2-68.3-40.1-93.2c-25-25-58-38.7-93.2-38.7m77.5 188.4c-3.3 9.3-19.1 17.7-26.7 18.8c-12.6 1.9-22.4.9-47.5-9.9c-39.7-17.2-65.7-57.2-67.7-59.8s-16.2-21.5-16.2-41s10.2-29.1 13.9-33.1c3.6-4 7.9-5 10.6-5c2.6 0 5.3 0 7.6.1c2.4.1 5.7-.9 8.9 6.8c3.3 7.9 11.2 27.4 12.2 29.4s1.7 4.3.3 6.9c-7.6 15.2-15.7 14.6-11.6 21.6c15.3 26.3 30.6 35.4 53.9 47.1c4 2 6.3 1.7 8.6-1c2.3-2.6 9.9-11.6 12.5-15.5c2.6-4 5.3-3.3 8.9-2s23.1 10.9 27.1 12.9s6.6 3 7.6 4.6c.9 1.9.9 9.9-2.4 19.1M400 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48M223.9 413.2c-26.6 0-52.7-6.7-75.8-19.3L64 416l22.5-82.2c-13.9-24-21.2-51.3-21.2-79.3C65.4 167.1 136.5 96 223.9 96c42.4 0 82.2 16.5 112.2 46.5c29.9 30 47.9 69.8 47.9 112.2c0 87.4-72.7 158.5-160.1 158.5'/%3E%3C/svg%3E"); } /* 基于父按钮的状态触发SVG样式变化,删除原有的.fa-brands--whatsapp-square:hover/active */ #nosotros .btn:hover .fa-brands--whatsapp-square, #nosotros .btn:active .fa-brands--whatsapp-square { /* 仅修改背景图,其他属性继承基础样式,减少冗余 */ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 448 512'%3E%3Cpath fill='white' d='M224 122.8c-72.7 0-131.8 59.1-131.9 131.8c0 24.9 7 49.2 20.2 70.1l3.1 5l-13.3 48.6l49.9-13.1l4.8 2.9c20.2 12 43.4 18.4 67.1 18.4h.1c72.6 0 133.3-59.1 133.3-131.8c0-35.2-15.2-68.3-40.1-93.2c-25-25-58-38.7-93.2-38.7m77.5 188.4c-3.3 9.3-19.1 17.7-26.7 18.8c-12.6 1.9-22.4.9-47.5-9.9c-39.7-17.2-65.7-57.2-67.7-59.8s-16.2-21.5-16.2-41s10.2-29.1 13.9-33.1c3.6-4 7.9-5 10.6-5c2.6 0 5.3 0 7.6.1c2.4.1 5.7-.9 8.9 6.8c3.3 7.9 11.2 27.4 12.2 29.4s1.7 4.3.3 6.9c-7.6 15.2-15.7 14.6-11.6 21.6c15.3 26.3 30.6 35.4 53.9 47.1c4 2 6.3 1.7 8.6-1c2.3-2.6 9.9-11.6 12.5-15.5c2.6-4 5.3-3.3 8.9-2s23.1 10.9 27.1 12.9s6.6 3 7.6 4.6c.9 1.9.9 9.9-2.4 19.1M400 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48M223.9 413.2c-26.6 0-52.7-6.7-75.8-19.3L64 416l22.5-82.2c-13.9-24-21.2-51.3-21.2-79.3C65.4 167.1 136.5 96 223.9 96c42.4 0 82.2 16.5 112.2 46.5c29.9 30 47.9 69.8 47.9 112.2c0 87.4-72.7 158.5-160.1 158.5'/%3E%3C/svg%3E"); }
2. 修复按钮Active状态的语法错误
原按钮active选择器存在语法错误,修正后确保按钮激活时文本颜色正常变化:
#nosotros .btn, .filter-button { font-size: 18px; border: 1px solid #FF0000; border-radius: 5px; text-align: center; color: #FF0000; margin-bottom: 30px; } #nosotros .btn:hover, .filter-button:hover { font-size: 18px; border: 1px solid #FFF; border-radius: 5px; text-align: center; color: #ffffff; background-color: #FF0000; } /* 修正选择器语法,确保每个元素的active状态独立生效 */ #nosotros .btn:active, .btn-default:active, .filter-button:active { background-color: #FFA500; color: white; }
优化建议(可选)
如果后续需要更灵活地修改SVG颜色,建议将SVG改为内联元素(直接写入HTML),这样可以通过fill: currentColor让SVG继承父元素的文本颜色,无需替换整个背景图,减少代码冗余。
内容的提问来源于stack exchange,提问作者Jerardo
相关产品推荐
相关产品推荐

