Center HTML Input Text Field Placeholder - html

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="support#socialpic.org" style="text-align: center" name="email" />
<!<input type="submit" value="submit" />
</form>

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

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".

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

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">

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

You can make like this:
<center>
<form action="" method="POST">
<input type="text" class="emailField" placeholder="support#socialpic.org" style="text-align: center" name="email" />
<input type="submit" value="submit" />
</form>
</center>
Working CodePen here

You can try like this :
input[placeholder] {
text-align: center;
}

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

For vertically centering the placeholder:
input::placeholder{
position:relative !important;
top:50% !important;
transform:translateY(-50%) !important;
}
You can also use padding to adjust in case you need!

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
}

Related

Vertically align an input placeholder with CSS (or set line-height)

Is it possible to vertically align a placeholder independently of the parent input? There seems to be no way to have the placeholder in the code below appear in the vertical center of the input field. I've tried all css properties and workarounds I can think of or find online but the different font sizes on the input and the placeholder seem to make it impossible in webkit browsers at least.
Edit: Left is the issue, right is the desired look:
input {
font-size: 22px;
text-align: center;
font-weight: bold;
line-height: 44px;
}
input::-webkit-input-placeholder {
font-size: 9px;
text-transform: uppercase;
color: #AAA;
}
<input type='text' placeholder='enter some text' />
The best solution out there might be to simply add a translate3d transform to the placeholder CSS rule as follows:
input {
font-size: 22px;
text-align: center;
font-weight: bold;
line-height: 44px;
}
input::-webkit-input-placeholder {
font-size: 9px;
text-transform: uppercase;
color: #AAA;
transform:translate3d(0,-4px,0)
}
<input type='text' placeholder='enter some text' />
This feels like such a stupid "solution", but maybe you could substitute transform:scale(0.45) in place of the font-size: 9px;. It seems to work just about anywhere font-size in ::-webkit-input-placeholder is supported.
input {
font-size: 22px;
text-align: center;
font-weight: bold;
line-height: 44px;
}
input::-webkit-input-placeholder {
text-transform: uppercase;
color: #AAA;
transform: scale(0.45);
}
<input type='text' placeholder='enter some text' />
Edit: Left Aligned
input {
font-size: 22px;
text-align: left;
font-weight: bold;
line-height: 44px;
}
input::-webkit-input-placeholder {
text-transform: uppercase;
color: #AAA;
transform: scale(0.45);
transform-origin: 0% 50%;
}
<input type='text' placeholder='enter some text' />

Placeholder is not vertical aligned

I used bootstrap but placeholder is not in center align
<input id="findNearestClinicText" type="text" placeholder="ENTER YOUR ZIP CODE">
Inputbox font size is : 20px
Placeholder font size is: 14px
my client doesn't want to change the font size of the input box and placeholder. but if I set it with the same font size then it aligned center.
I want the placeholder to be aligned the center of the text box with a different font size of textbox and placeholder. Please help.
use ::placeholder class in css
css
#findNearestClinicText{
width: 400px;
font-size: 20px;
}
::-webkit-input-placeholder { /* Chrome/Opera/Safari */
text-align:center;
font-size: 14px;
}
::-moz-placeholder { /* Firefox 19+ */
text-align:center;
font-size: 14px;
}
:-ms-input-placeholder { /* IE 10+ */
text-align:center;
font-size: 14px;
}
:-moz-placeholder { /* Firefox 18- */
text-align:center;
font-size: 14px;
}
Please check this link https://jsfiddle.net/xunbfvvh/
using Padding In css
Method-1 : using id
#findNearestClinicText
{
padding:10px;
font-size:20px;/* Inputbox font size*/
}
/* Placeholder font size is: 14px */
#findNearestClinicText::-webkit-input-placeholder {
font-size: 14px;
}
#findNearestClinicText::-moz-placeholder {
font-size: 14px;
}
#findNearestClinicText::-ms-input-placeholder {
font-size: 14px;
}
#findNearestClinicText::-moz-placeholder {
font-size: 14px;
}
<input id="findNearestClinicText" type="text" placeholder="ENTER YOUR ZIP CODE">
Method-2 : Using Attribute
input[type='text']
{
padding:10px;
font-size:20px;/* Inputbox font size*/
}
/* Placeholder font size is: 14px */
input[type='text']::-webkit-input-placeholder {
font-size: 14px;
}
input[type='text']::-moz-placeholder {
font-size: 14px;
}
input[type='text']::-ms-input-placeholder {
font-size: 14px;
}
input[type='text']::-moz-placeholder {
font-size: 14px;
}
<input id="findNearestClinicText" type="text" placeholder="ENTER YOUR ZIP CODE">

