问题描述
原谅我问这个简单的问题,我是HTML和CSS的新手。
有没有简单的方法可以使文本区域居中?我想我只是尝试使用
textarea {
margin-left:auto;
margin-right:auto;
}
但是它(显然吗?)不起作用。
边距不会影响文本区域,因为它不是块级元素,但是您可以根据需要将其显示为块
textarea {
display:block;
margin-left:auto;
margin-right:auto;
}
默认情况下,文本区域显示为显示:内联
,这就是为什么您可以轻松地将它们并排放置的原因,以及 text-align:center
答案也起作用的原因。
通过将文本区域放在:
< 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将文本区域居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!