CSS / HTML 居中文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3005119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:12:08  来源:igfitidea点击:

CSS / HTML centering a textbox

css

提问by Robert

This should be easy but its proving difficult...

这应该很容易,但事实证明它很难......

My element I want centred is exactly this <input type="text">

我想要居中的元素正是这个 <input type="text">

I don't want the text centred, just the text box within the outer div.

我不希望文本居中,只是外部 div 内的文本框。

This is my attempt which is not working

这是我的尝试,但不起作用

<div class ="temp123">
    <input type="text" />
</div>

Where:

在哪里:

.temp123
{
margin: 0 auto;
margin-left: auto;
margin-right: auto;
}

The text box remains on the left.

文本框保留在左侧。

The outer div has a fixed with of 300px and itself is centered using margin: 0 auto;

外部 div 有一个固定的 300px 并且它本身使用 margin: 0 auto;

回答by oezi

.temp123{
  text-align:center;
}
.temp123 > input{
  text-align:left;
}

or simply (but not tested):

或简单地(但未测试):

.temp123 > input{
  margin: 0 auto;
  margin-left: auto;
  margin-right: auto;
}

回答by Ralf de Kleine

Maybe I'm not getting the question but this should do it.

也许我没有得到这个问题,但这应该可以。

<style>
.temp123
{
margin: 0 auto;
margin-left: auto;
margin-right: auto;
text-align:center;
}
</style>
<div class ="temp123">
    <input type="text" />
</div>

回答by Kyle

in your CSS you're selecting the div's margin, the space outside the div.

在您的 CSS 中,您选择了 div 的边距,即 div 外的空间。

You'll need to select it like .temp123 input {...}

你需要像这样选择它 .temp123 input {...}

Hope that helps.

希望有帮助。