Change width of a DIV along central vertical axis

Posted on 2008-01-30
Medium Priority
Last Modified: 2013-11-19
I have a container with a bunch of absolutely positioned DIV elements, and I am working on a script that creates a shrinking animation effect using setInterval.  During each interval, a method is called that decreases the width of the DIV by 1, until it reaches 0, then does the opposite until it reaches its original size.

The problem is that I cannot seem to get the DIVs to shrink to the middle.  They will only shrink to the left.  Here is the relevant setInterval code that shrinks the DIV (the code to grow it back again is the same with the sign changed):

          var cur_w = parseInt( block.style.width );
          cur_w -= 10;
          if( cur_w < 0 )
           cur_w = 0;
          block.style.width = cur_w + "px";

and here is the css stuff for both the container and the DIVs themselves.


  margin-left: auto;
  margin-right: auto;



The DIVs are set up in a table-less way, so they need to be absolute.  How do I get them aligned in such a way as to have them shrink along the center axis as opposed to the left side axis????

Thanks in advance...

Question by:wmethlie
LVL 16

Accepted Solution

LeeKowalkowski earned 750 total points
ID: 20775694
A more complete example would help me better.

For now, it seems your blocks are absolutely positioned, you'd have to increment the style.left by half your width decrement to do that (although incrementing left by .5 would not work if you're passing it through parseInt() on the next cycle).

If your blocks are centered, they just need margin:auto instead.


Author Closing Comment

ID: 31426306
thanks for the help.  there really is no way to center an absolutely positioned element along its y-axis.  In Flash you can set the axis to any point, and my goal with this project was to do as much as I could without Flash, but obviously there are limitations.  Your solution worked, but is too "expensive" to be practical in most circumstances, so I'm going back to the drawing board.

Thanks again.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question