Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A How to make the text box such that its placeholder goes up and arranges itself in the centre of the border upon clicking?

The basic ideia is to create an input with a "fake" placeholder, and a span that will serve as the actual placeholder text. Then you group both inside a label, like this: <label> <inpu...

posted 2y ago by hkotsubo‭  ·  edited 2y ago by hkotsubo‭

Answer
#2: Post edited by user avatar hkotsubo‭ · 2021-09-10T01:13:04Z (over 2 years ago)
  • The basic ideia is to create an `input` with a "fake" placeholder, and a `span` that will serve as the actual placeholder text. Then you group both inside a `label`, like this:
  • ```html
  • <label>
  • <input type="password" placeholder=" ">
  • <span>Enter your password</span>
  • </label>
  • ```
  • Note the `input`'s "fake" placeholder (just a single space). We'll use it just to know when the placeholder is being displayed or not (such as when you click in the `input` field - in that case, the placeholder disappears so you can type). To detect when the placeholder is being displayed, we're gonna use the [`placeholder-shown` pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown).
  • The actual placeholder text is whatever it is inside the `span`, so we just need some CSS adjustments to place it where we need:
  • ```css
  • label {
  • position: relative;
  • display: inline-block;
  • }
  • span {
  • padding: 10px;
  • pointer-events: none;
  • position: absolute;
  • left: 0;
  • top: 0;
  • transition: 0.4s;
  • opacity: 0.5;
  • }
  • input {
  • padding: 10px;
  • }
  • /* When input is focused, or the placeholder is not being displayed, change the border to blue */
  • input:focus, input:not(:placeholder-shown) {
  • outline: none;
  • border-color: blue;
  • }
  • /* When input is focused, or the placeholder is not being displayed, change the placeholder size, position and color */
  • input:focus + span, input:not(:placeholder-shown) + span {
  • opacity: 1;
  • background-color: white;
  • color: blue;
  • transform: scale(0.75) translateY(-65%) translateX(-20px);
  • }
  • ```
  • We use `input:focus` and `input:not(:placeholder-shown)` to know when the placeholder must be moved to the top (the former is for when we click on it, the latter is for any situation when the placeholder is not being shown, such as when there's something already typed in the `input` field, regardless of its focus).
  • In those cases, the `input` changes its border color, and the placeholder (the `span` content) is moved to the top (`translateY` and `translateX`), with a smaller size (`scale(0.75)`), and I also change its color - and the background color, so the text is shown over the `input` border.
  • And you can also play with the `transition` values to control how the placeholder is animated: the `0.4s` is the time it takes to move from/to the top (0.4 seconds), and there are [lots of other options](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function) available, to achieve different effects.
  • The basic ideia is to create an `input` with a "fake" placeholder, and a `span` that will serve as the actual placeholder text. Then you group both inside a `label`, like this:
  • ```html
  • <label>
  • <input type="password" placeholder=" ">
  • <span>Enter your password</span>
  • </label>
  • ```
  • Note the `input`'s "fake" placeholder (just a single space). We'll use it just to know when the placeholder is being displayed or not (such as when you click in the `input` field - in that case, the placeholder disappears so you can type). To detect when the placeholder is being displayed, we're gonna use the [`placeholder-shown` pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown).
  • The actual placeholder text is whatever it is inside the `span`, so we just need some CSS adjustments to place it where we need:
  • ```css
  • label {
  • position: relative;
  • display: inline-block;
  • }
  • span {
  • padding: 10px;
  • pointer-events: none;
  • position: absolute;
  • left: 0;
  • top: 0;
  • transition: 0.4s;
  • opacity: 0.5;
  • }
  • input {
  • padding: 10px;
  • }
  • /* When input is focused, or the placeholder is not being displayed, change the border to blue */
  • input:focus, input:not(:placeholder-shown) {
  • outline: none;
  • border-color: blue;
  • }
  • /* When input is focused, or the placeholder is not being displayed, change the placeholder size, position and color */
  • input:focus + span, input:not(:placeholder-shown) + span {
  • opacity: 1;
  • background-color: white;
  • color: blue;
  • transform: scale(0.75) translateY(-65%) translateX(-20px);
  • }
  • ```
  • We use `input:focus` and `input:not(:placeholder-shown)` to know when the placeholder must be moved to the top (the former is for when we click on it, the latter is for any situation when the placeholder is not being shown, such as when there's something already typed in the `input` field, regardless of its focus).
  • In those cases, the `input` changes its border color, and the placeholder (the `span` content) is moved to the top (`translateY` and `translateX`), with a smaller size (`scale(0.75)`), and I also change its color - and the background color, so the text is shown over the `input` border.
  • And you can also play with the `transition` values to control how the placeholder is animated: the `0.4s` is the time it takes to move from/to the top (0.4 seconds), and there are [lots of other options](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function) available, to achieve different effects.
  • ---
  • You can see this example running [here](https://jsfiddle.net/edz3ms1t/1/).
#1: Initial revision by user avatar hkotsubo‭ · 2021-09-10T00:50:50Z (over 2 years ago)
The basic ideia is to create an `input` with a "fake" placeholder, and a `span` that will serve as the actual placeholder text. Then you group both inside a `label`, like this:

```html
<label>
  <input type="password" placeholder=" ">
  <span>Enter your password</span>
</label>
```

Note the `input`'s "fake" placeholder (just a single space). We'll use it just to know when the placeholder is being displayed or not (such as when you click in the `input` field - in that case, the placeholder disappears so you can type). To detect when the placeholder is being displayed, we're gonna use the [`placeholder-shown` pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown).

The actual placeholder text is whatever it is inside the `span`, so we just need some CSS adjustments to place it where we need:

```css
label {
  position: relative;
  display: inline-block;
}
  
span {
  padding: 10px;
  pointer-events: none;
  position: absolute;
  left: 0;
  top: 0;
  transition: 0.4s;
  opacity: 0.5;
}

input {
  padding: 10px;
}

/* When input is focused, or the placeholder is not being displayed, change the border to blue */
input:focus, input:not(:placeholder-shown) {
  outline: none;
  border-color: blue; 
}

/* When input is focused, or the placeholder is not being displayed, change the placeholder size, position and color */
input:focus + span, input:not(:placeholder-shown) + span {
  opacity: 1;
  background-color: white;
  color: blue;
  transform: scale(0.75) translateY(-65%) translateX(-20px);
}
```

We use `input:focus` and `input:not(:placeholder-shown)` to know when the placeholder must be moved to the top (the former is for when we click on it, the latter is for any situation when the placeholder is not being shown, such as when there's something already typed in the `input` field, regardless of its focus).

In those cases, the `input` changes its border color, and the placeholder (the `span` content) is moved to the top (`translateY` and `translateX`), with a smaller size (`scale(0.75)`), and I also change its color - and the background color, so the text is shown over the `input` border.

And you can also play with the `transition` values to control how the placeholder is animated: the `0.4s` is the time it takes to move from/to the top (0.4 seconds), and there are [lots of other options](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function) available, to achieve different effects.