Dear CSS Experts,
I want to make a textarea fill all the available width. This is
something I've done before but mostly by trial and error; now I want to
understand what is going on. So I try:
textarea {
margin-left: 0;
margin-right: 0;
width: auto;
}
But it doesn't work, I get something narrow. (All testing with Firefox
1.0. I'll worry about issues with other browsers later.) Of course,
if I do
width: 100%;
it seems to work, but actually because the 100% is the content width
and there are a few pixels of borders present it is overflowing
slightly. You find a lot of sites with width: 95% or similar for this
reason.
But why doesn't the original style work? It seems that mozilla
implements a textarea as approximately display:inline-block where
width:auto means shrink-wrap (CSS2.1). OK, so let's change it:
textarea {
display: block;
margin-left: 0;
margin-right: 0;
width: auto;
}
But this doesn't work either. I was hoping that explicitly giving it
display:block would change the meaning of width:auto, but it doesn't.
So is this a Mozilla bug ("meaning of width:auto does not change when
textarea has display:block")? Am I misunderstanding something? Is
there a better solution? Currently I think that the best method is to
put an extra div around it in the markup:
<div class="textareactr">
<textarea/>
</div>
div.textareactr textarea {
width:100%;
}
(Previous experience also suggests that this is the best way to avoid
some of IE's weirdnesses, but I'm not worrying about them right now.)
Any thoughts?
--Phil.