Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

floating label not working correctly with not(:placeholder-shown)

currently I’m implementing a simple floating label. Below is a sample. The issue is, that if I fill something in the input and lose focus, the label stays at it’s place and the not(:placeholder-shown) isn’t recognized. Why?

.k-textbox {
  display: flex;
  width: 100%;
  padding: 0.375rem 0.75rem;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.5;
  height: 35px;
  color: #3e5569;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #e9ecef;
  appearance: none;
  border-radius: 4px;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  font-family: "Nunito Sans", sans-serif;
}

.x-floating {
  position: relative;
}

.x-floating-label {
  font-weight: 700;
}

.x-floating > .x-floating-label {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  padding: 1rem 0.75rem;
  pointer-events: none;
  border: 1px solid transparent;
  transform-origin: 0 0;
  transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
  box-sizing: border-box;
}

.x-floating > .k-textbox:focus,
.x-floating > .k-textbox:not(:placeholder-shown) {
  padding-top: 1.625rem;
  padding-bottom: 0.625rem;
}

.x-floating > .k-textbox:focus ~ span,
.x-floating > .k-textbox:not(:placeholder-shown) ~ kendo-label {
  opacity: 0.65;
  transform: scale(0.85) translateY(-0.5rem) translateX(0.15rem);
}
<div class="x-floating">
        <input class="k-textbox" placeholder="&nbsp;"></input>
        <span  class="x-floating-label">Username</span>
      </div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I think you should use .x-floating-label instead of kendo-lable in the last styles of your css.

.k-textbox {
  display: flex;
  width: 100%;
  padding: 0.375rem 0.75rem;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.5;
  height: 35px;
  color: #3e5569;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #e9ecef;
  appearance: none;
  border-radius: 4px;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  font-family: "Nunito Sans", sans-serif;
}

.x-floating {
  position: relative;
}

.x-floating-label {
  font-weight: 700;
}

.x-floating > .x-floating-label {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  padding: 1rem 0.75rem;
  pointer-events: none;
  border: 1px solid transparent;
  transform-origin: 0 0;
  transition: opacity 0.1s ease-in-out, transform 0.1s ease-in-out;
  box-sizing: border-box;
}

.x-floating > .k-textbox:focus,
.x-floating > .k-textbox:not(:placeholder-shown) {
  padding-top: 1.625rem;
  padding-bottom: 0.625rem;
}

.x-floating > .k-textbox:focus ~ span,
.x-floating > .k-textbox:not(:placeholder-shown) ~ .x-floating-label {
  opacity: 0.65;
  transform: scale(0.85) translateY(-0.5rem) translateX(0.15rem);
}
<div class="x-floating">
        <input class="k-textbox" placeholder="&nbsp;"></input>
        <span  class="x-floating-label">Username</span>
      </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading