How to center a textarea using CSS?

Chris picture Chris · Aug 27, 2010 · Viewed 91.9k times · Source

Forgive me for asking such a simple question, I'm new to both HTML and CSS. Is there an easy way to center a textarea? I figured I'd just try using

textarea{
    margin-left: auto;
    margin-right: auto;
}

but it (obviously?) didn't work.

Answer

Douglas picture Douglas · Aug 27, 2010

The margins won't affect the textarea because it is not a block level element, but you can make it display block if you like:

textarea {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

before and after

By default, textareas are display: inline, which is why you can put them side-by-side easily, and why the text-align: center answers work too.

The textarea can also be centered by putting it inside a flexbox container like this:

<style>
    div.justified {
        display: flex;
        justify-content: center;
    }
</style>

<div class="justified">
    <textarea>Textarea</textarea>
</div>