We help IT Professionals succeed at work.

Obtain table width after the fact.

jglazer63 asked
Last Modified: 2012-08-13
In javascript, how can I determine the width of a table that is sized based on its contents.  That is, there is no width= specification on the table, it just gets wide enough depending on the content within it.  I need to know how wide it ended up being.

Watch Question

 var w = document.getElementById("myTable").width;
should give the width of the table. provided you defined the table as

<table id="mytable" ...>

Checkout  http://msdn2.microsoft.com/en-us/library/ms535901.aspx
for more info on all the attributes available


Nope.  Width comes back as ""  (blank).  Already thought of that.

You can use the table's offsetWidth property.  All you have to do is get a reference to that table, and you can get it back out.  For example:

<script type="text/javascript">
function getWidth(obj)
<style type="text/css">
#theTable {
  border-collapse: collapse;

#theTable td {
  border: 1px solid black;

<table id="theTable">

<input type="button" value="Get table width" onclick="getWidth(document.getElementById('theTable'));" />

I used getElementById(), but just about anything will work there.  So long as you pass in the table, the getWidth() function would give you that object's width.

I used a little bit of styling to make the table more visible, but you can safely ignore it.  It doesn't do/add anything to the snippet.  Hope that helps.


Is the CSS necessary? I did offsetWidth earlier and it comes back as zero (0) even though the content is there and the table is obviously wider than zero.
Unlock this solution and get a sample of our free trial.
(No credit card required)

My fauilt .. It shud have been offsetWidth

From MSDN:
 offsetWidth : Retrieves the width of the object relative to the layout or coordinate parent, as specified by the offsetParent property
Forced accept.

EE Admin

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.