用 transition 实现 hover 宽高平滑变化,需设明确初始和 hover 数值(如 width: 200px→280px),禁用 auto/fit-content;推荐单独声明 transition: width 0.3s ease, height 0.3s ease;避免 all;配合 overflow: hidden 和 min-width/min-height 防卡顿;更优方案是用 transform: scale() 替代,性能更好。
用 transition 实现 hover 时宽高平滑变化,关键在于正确设置过渡属性和初始状态,避免突兀跳变。
CSS 中只有可计算的、有明确数值的 width 和 height 才能过渡。不能对 auto、fit-content 或无显式定义的尺寸做过渡。
width: 200px; height: 40px;
width: 280px; height: 60px;
width: auto; 或 height: 100%;(除非父容器高度固定且可继承)
ion 属性推荐单独声明要过渡的属性,而不是用 all,防止意外触发其他样式变化:
transition: width 0.3s ease, height 0.3s ease;transition: width 0.3s ease, height 0.3s ease;
transition: all 0.3s ease;(可能让 color、opacity 等也参与过渡,干扰预期效果)如果元素内是文字或图片,且未设宽高,浏览器渲染时可能先按内容撑开再过渡,造成视觉卡顿。
overflow: hidden; 防止内容溢出影响布局感知min-width/min-height 设下限,配合 transition 更稳定transform: scale() 替代宽高变化(性能更好、支持 auto 尺寸)如果目标是“看起来变大”,transform: scale() 更高效,且无需预设尺寸:
transform: scale(1);
transform: scale(1.2);
transition: transform 0.3s ease;