Jquery width () seems wrong in every browser I try except FF4

I am trying to get the width of the <td> element exactly. The results are what I expect in FF4, but in IE9 / Safari 5, the result I get is 1 pixel. It drove me crazy.

UPDATE:

My ultimate goal is to take the width from the cells of the ROWS table and apply it to the cells of the HEADER table so that each cell in the table is perfectly aligned. Doing this for all browsers is my problem.

Here is a simple example: http://jsfiddle.net/XwNEj/16/

It works only in FF4 :( I would really appreciate any help in working on all modern browsers.

+6
source share
3 answers

For fixed table headers see this post or try this for a clean css solution.

+1
source

I would not say that this is wrong, moreover, the table cells do not have the same box model as a div. You can see this by playing with the following:

  • Use .outerWidth() and compare with .width()
  • Add display:block; into your CSS for th,td and compare the width with the default rendering of the table cell.

I think you will understand what I mean. This is also described in http://blog.anofsinger.com/2008/03/table-cell-box-model.html .

If you look at the example http://codepen.io/morewry/pen/pqGhv , here I intend to show that the inner width of the table cell will not be the same as the inner width of a div , even if both are given width:200px .

However, although this may not be the width you expect, and the width will be potentially different in each browser, the cells should be consistent in each browser.

(At least this is what I expect.) I would expect this:

  • You set the width of, say, 200 pixels to a table cell.
  • Firefox can return a width of 199 and IE a width of 201. These widths are accurate and reflect each cell model of a cell in a browser table.
  • If you set the same width to th , it will be displayed with the same width as td if it has the same width and fill width.

But, looking at the example again today, I saw that either automatically setting the same width for each th as the corresponding td with JavaScript or setting a wide equal width with both CSS and th sometimes ended up in the same browser other width from td . (I have some vague thoughts about this, but suffice it to say that it is really interesting when this mismatch appears [and disappears]. I compared it by setting width vs a min-width vs, giving width:100% whole table , etc.)

I believe your goal is a fixed table header. To do this, I feel that it is much better to do what Mike showed and keep it all in one table using thead and tbody. You break down the "semantics" with two different tables - th no longer related to td , except visually.

  • However, you can use JavaScript something like this (it works in all my browsers at least and could be better than detecting the browser): http://codepen.io/morewry/pen/toCqs
  • Using markup / CSS like this also fixed the problem for me http://codepen.io/morewry/pen/gjELD without JavaScript. It is uncertain if this works everywhere (I don't have IE9 for one), or if it can be used in the context you need.
+1
source

Well, it doesn't seem to be able to fix this otherwise than doing a browser check:

Adding 1px to the width of browsers other than mozilla seems to work. if (! $. browser.mozilla) {w ++; }

I'm not crazy about fixing it, but haven’t seen anything yet. We hope that the browsers agree on how to quickly measure the table cell.

Even better, why CSS does not support the concept of a fixed header table?!?!

0
source

Source: https://habr.com/ru/post/887242/


All Articles