Create Animated Input Field Floating Label UI With Latest Tailwindcss v2.2.0

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 พ.ย. 2024

ความคิดเห็น • 23

  • @Laratips
    @Laratips  3 ปีที่แล้ว +3

    When you write some text in the input and then click on the outside, the label will overlap the text. To fix this you need some JavaScript. Thanks, Aashiq Aasnat for pointing it out.
    You need to first add and id to the label as 'username-label', then you can add this in the JavaScript. This code is just adding and removing some classes depending on the value of the input.
    let label = document.getElementById('username-label');
    document.getElementById('username')
    .addEventListener('input', event => {
    let input = event.target;
    if (event.target.value) {
    ['border-purple-600', 'border-b-2'].forEach(c => input.classList.add(c));
    ['text-xs', '-top-4', 'text-purple-600'].forEach(c => label.classList.add(c));
    } else {
    ['border-purple-600', 'border-b-2'].forEach(c => input.classList.remove(c));
    ['text-xs', '-top-4', 'text-purple-600'].forEach(c => label.classList.remove(c));
    }
    })

    • @danuhandika8328
      @danuhandika8328 ปีที่แล้ว

      you are the good man bro, thanks

    • @psyferinc.3573
      @psyferinc.3573 ปีที่แล้ว

      i wish you helped with the js code too. im new to design so the js behind it is still rough

  • @AnkurRajbongshi-u1q
    @AnkurRajbongshi-u1q ปีที่แล้ว

    Thanks a lot.... This is step-by-step kind info.

  • @sharmajikaladka836
    @sharmajikaladka836 3 ปีที่แล้ว

    Daaami ❤️. Feri yesto dami dekhkhiyo bhane hanuwla subscribe

  • @penksantuyyy9519
    @penksantuyyy9519 2 ปีที่แล้ว

    Thankns for the video, you saved my day

    • @Laratips
      @Laratips  2 ปีที่แล้ว

      You are most welcome

  • @MrSonicastra
    @MrSonicastra 3 ปีที่แล้ว

    Thanks for this video!

    • @Laratips
      @Laratips  3 ปีที่แล้ว

      You are welcome

  • @therevealmusic
    @therevealmusic ปีที่แล้ว

    how can we do this to a drop down select

  • @karlnewgrove
    @karlnewgrove 2 ปีที่แล้ว +3

    Instead of a JavaScript solution for the overlapping texts, another alternative would be to use the peer-placeholder-shown pseudo-class:
    text-xs
    peer-focus:text-xs
    peer-placeholder-shown:text-base
    Then just use an empty placeholder in the input field: placeholder=" "

    • @Laratips
      @Laratips  2 ปีที่แล้ว

      Wow. I will try this. Thanks

    • @michaelfestus3619
      @michaelfestus3619 2 ปีที่แล้ว

      Please can you explain better

    • @karlnewgrove
      @karlnewgrove 2 ปีที่แล้ว

      ​@@michaelfestus3619 The solution is from Tailwind's TH-cam channel: th-cam.com/video/nJzKi6oIvBA/w-d-xo.html

    • @shahchandan1369
      @shahchandan1369 ปีที่แล้ว

      Can you please explain better

    • @karlnewgrove
      @karlnewgrove ปีที่แล้ว +1

      Add placeholder=" " to (the empty space is important). Then use peer-placeholder-shown and peer-focus on . This enables the label to be used as both the placeholder and the label. Example: .

  • @aashiq_hasnat
    @aashiq_hasnat 3 ปีที่แล้ว

    So you didn't show what happens after you write something on the input, label overlap with the input text. plz fix it

    • @Laratips
      @Laratips  3 ปีที่แล้ว +1

      Oh yes thanks for the feedback. It looks like a little bit tricky. I will try to fix it and pin the fix in the comment.

    • @Laratips
      @Laratips  3 ปีที่แล้ว +1

      It looks like we need some JavaScript to accomplish this:
      You need to first add and id to the label as 'username-label', then you can add this in the JavaScript. This code is just adding and removing some classes depending on the value of the input.
      let label = document.getElementById('username-label');
      document.getElementById('username')
      .addEventListener('input', event => {
      let input = event.target;
      if (event.target.value) {
      ['border-purple-600', 'border-b-2'].forEach(c => input.classList.add(c));
      ['text-xs', '-top-4', 'text-purple-600'].forEach(c => label.classList.add(c));
      } else {
      ['border-purple-600', 'border-b-2'].forEach(c => input.classList.remove(c));
      ['text-xs', '-top-4', 'text-purple-600'].forEach(c => label.classList.remove(c));
      }
      })

  • @nicholassingh138
    @nicholassingh138 3 ปีที่แล้ว

    Can this work for textarea?

    • @Laratips
      @Laratips  3 ปีที่แล้ว

      It should work.