Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move label in placeholder CSS

Tags:

html

css

I have this sample:

link

CODE HTML:

<div class="field">
    <label>First Name</label>
    <div class="control">
        <input type="text" class="input-text">
    </div>
</div>

CODE CSS:

.field{
  position: relative;
}

.label {
  position: absolute;
  pointer-events: none;
  left: 10px;
  top: 2px;
  transition: 0.2s ease all;
  font-size: 15px;
}

input:focus ~ .label,
input:not(:focus):valid ~ .label {
  top: -6px;
}

Basically I want when the user writes something in the input, the label is moved over the input.

Can this be obtained only from the css?

Can you please change the example I created to understand exactly how this is done?

Thanks in advance!

like image 768
Cristi Avatar asked Sep 11 '19 07:09

Cristi


People also ask

How do I move placeholder text in CSS?

You can align the placeholder text (right, left or center) using CSS ::placeholder selector property.

How do I move my placeholder?

Move the mouse pointer over the slanted lines. When a four-headed arrow appears over the borders, click and drag the placeholder to a new location.

How do you style placeholder text?

Use the ::placeholder pseudo-element to style your placeholder text in an <input> or <textarea> form element. Most modern browsers support this, but for older browsers, vendor prefixes will be required.


2 Answers

Please try this modest code

.field{
  position: relative;
}

label {
  position: absolute;
  pointer-events: none;
  left: 10px;
  top: 2px;
  transition: 1.2s ease all;
  font-size: 15px;
}
input:focus ~ label{
  top:-10px;
  font-size:12px;
  opacity: 1; 
	margin-left: 45px;  
	transition: all 1.5s ease-out; 
	-webkit-transform: rotate(720deg);   
	zoom: 1;
}
<div class="field">
	<input type="text" class="input-text">
  <label>First Name</label>
</div>
like image 91
Wassim Al Ahmad Avatar answered Sep 21 '22 19:09

Wassim Al Ahmad


i hope this is what you need. but there is a problem when you unfocused input.

i just make little changes in HTML.

.field{
  position: relative;
}

label {
  position: absolute;
  pointer-events: none;
  left: 10px;
  top: 2px;
  transition: 0.2s ease all;
  font-size: 15px;
}
input:focus ~ label{
  top:-10px;
  font-size:12px;
}
<div class="field">
	<input type="text" class="input-text">
  <label>First Name</label>
</div>
like image 30
sbrrk Avatar answered Sep 23 '22 19:09

sbrrk