I have text with a background image of a fixed width and height. I want the text to be a fixed number of lines (3). The text wraps differently in different browsers, sometimes inserting an extra line which makes the text go over the boundary of the background image. This means I have to add/remove words from my text and continually check the rendering in each browser to see that it is the same number of lines.
I am guessing that either the browsers are rounding the width/margin/padding so that the width of the container is different, or that the text is rendered slightly differently in each browser (with more or less width).
What would be the recommended practice for this? I want the text in the container to be no more than 3 lines long, so I am just as well to fix the height of the container.
The recommended practice is not do something like this. Try to redesign so that the background image isn’t/doesn’t need to be of fixed height and the text doesn’t need to be limited to exactly three lines. If this is some kind of requirement imposed on you by your boss/customer, you’ll need to talk with them and explain, that this isn’t something one can or should do in HTML/CSS.
Keep in mind, that not only the width of the characters will be different in each browser resulting in different wrapping, but browsers will display the text lines in different heights, too, so it can be that your element can’t contain three lines.
If you have no other choice, consider using
overflow: hiddenon the fixed-size element, however this will most likely lead to cut off letters and/or words.EDIT re comment:
I don’t think the available space would variate between browsers. If you were using relative units (
em,%), then maybe there would be rounding differences of one pixel max. However you are using pixels, which should be identical in all current browsers (in Standards Mode).Anyway I’m not saying you should restrict the amount of text you want to display, just give the text some space to “breath” and don’t give the element a fixed height in pixels. Instead:
line-height(for example1.25) and amin-heightthree times the amount (here3.75em).