怎么让背景颜色从左到有慢慢出来?

我想实现鼠标悬浮按钮背景颜色从左到右慢慢出来。
图片描述

html

<div class="btn">
    <button>按钮</button>
</div>

css


.btn button{
        background-color: red;
        width: 200px;
        height: 40px;
        border: none;
        outline: none;
        cursor: pointer;
        color: white;
    }

就是这样的。
谢谢啦。

阅读 3.1k
2 个回答

    .btn button {
      position: relative;
    }

    .btn button::after {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      transition: .3s;
      opacity: .3;
      background: #000;
      transform-origin: left;
      transform: scaleX(0);
    }

    .btn button:hover::after {
      transform: scaleX(1);
    }
<!DOCTYPE html>
<html lang="en">
    <head>
        <style>
            .btn{
                position: relative;
                width: 130px;
                height: 40px;
                line-height: 40px;
                text-align: center;
                color: #fff;
                background: red;
                cursor: pointer;
            }

            .btn:after {
                content: '';
                position: absolute;
                left: 0;
                top: 0;
                width: 0;
                height: 100%;
                transition: .3s;
                opacity: .3;
                background: #c33;
            }

            .btn:hover:after {
                width: 100%;
            }
        </style>
    </head>
    <body>
        <div class="btn">按钮</div>
    </body>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题