Why does Safari & Firefox cut off bottom of input text?

What I want
Chrome
On Chrome, the input text looks normal.
What is happening
Firefox
Safari
As you can see, the input text is being slightly cut off at the bottom on Firefox and majorly cut off on Safari. How can I fix that?
If anyone could help w/ this it would be greatly appreciated!
Code
HTML
<div class="row page-header">
<div class="col-xs-10">
<div class="form-group">
<input type="text" name="Worksheet-Name" class="form-control input-lg" placeholder="Worksheet Name..." aria-label="Write worksheet name here"> </div>
</div>
</div>
CSS
/*Add borders when hover or click on input boxes*/
input[type="text"] {
outline: none;
box-shadow:none !important;
border: 1px solid white; /*make the borders invisble by turning same color as background*/
}
input[type="text"]:hover, input[type="text"]:focus{
border: 1px solid #cccccc;
border-radius: 8px;
}
/*Style input text boxes*/
input[type='text'][name='Worksheet-Name']{
font-size: 36px;
margin-top: 20px;
margin-left: 15px;
}
input[type='text'][name='Worksheet-Problem']{
font-size: 20px;
}
/*Change placeholder*/
input[type='text'][name='Worksheet-Name']::-webkit-input-placeholder { /* Chrome/Opera/Safari */
font-weight: 500;
font-size: 36px;
}
input[type='text'][name='Worksheet-Name']::-moz-placeholder { /* Firefox 19+ */
font-weight: 500;
font-size: 36px;
}
input[type='text'][name='Worksheet-Name']:-ms-input-placeholder { /* IE 10+ */
font-weight: 500;
font-size: 36px;
}
input[type='text'][name='Worksheet-Name']:-moz-placeholder { /* Firefox 18- */
font-weight: 500;
font-size: 36px;
}
/*Change placeholder*/
input[type='text'][name='Worksheet-Problem']::-webkit-input-placeholder { /* Chrome/Opera/Safari */
font-weight: 400;
font-size: 20px;
}
input[type='text'][name='Worksheet-Problem']::-moz-placeholder { /* Firefox 19+ */
font-weight: 400;
font-size: 20px;
}
input[type='text'][name='Worksheet-Problem']:-ms-input-placeholder { /* IE 10+ */
font-weight: 400;
font-size: 20px;
}
input[type='text'][name='Worksheet-Problem']:-moz-placeholder { /* Firefox 18- */
font-weight: 400;
font-size: 20px;
}
JSFiddle
Guys sometimes proposed solutions don't work with placeholders, here is more powerful approach:
input::placeholder {
overflow: visible;
}
You can reduce the bottom padding and/or the font size and that will fix your overflow issue.
input[type='text'][name='Worksheet-Name']{
font-size: 35px;//instead of 36
margin-top: 20px;
margin-left: 15px;
}
or
.input-lg {
height: 46px;
padding: 10px 16px 0;//change here to have 0
font-size: 18px;
line-height: 1.33333;
border-radius: 6px;
}
also possibly answered here with line-height:
Why is Firefox cutting off the text in my <input type="text"/>?
Fixed this with line-height:1 on the input
The cause is placing the line-height on the placeholder, if you remove that then it will no longer be cut

Input's placeholder misaligned in Chrome when input and placeholder have different font size

