Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:30:44+00:00 2026-05-27T09:30:44+00:00

I’m not sure if this is possible, but I thought it would be cool

  • 0

I’m not sure if this is possible, but I thought it would be cool using CSS transforms to create an effect where a div expands from its center to a predetermined height and width, rather than just from the upper left corner.

For instance, if I have (demo)

<div id="square"></div>

and (vendor prefixes left out for brevity)

#square {
    width: 10px;
    height: 10px;
    background: blue;
    transition: width 1s, height 1s;
}
#square:hover {
    width: 100px;
    height: 100px;
}

On hover, this will expand the square to the right and down to 100px. I would like to have it expand from the middle.

I’m aware that I could probably use transform: scale(x) (demo), but this doesn’t really provide me with a very “pixel perfect” layout (as it is percentage-based), and it also does not affect the surrounding layout (make other elements that are in the document flow adjust to its resizing). This is essentially what I want to do, except with the document flow affected accordingly.

Does anyone know of a way to do this without javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:30:45+00:00Added an answer on May 27, 2026 at 9:30 am

    The key is to transition the margin by a formula. There is a little “wiggle” that is annoying during the transition if it is floated.

    EDITED ADDING OPTIONS

    Option 1: Expands within space reserved around it http://jsfiddle.net/xcWge/14/:

    #square {
        width: 10px;
        height: 10px;
        margin: 100px; /*for centering purposes*/
        -webkit-transition: width 1s, height 1s, margin 1s;
        -moz-transition: width 1s, height 1s, margin 1s;
        -ms-transition: width 1s, height 1s, margin 1s;
        transition: width 1s, height 1s, margin 1s;
    }
    #square:hover {
        width: 100px;
        height: 100px;
        margin: 55px; /* initial margin - (width change (and/or height change)/2), so here 100px is initial margin, and the change is (100px final W/H - 10px initial W/H = 90px change, so 100px - (90px / 2 [= 45px]) = 55px) */
    }
    

    Option 2: Expands over elements around it http://jsfiddle.net/xcWge/18/:

    #square {
        width: 10px;
        height: 10px;
        margin: 0; /*for centering purposes*/
        -webkit-transition: width 1s, height 1s, margin 1s;
        -moz-transition: width 1s, height 1s, margin 1s;
        -ms-transition: width 1s, height 1s, margin 1s;
        transition: width 1s, height 1s, margin 1s;
    }
    #square:hover {
        width: 110px;
        height: 110px;
        margin: -50px; /* 0 - (110px - 10px [= 100px]) / 2 =  -50px */
    }
    

    Option 3: Expands over elements before it in flow and shifts elements after it http://jsfiddle.net/xcWge/22/:

    #square {
        width: 10px;
        height: 10px;
        margin: 0;
        position: relative;
        top: 0;
        left: 0;
        -webkit-transition: width 1s, height 1s, top 1s, left 1s, margin 1s;
        -moz-transition: width 1s, height 1s, top 1s, left 1s, margin 1s;
        -ms-transition: width 1s, height 1s, top 1s, left 1s, margin 1s ;
        transition: width 1s, height 1s, top 1s, left 1s, margin 1s;
    }
    #square:hover {
        width: 110px;
        height: 110px;
        top: -50px; /* initial top[0] - (new height[110px] - initial height[10px] [=100px])/2 [=50px] = -50px) */
        left: -50px; /* initial left[0] - (new width[110px] - initial width[10px] [=100px])/2 [=50px] = -50px) */
        margin-right: -50px;
        margin-bottom: -50px;
    }
    

    ADDED NON-SQUARE EXAMPLE

    A comment was made this does not work for a non-square (same width/height), but that just means one has to adjust differently for each direction during the transition. So here is Option 2 (with non-square) that starts as a rectangle, and the width expands twice as much as the height (so changes rectangle shape even) during the transition: Expands over elements around it http://jsfiddle.net/xcWge/2131/

    #rectangle {
        width: 110px;
        height: 10px;
        margin: 0; /*for centering purposes*/
        -webkit-transition: width 1s, height 1s, margin 1s;
        -moz-transition: width 1s, height 1s, margin 1s;
        -ms-transition: width 1s, height 1s, margin 1s;
        transition: width 1s, height 1s, margin 1s;
    }
    #rectangle:hover {
        width: 310px;
        height: 110px;
        margin: -50px -100px; /* initial margin - ((initial margin - width change (or height change))/2) */
    }
    

    If the width were only changing by 100px also (so from 110px to 210px), then just a margin: -50px would have still worked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.