如何使用CSS将文本区域居中? [英] How to center a textarea using CSS?

查看:123
本文介绍了如何使用CSS将文本区域居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我问这个简单的问题,我是HTML和CSS的新手。
有没有简单的方法可以使文本区域居中?我想我只是尝试使用

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

但是它(显然吗?)不起作用。

解决方案

边距不会影响文本区域,因为它不是块级元素,但是您可以根据需要将其显示为块

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



默认情况下,文本区域显示为显示:内联,这就是为什么您可以轻松地将它们并排放置的原因,以及 text-align:center 答案也起作用的原因。



通过将文本区域放在 flexbox中,也可以将文本区域居中这样的容器

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

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


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.

解决方案

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;
}

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>

这篇关于如何使用CSS将文本区域居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