How to center a textarea using CSS?
Asked Answered
L

5

60

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.

Linguini answered 26/8, 2010 at 22:29 Comment(0)
O
107

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>
Orange answered 26/8, 2010 at 22:44 Comment(3)
This is what I'm using now, do you know if there's anything bad about it (as it sounds too easy to be true/good ;-) )?Linguini
Nope, I don't know of any downsides.Orange
One downside is when textarea is resized, (by dragging resize-handle), it is no longer adjusted to center. (At least on Chrome).Girdle
S
11

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/

Stromboli answered 26/8, 2010 at 22:34 Comment(5)
Thank you for your quick reply! I have already assigned some colors and other things to the div element though, how do I get one which only has text-align:center;?Linguini
@Linguini -- just add text-align:center; to your current <div>'s css declaration then. :-)Mallorymallow
You can add all styling to the same div (color, font-family, text-align,...) or isn't this what you mean?Stromboli
I actually wanted to remove the styles from the div so they wouldn't apply to the textarea, but perhaps that isn't needed (thought it would form a box with background color and padding, like my div does)?Linguini
They do not apply to the textarea, you can leave them as they are.Stromboli
C
2

add display: block; to your textarea styles

Carchemish answered 26/8, 2010 at 22:42 Comment(1)
Hey that did the trick, combined with the styles I already had, like you said. Thanks!Linguini
B
1
<!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;

Brilliant answered 26/8, 2010 at 22:36 Comment(1)
Thanks a lot :-)! However, like I explained above, I have already made my div prettier, how do I get back to a default one with just the text align?Linguini
J
0

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

textarea {
    margin: auto;
    display: block;
}
Johnettajohnette answered 10/11, 2020 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.