Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css - how to change position of input placeholder [duplicate]

Tags:

html

css

I want to put input placeholder on input border.

enter image description here

I manage to change color but not location.

.filter::placeholder {
   color: gray;
}
<input type="text" class="filter">

Is there a way to do it?

like image 946
haya Avatar asked Jan 15 '18 10:01

haya


People also ask

How do I change placeholder position in CSS?

Change Input Placeholder Text with CSS You can use the ::placeholder pseudo-element to change the styles of the placeholder text, which includes the ability to change the background.

Can you change placeholder with CSS?

<input type="text" placeholder="A red placeholder text..">

How do you align a placeholder vertically?

I believe that as you have set min-height: 45px you should set line-height: 45px too in input[type=text] . See that for the placeholder if you vary font-size , the placeholder will stay vertically centered.


2 Answers

You can't do this by styling the ::placeholder, it only supports a limited number of styles: https://css-tricks.com/almanac/selectors/p/placeholder/#article-header-id-4

You can add a span that achieves a similar effect however.

.filter {
  position: relative;
  width: 195px;
  height: 30px;
  border-radius: 5px;
  outline: none;
  padding: 5px;
  border: 1px solid lightblue;
}

.placeholder {
  position: absolute;
  left: 20px;
  top: 0px;
  background-color: white;
  padding: 0 20px;
}

.filter:focus ~ .placeholder,
.filter:valid ~ .placeholder {
  display: none;
}
<input required type="text" class="filter" />
<span class="placeholder">First Name..</span>
like image 119
Jesse Avatar answered Sep 27 '22 00:09

Jesse


A more semantically correct approach would be to use labels instead of spans.

.group{
  position:relative;
}
.group>label{
  padding:0 0.2em;
  position:absolute;
  top:-0.5em;
  left:1em;
  background-color:white;
}

.group>input{
  padding:0.8em;
  border-radius: 0.5em;
  border: 2px solid lightblue;
  outline:none;
}
<form>
    <div class="group">
         <label>First name</label>
         <input type="text" class="">
    </div>
</form>
like image 42
Facundo Corradini Avatar answered Sep 25 '22 00:09

Facundo Corradini