居中 HTML 输入文本字段占位符

发布于 2024-12-04 02:17:55 字数 722 浏览 1 评论 0原文

如何在 html 表单中将输入字段的占位符对齐居中?

我正在使用以下代码,但它不起作用:

CSS ->

input.placeholder {
    text-align: center;
}
.emailField {
    top:413px;
    right:290px;
    width: 355px;
    height: 30px;
    position: absolute;
    border: none;
    font-size: 17;
    text-align: center;
}

HTML->

<form action="" method="POST">
    <input type="text" class="emailField" placeholder="[email protected]" style="text-align: center" name="email" />
    <!<input type="submit" value="submit" />  
</form>

How can I centre the input field's placeholder's alignment in a html form?

I am using the following code, but it doesn't work:

CSS ->

input.placeholder {
    text-align: center;
}
.emailField {
    top:413px;
    right:290px;
    width: 355px;
    height: 30px;
    position: absolute;
    border: none;
    font-size: 17;
    text-align: center;
}

HTML ->

<form action="" method="POST">
    <input type="text" class="emailField" placeholder="[email protected]" style="text-align: center" name="email" />
    <!<input type="submit" value="submit" />  
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

嗫嚅 2024-12-11 02:17:55

如果您只想更改占位符样式,请选择 ::placeholder 伪元素

::placeholder {
   text-align: center; 
}

/* or, for legacy browsers */

::-webkit-input-placeholder {
   text-align: center;
}

:-moz-placeholder { /* Firefox 18- */
   text-align: center;  
}

::-moz-placeholder {  /* Firefox 19+ */
   text-align: center;  
}

:-ms-input-placeholder {  
   text-align: center; 
}

If you want to change only the placeholder style, select the ::placeholder pseudo-element

::placeholder {
   text-align: center; 
}

/* or, for legacy browsers */

::-webkit-input-placeholder {
   text-align: center;
}

:-moz-placeholder { /* Firefox 18- */
   text-align: center;  
}

::-moz-placeholder {  /* Firefox 19+ */
   text-align: center;  
}

:-ms-input-placeholder {  
   text-align: center; 
}
手长情犹 2024-12-11 02:17:55
input{
   text-align:center;
}

就是你所需要的。

FF6 中的工作示例。此方法似乎不跨浏览器兼容。

您之前的 CSS 试图将具有“占位符”类的输入元素的文本居中。

input{
   text-align:center;
}

is all you need.

Working example in FF6. This method doesn't seem to be cross-browser compatible.

Your previous CSS was attempting to center the text of an input element which had a class of "placeholder".

没有心的人 2024-12-11 02:17:55

您也可以使用这种方式为占位符编写CSS

input::placeholder{
   text-align: center;
}

/* or, for legacy browsers */

::-webkit-input-placeholder {
   text-align: center;
}

:-moz-placeholder { /* Firefox 18- */
   text-align: center;  
}

::-moz-placeholder {  /* Firefox 19+ */
   text-align: center;  
}

:-ms-input-placeholder {  
   text-align: center; 
}

you can use also this way to write css for placeholder

input::placeholder{
   text-align: center;
}

/* or, for legacy browsers */

::-webkit-input-placeholder {
   text-align: center;
}

:-moz-placeholder { /* Firefox 18- */
   text-align: center;  
}

::-moz-placeholder {  /* Firefox 19+ */
   text-align: center;  
}

:-ms-input-placeholder {  
   text-align: center; 
}
陈甜 2024-12-11 02:17:55

您可以在如下所示的类中使用 set 并将其设置为输入文本类

CSS:

 .place-holder-center::placeholder {
        text-align: center;
    }

HTML:

<input type="text" class="place-holder-center">

You can use set in a class like below and set to input text class

CSS:

 .place-holder-center::placeholder {
        text-align: center;
    }

HTML:

<input type="text" class="place-holder-center">
忆梦 2024-12-11 02:17:55

HTML5 占位符元素可以针对接受该元素的浏览器进行样式设置,但方式不同,如下所示: http://davidwalsh.name/html5-placeholder-css

但我不相信 text-align 会被浏览器解释。至少在 Chrome 上,该属性被忽略。但是你总是可以改变其他的东西,比如colorfont-sizefont-family等。我建议你重新考虑你的设计是否可以删除这个中心行为。

编辑

如果您确实希望此文本居中,您可以随时使用一些 jQuery 代码或插件来模拟占位符行为。以下是它的示例: http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html" hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html

这样样式就可以工作了:

input.placeholder {
    text-align: center;
}

The HTML5 placeholder element can be styled for those browsers that accept the element, but in diferent ways, as you can see here: http://davidwalsh.name/html5-placeholder-css.

But I don't believe that text-align will be interpreted by the browsers. At least on Chrome, this attribute is ignored. But you can always change other things, like color, font-size, font-family etc. I suggest you rethinking your design whether possible to remove this center behavior.

EDIT

If you really want this text centered, you can always use some jQuery code or plugin to simulate the placeholder behavior. Here is a sample of it: http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html.

This way the style will work:

input.placeholder {
    text-align: center;
}
梦归所梦 2024-12-11 02:17:55

你可以这样做:

    <center>
    <form action="" method="POST">
    <input type="text" class="emailField" placeholder="[email protected]" style="text-align: center" name="email" />
    <input type="submit" value="submit" />  
</form>
    </center>

这里工作CodePen

You can make like this:

    <center>
    <form action="" method="POST">
    <input type="text" class="emailField" placeholder="[email protected]" style="text-align: center" name="email" />
    <input type="submit" value="submit" />  
</form>
    </center>

Working CodePen here

独守阴晴ぅ圆缺 2024-12-11 02:17:55

你可以这样尝试:

input[placeholder] {
   text-align: center;
}

You can try like this :

input[placeholder] {
   text-align: center;
}
走过海棠暮 2024-12-11 02:17:55

对于占位符垂直居中:

input::placeholder{
position:relative !important;
top:50% !important;
transform:translateY(-50%) !important; 
}

For vertically centering the placeholder:

input::placeholder{
position:relative !important;
top:50% !important;
transform:translateY(-50%) !important; 
}
此岸叶落 2024-12-11 02:17:55
::-webkit-input-placeholder {
 font-size: 14px;
 color: #d0cdfa;
 text-transform: uppercase;
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
}
:-moz-placeholder { 
 font-size:14px;
 color: #d0cdfa;
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
}
::-moz-placeholder { 
 font-size: 14px;
 color: #d0cdfa; 
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
} 
:-ms-input-placeholder { 
 font-size: 14px; 
 color: #d0cdfa;
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
}
::-webkit-input-placeholder {
 font-size: 14px;
 color: #d0cdfa;
 text-transform: uppercase;
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
}
:-moz-placeholder { 
 font-size:14px;
 color: #d0cdfa;
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
}
::-moz-placeholder { 
 font-size: 14px;
 color: #d0cdfa; 
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
} 
:-ms-input-placeholder { 
 font-size: 14px; 
 color: #d0cdfa;
 text-transform: uppercase;
 text-align: center;
 font-weight: bold;
}
趁年轻赶紧闹 2024-12-11 02:17:55

通过使用下面的代码片段,您可以选择输入中的占位符,并且放置在其中的任何代码只会影响占位符。

input::-webkit-input-placeholder {
      text-align: center
}

By using the code snippet below, you are selecting the placeholder inside your input, and any code placed inside will affect only the placeholder.

input::-webkit-input-placeholder {
      text-align: center
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文