CSS卡片按钮hover时background属性过渡不流畅问题求助
问题解决:CSS按钮背景色hover过渡不生效
问题原因
你的.Card-Button元素的transition属性仅声明了对color属性的过渡,未包含background-color,因此背景色变化是瞬间完成的,没有平滑过渡效果。
修复方案
修改.Card-Button的transition属性,将background-color加入过渡列表:
.Card-Button { padding: 10px; width: 100%; font-size: 22px; cursor: pointer; /* 同时指定color和background-color的过渡 */ transition: color 0.5s, background-color 0.5s; }
也可以用更简洁的all统一指定所有可过渡属性的过渡时长(但推荐明确指定属性,避免不必要的性能消耗):
.Card-Button { padding: 10px; width: 100%; font-size: 22px; cursor: pointer; transition: all 0.5s; }
建议使用background-color而非复合属性background,仅针对背景色做过渡更精准。
完整修改后的CSS代码
* { margin: 0px; padding: 0px; font-family: "Roboto Slab"; outline: none; border: 0px; } .Cards { padding: 25px; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; } .Card { position: relative; width: 400px; margin: 15px; display: flex; flex-direction: column; background: #FFFFFF; box-shadow: 0px 0px 15px 5px rgba(0, 0, 0, 0.25); } .Card-Image { width: 100%; height: 225px; } .Card-Content { padding: 20px 20px 0px 20px; background: #BC4749; } .Card-Body { padding: 15px; background: #FFFFFF; box-shadow: 0px 0px 15px 5px rgba(0, 0, 0, 0.25); transform: translateY(-40px); } .Card-Title { font-size: 22px; font-weight: bold; } .Card-Description { font-size: 16px; margin-top: 10px; } .Card-List { font-size: 16px; list-style-type: square; margin-top: 10px; margin-left: 15px; } .Card-Button-Space { display: flex; justify-content: center; align-items: center; transform: translateY(-20px); } .Card-Button { padding: 10px; width: 100%; font-size: 22px; cursor: pointer; transition: color 0.5s, background-color 0.5s; } .Card-Button:hover { background-color: #000000; color: #FFFFFF; }
说明
CSS过渡效果仅对transition属性中明确声明的属性生效,未声明的属性状态变化会直接触发瞬间切换。之前的代码只针对文字颜色做了过渡配置,因此背景色没有平滑动画。
内容的提问来源于stack exchange,提问作者user19934333
相关产品推荐
相关产品推荐

