AnswerBun.com

How can I vertically align this placeholder inside an input="text" form?

Stack Overflow Asked by Joaquin on January 1, 2022

The Problem

So I am trying to make this look good, but I can’t. The problem is that I can’t vertically align the text. If someone can help me, I would be very grateful.

Here is the css:

#search-bar {
    margin-top: -3%;
    margin-left: 30%;
    width: 40%;
    padding-top: 2%;
    border: 2px solid whitesmoke;
    border-radius: 25px;
}

3 Answers

I guess you want to create custom input.
And my suggestion is you can achieve it by writing code like this

#search-bar {
  padding: 10px 20px;
  border-radius: 20px;
  border: none;
}

body {
  background: #292929;
}
<input id="search-bar" placeholder="Some text.." />

You can also style placeholder with ::placeholder

#search-bar {
  padding: 10px 20px;
  border-radius: 20px;
  border: none;
}

#search-bar::placeholder {
  color: red;
  letter-spacing: 4px;
}

body {
  background: #292929;
}
<input id="search-bar" placeholder="Some text.." />

EDIT

And I would like to suggest you some flexbox too.
Please take a look at this code. We can center the child element in the parent easily with display: flex and use justify-content, align-items

.parent {
  border: 1px solid black;
  width: 200px;
  height: 200px;
  
  display: flex;
  justify-content: center; /* this is for horizontal alignment */
  align-items: center; /* this is for vertical alignment */
}

.child {
  background: lightskyblue;
  width: 50px;
  height: 50px;
}
<div class="parent">
  <div class="child"></div>
</div>

Answered by Thanyathon Pornsawatchai on January 1, 2022

by default text in a input in vertically centered.

padding-top: 2%;

is causing it to be pushed to the bottom.

for a fix i would recommend using a height or also setting padding-bottom to 2%

Answered by denniz crypto on January 1, 2022

Hard to say without providing any html or css, but it looks like you might be forcing a height on your input without changing the line-height property.

Answered by David Carignan on January 1, 2022

Add your own answers!

Related Questions

Add AWS Cognito to angular application

1  Asked on November 16, 2021 by captainmorgan

     

How to analize which object owns the max memory

0  Asked on November 16, 2021 by loverszhaokai

         

enable_if, SFINAE and template parameters?

2  Asked on November 16, 2021

 

Dynamic ‘wait’ arg in Scrapy-splash

0  Asked on November 16, 2021 by winters

       

Grab a decimal between 2 strings regex

1  Asked on November 15, 2021 by lenny-gonzalez

 

Django , models object not displayed in views

2  Asked on November 15, 2021 by mxzero-mxzero

     

Re-render a sibling component using hooks

3  Asked on November 15, 2021 by programmer1

     

filter method returns empty array

3  Asked on November 15, 2021

     

Fit container inside image

1  Asked on November 15, 2021

 

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir