Fill element from center on hover

后端 未结 3 2284
自闭症患者
自闭症患者 2020-12-28 09:36

How can I create a button so that on hover the background colour fills the element from center to left and right of the element.

Example :

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 09:55

    To fill an element with a solid color from center on hover, you can use a pseudo element and CSS3 transitions.
    In the following example, the background is made with a pseudo element and scaled from 0 to 1 horizontaly on hover:

    div {
      position: relative;
      display: inline-block;
      padding: 15px 70px;
      border: 5px solid #B17461;
      color: #B17461;
      font-size: 30px;
      font-family: arial;
      -webkit-transition: color .5s;
              transition: color .5s;
    }
    div:before {
      content: '';
      position: absolute;
      top: 0; left: 0;
      width: 100%; height: 100%;
      background: #B17461;
      z-index: -1;
      -webkit-transform:scaleX(0);
          -ms-transform:scaleX(0);
              transform:scaleX(0);
      -webkit-transition: -webkit-transform .5s;
              transition:         transform .5s;
    }
    div:hover {
      color: #fff;
    }
    div:hover:before {
      -webkit-transform: scaleX(1);
          -ms-transform: scaleX(1);
              transform: scaleX(1);
    }
    NEXT

提交回复
热议问题