Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing the width of div from both left and right at the same time

Tags:

html

css

how to reduce the width of div from both left and right at the same time when i mouse over the div?

something like this -> example

i don't know why the bottom div is just reduced from left.

    #border-top-category {
        position: absolute;
        border-top: solid 20px rgb(234, 198, 255);
        width: 100px;
        top: 0px;
        right: 10px;
        transition: width 2s;
        }
#border-top-category:hover {
       width: 50px;
       }
          <div id="border-top-category"></div>
like image 801
sina Avatar asked Dec 17 '25 11:12

sina


2 Answers

Instead of animating the width, you could change the scale of the element on hover.
Set the default scaleX to 1 and change it on hover to 0.

Your CSS code could then look like this:

    #border-top-category {
        position: absolute;
        border-top: solid 20px rgb(234, 198, 255);
        width: 100px;
        transform: scaleX(1);
        transform-origin: center;
        top: 0px;
        right: 10px;
        transition: transform 2s;
    }

    #border-top-category:hover {
        transform: scaleX(0);
    }
<div id="border-top-category"></div>
like image 106
flvps Avatar answered Dec 20 '25 02:12

flvps


#border-top-category {
    position: absolute;
    border-top: solid 20px rgb(234, 198, 255);
    width: 100px;
    top: 0px;
    right: 10px;
    transition: transform 2s;
    }
    #border-top-category:hover {
   transform: scaleX(0.5);
   }

Use transition to transform the X axis (scaleX(k) it will make the x axis to k time)

like image 20
Student Avatar answered Dec 20 '25 03:12

Student



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!