Please have a look at this fiddle - http://jsfiddle.net/Z27hC/
var container = document.createElement('span');
container.style.display = 'inline-block';
container.style.marginTop = '10px';
container.style.marginLeft = '50px';
container.style.background = 'lightblue';
document.body.appendChild(container);
var cell = document.createElement('span');
cell.style.display = 'inline-block';
cell.style.border = ' 2px solid black';
cell.style.width = '200px';
cell.style.height = '16px';
cell.style.overflow = 'hidden';
container.appendChild(cell);
var text_node = document.createTextNode('hallo');
cell.appendChild(text_node);
I have a container, containing a cell, containing a text node. The cell has a fixed width.
If the text passed to the text node exceeds the width, I want it to be truncated, so I set the cell to 'overflow: hidden'.
It works, but it causes the height of the cell to increase by 3px. The cell has a border, but the increased height appears below the border, not inside it.
As I have many cells in a spreadsheet style, it messes up the layout.
I have tested this on IE8 and Chrome, with the same result.
Any of the following solutions would be ok -
- prevent the increased height from occurring
- keep the increased height inside the border
- another way to truncate the text
As requested, here is a new fiddle that shows a more complete example.
http://jsfiddle.net/frankmillman/fA3wy/
I hope it is self-explanatory. Let me know if you need anything explained in more detail.
Here is (hopefully) my final fiddle -
http://jsfiddle.net/frankmillman/RZQQ8/
It incorporates ideas from all responders, and it now works as I want.
There are two main changes.
The first one was inspired by Mathias' table solution. Instead of an intermediate container, containing a blank row and a data row, one of which was hidden and one shown, I now just have alternating blank and data rows in the top-level container. I don't think it affected my layout problem, but it cut out a layer and simplified the code.
The second change, suggested by all the responders, actually fixed the problem. Instead of having elements of type 'span' with display 'inline-block', I now use 'div', and a careful mix of 'float left' and 'float right' to achieve the layout I want.
Many thanks to all.