When the placeholder's font-size is different to input font-size, the placeholder is misaligned vertically in Chrome (in Firefox it works fine).
Screenshot:
Here is the HTML / CSS:
body {
padding: 20px;
}
input {
padding: 0 10px;
color: green;
font-size: 30px;
height: 57px
}
input::-webkit-input-placeholder {
color: blue;
font-size: 14px;
line-height: 57px;
}
input::-moz-placeholder {
color: blue;
font-size: 14px;
}
<input type="text" value="" placeholder="Placeholder text">
Also available as a jsFiddle.
This seems like buggy behaviour by Chrome, the placeholder is aligned vertically with the baseline of the larger font size in the input.
In order to correctly vertically center the smaller placeholder text in Chrome, you can use position: relative and top: -5px as a workaround.
Workaround
body {
padding: 20px;
}
input {
padding: 0 10px;
color: green;
font-size: 30px;
height: 57px;
}
input::-webkit-input-placeholder {
color: blue;
font-size: 14px;
position: relative;
top: -5px;
}
input::-moz-placeholder {
color: blue;
font-size: 14px;
}
<input type="text" value="" placeholder="Placeholder text">

Why is the same text content rendered differently in <input> than in <div> or <span> tags?

The Situation
I have two pages with identical content and styling. The difference between them is that one lists items with <div> elements and the other with <input> elements (for showing and editing a resource, respectively).
I'd like both pages to have the same layout. I've achieved the layout I want on the standard <div> page and would like to duplicate it on the page with <input> elements. Both pages have the same CSS rules applied to them as well as Eric Mayer's Reset.
The Problem
Text is rendered differently in a <div> element than it is in an <input> element and results in an <input> that is too high.
What's Been Tried
I've tried setting the height of the input so that it is the same as the div, though that causes the text to become clipped at the bottom. I couldn't find a way to remove the white space at the top of the input.
I also did a diff of the computed styles for each element and they are almost identical (aside from a few styles that have no affect on the issue here).
The Question
Is there a way to make the input in the first picture match the height of the div in the second?
Additionally, is there a place where I can learn more about how/why browsers have this sort of behavior and what controls it? I've already read through W3C's CSS Fonts Module Level 3 with unsatisfactory results.
See current state (input, div):
.recipe .header {
margin-bottom: 50px;
text-align: left;
font-weight: 600;
color: #f0424b;
}
.form-recipe input {
font-family: "futura-pt";
}
.header {
margin-bottom: 40px;
font-size: 3em;
text-align: center;
}
// Reset
input {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-weight: inherit;
font-style: inherit;
font-family: inherit;
font-size: 100%;
vertical-align: baseline;
}
// User agent stylesheet
input {
padding: 1px 0px;
-webkit-appearance: textfield;
padding: 1px;
background-color: white;
border: 2px inset;
border-image-source: initial;
border-image-slice: initial;
border-image-width: initial;
border-image-outset: initial;
border-image-repeat: initial;
-webkit-rtl-ordering: logical;
-webkit-user-select: text;
cursor: auto;
}
<div class="delicious">
<form name="recipe.edit" class="form form-recipe recipe">
<div class="form-group">
<div class="form-row">
<input type="text" class="header" value="Banana Bread"/>
</div>
</div>
</form>
</div>
.recipe .header {
margin-bottom: 50px;
text-align: left;
font-weight: 600;
color: #f0424b;
}
.header {
margin-bottom: 40px;
font-size: 3em;
text-align: center;
}
// Reset
div {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-weight: inherit;
font-style: inherit;
font-family: inherit;
font-size: 100%;
vertical-align: baseline;
}
// User agent stylesheet
div {
display: block;
}
<div class="delicious">
<div class="recipe">
<div class="header">
Banana Bread
</div>
</div>
</div>
How about this? That works if you want them exactly identical. (The spaces between them are because of the other wrappers)
HTML
<div class="delicious">
<form name="recipe.edit" class="form form-recipe recipe">
<div class="form-group">
<div class="form-row">
<input type="text" class="header" value="Banana Bread"/>
</div>
</div>
</form>
</div>
<!------------------------------------------------------------>
<div class="delicious">
<div class="recipe">
<div class="header">
Banana Bread
</div>
</div>
</div>
CSS
.recipe .header {
margin-bottom: 50px;
text-align: left;
font-weight: 600;
color: #f0424b;
}
.form-recipe input {
font-family: "futura-pt";
}
.header {
margin-bottom: 40px;
font-size: 3em;
text-align: center;
width:100%;
border:none;
padding:0;
outline:0;
}
Though I personally wouldn't recommend this, because I believe the user needs to realize that it's now able to edit the contents, anyway, maybe set outline-color:#f0424b and focus the element on edit? outline does not affect the element's width/height but it does give some clue that you are in focus and able to edit.
Hope it helps!