Home > Software design >  floating label not working correctly with not(:placeholder-shown)
floating label not working correctly with not(:placeholder-shown)

Time:01-02

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 >
        <input  placeholder="&nbsp;"></input>
        <span  >Username</span>
      </div>

CodePudding user response:

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 >
        <input  placeholder="&nbsp;"></input>
        <span  >Username</span>
      </div>

  • Related