I'm trying to make a table with 2 cells in a row with the second one containing a 1-row textarea so that the text is aligned vertically to the same level as the text in the first cell:
I've noticed that the CSS that worked well enough for FF fails in Chrome (actually, it also depends on the browser zoom level) and I need a consistent way to implement this.
To give an MCVE, I've created this html:
td, td textarea {
font-family: serif;
font-size: 15px;
}
table, tr, td, textarea {
border: none;
}
td {
padding-left: 0.5em;
padding-right: 0.5em;
}
<table><tbody><tr>
<td>text</td>
<td><textarea>editable text</textarea></td>
</tr></tbody></table>
<script>
// in the real case, this code is inside a handler of the input event
// which is meant to adjust height when the number of lines is changed
var txt = document.querySelector('textarea');
txt.style.height = '1px';
txt.style.height = txt.scrollHeight+'px';
</script>
As you can see, it doesn't quite work as desired but there's also not that much CSS so various paddings are browser-dependent. Now, here's what I can think of to "fix" this: specify vertical padding and vertical-align
for cells, remove padding-top
for textarea.. And the following snippet works well for me in a separate html:
td, td textarea {
font-family: serif;
font-size: 15px;
}
table, tr, td, textarea {
border: none;
}
td {
padding: 0 0.5em;
vertical-align: middle;
}
textarea {
padding: 0;
}
<table><tbody><tr>
<td>text</td>
<td><textarea>editable text</textarea></td>
</tr></tbody></table>
<script>
// in the real case, this code is inside a handler of the input event
// which is meant to adjust height when the number of lines is changed
var txt = document.querySelector('textarea');
txt.style.height = '1px';
txt.style.height = txt.scrollHeight+'px';
</script>
..but it doesn't work in my initial context and also it doesn't work inside SO snippet! Right, setting display: block
for textarea
fixes this in SO snippet:
td, td textarea {
font-family: serif;
font-size: 15px;
}
table, tr, td, textarea {
border: none;
}
td {
padding: 0 0.5em;
vertical-align: middle;
}
textarea {
padding: 0;
display: block;
}
<table><tbody><tr>
<td>text</td>
<td><textarea>editable text</textarea></td>
</tr></tbody></table>
<script>
// in the real case, this code is inside a handler of the input event
// which is meant to adjust height when the number of lines is changed
var txt = document.querySelector('textarea');
txt.style.height = '1px';
txt.style.height = txt.scrollHeight+'px';
</script>
But still! This gives perfect alignment here at SO (and vertical "padding" is same on top and on bottom):
but in my original context it is not that good:
I'm puzzled, what else can I be missing?