How to center a textarea using CSS?

HtmlCssTextareaCenter

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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>

Solution 2 - Html

Set text-align of the element's parent to center, like this:

HTML:

<div>
    <textarea></textarea>
<div>

CSS:

div { text-align: center; }

Here is an example: http://jsfiddle.net/ujzLt/

Solution 3 - Html

add display: block; to your textarea styles

Solution 4 - Html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>#container {width:100%; text-align:center;}</style>
</head>

<body>
<div id="container">
<textarea name="mytextarea" cols="10" rows="10"></textarea>
</div>
</body>
</html>

you wrap your textarea with a div, give it width and then you align it with text-align:center;

Solution 5 - Html

This should do the trick. It works for me here in 2020:

textarea {
    margin: auto;
    display: block;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - HtmlDouglasView Answer on Stackoverflow
Solution 2 - HtmldavehauserView Answer on Stackoverflow
Solution 3 - HtmlJeff AdamsView Answer on Stackoverflow
Solution 4 - HtmlSotirisView Answer on Stackoverflow
Solution 5 - HtmlCaptainGenesisXView Answer on Stackoverflow