Is there a way to lock the eye icon in place within the password field using Vue.js?

I added an eye icon to the right side of my password input fields. However, occasionally error messages may pop up on the page to signify issues with the input fields. When these error messages appear below the relevant input field, the position of the eye icon in the password field shifts. Here is my password field featuring an eye icon. (To view the password in text format, the user must click and hold.)

      <div>
        <label></label>
        <input :type="show ? 'text' : 'password'" id="password" v-model="password"/>
        <div class="eyeButton">
          <span @mousedown="show = !show" @mouseup="show = !show" @touchstart="show = !show" @touchend="show = !show" style="cursor: pointer;">eye_icon</span>
        </div>
        <small class="errorMessage" v-if="password.error">Password field is required.</small>    
      </div>

I have excluded the line with the error message as I simplified it for this context. It doesn't affect the question but was left for you to know that there is a conditional error message present.

Additionally, I included some CSS to position the eye icon within the password input field.

.eyeButton {
  position: absolute;
  right: 5px;
  bottom: 4px;
  top: 23px;
}

Here is my query accompanied by a clear screenshot.

Despite any error message appearing below the input field, the eye icon should maintain its original look.

However, when an error message is displayed on the page, the position of the eye icon changes accordingly. This is what happens;

How can I adjust the placement of the eye icon to align with the input field consistently?

Answer №1

If you want to incorporate an input and icon together, consider enclosing them in a div with relative positioning:

<div style="position: relative">
  <input :type="show ? 'text' : 'password'" id="password" v-model="password"/>
  <div class="eyeButton">
    <span @mousedown="show = !show" @mouseup="show = !show" @touchstart="show = !show" @touchend="show = !show" style="cursor: pointer;">eye_icon</span>
  </div>
</div>
<small class="errorMessage" v-if="password.error">Password field is required.</small>

Consider using predefined utility classes from your CSS framework or creating a custom one for styling purposes. According to the MDN documentation, an absolutely positioned element is relative to its closest ancestor with position property set.

Alternatively, you can eliminate bottom: 4px; from .eyeButton.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble triggering hidden radio button in Angular 9 when clicked

I have implemented radio buttons within a div with the following CSS styles (to enable selection by clicking on the div): .plans-list { display: flex; justify-content: center; margin: 2rem 0; .plan { display: flex; margin: 0 0.5rem; p ...

CDK virtual scroll problem occurs when scrolling down, items are displayed slowly and occasionally blank for a few seconds

In my project, I am utilizing Angular's virtual scroll feature. However, whenever I scroll down, the items load but flicker momentarily. I have attempted to use maxBufferPx and minBufferPx as well as adjusting positions to resolve this issue, but so ...

I am struggling with how to print the value of an object in nativescript-vue. Can anyone help me with

Upon examining the nativescript-vue code provided, I encountered an issue where I am unable to print json.city.value in the Label element. However, I can successfully print it within the script section; specifically inside the readFromJson method using c ...

There seems to be a white space problem located beneath the header photo and the initial

I'm a beginner at coding and working on this project for fun. I'm almost done, but I can't get rid of a white space that's dividing my header from the first section. See the screenshot below: https://i.stack.imgur.com/a1flt.png Even af ...

What is the best way to make a div stick to the inside of another div with Bootstrap?

As a newcomer to Bootstrap 4, I am curious if there is a way to affix a div to the bottom of its parent div. My structure involves using the classes .container within a .row, along with several child divs: <div class='container'> <d ...

What is the best way to connect DataTable() to an already existing HTML <table> by utilizing Vue.js and populating it with data from an array using a v-for loop

Having trouble implementing DataTable features like search field and column sorting in Vue.js for my table. I have included the original datatable.min.js in the <head> section, and also added and called another custom.js file with the following cont ...

Ways to modify the font style of Python comments in JupyterLab's code cells and code editor:

Many resources discuss how to change font families for all content, but I am seeking guidance on making syntax-specific changes. For example, I would like to switch the font style of Python comments from italic to normal. Can anyone offer assistance? Thank ...

Expanding SVG Button Overlay in ChakraUI

I am trying to design a uniquely shaped button that sits on top of an image, but I am encountering some challenges with the scaling of the button mask. Both the image and masks were created at the same base dimensions for easy alignment at 0,0. Here is a ...

Exploring the functionality of the PageIndicator for Wearable Technology

Exploring the Page Indicator within a Tizen Wearable Application for Gear S3 Frontier has been an interesting journey. Despite successful implementation with text content, adding controls to each section or incorporating background images has proven challe ...

Positioning the horizontal menu and footer links to the right side of the page

Struggling to get my menu and footer links aligned all the way to the right of the page. Here's what it currently looks like http://prntscr.com/32snbr, and this is the desired alignment http://prntscr.com/32snrm I've shared the HTML code below, ...

The text element does not line up with the icons

As I'm working on creating my first account page header, I've run into an issue with some of the icons not aligning perfectly with the text. While advanced developers may find this task simple, as a beginner, I could really use some advice on how ...

CSS - selecting elements that are greater than a specific criteria N

I have multiple <p> tags in the body of my HTML. I want to display only the first two paragraphs and hide all the paragraphs that come after. However, the code I'm using doesn't seem to work as expected. <html> <head> < ...

Div I add to page is not being styled by CSS

I'm currently developing a Chrome extension that provides a preview of a webpage when hovering over a hyperlink to it. To achieve this, I am creating a div and filling it with different items. However, the issue I'm facing is that the CSS styles ...

Is there a way to apply -webkit-line-clamp to this JavaScript content using CSS?

i have a random-posts script for my blogger website <div class="noop-random-posts"><script type="text/javascript"> var randarray = new Array(); var l=0; var flag; var numofpost=10; function nooprandomposts(json){ var total = ...

Troubleshooting problem with iPhone X responsiveness

Struggling with responsive issues on iPhone X. Issue is only appearing on actual device. Any tips for fixing this? I'm facing an issue where the website looks good and responsive on all devices in Chrome's responsive view. But when I access it th ...

What is the process for invoking a Java function in Android from JavaScript when using Vue?

My project is developed using Vue.js. In order to view it on a mobile phone, I decided to create an Android app. To achieve this, I am using the WebView component. However, when I click on an element in the Vue.js app, JavaScript calls Java and some events ...

Choosing the right jQuery selector to target a section that contains div elements

Whenever the h2 element within a section is clicked, I want all the fields in that section to be displayed. For example, if the user clicks on 'Contact Information', the three form inputs (fields) below the Contact Information heading should appe ...

Utilizing PrimeVue Toast to showcase rich HTML content

Looking for a way to showcase a link in a primevue toast message without using the v-html directive or triple brackets. Anyone have any alternative solutions to suggest? ...

Is there a solution for the noticeable delay in loading fonts on a web page?

I've encountered an issue in my React project where a Google font I'm using seems to briefly load as a different font before switching to the correct one. How can I prevent this from happening? This is how I'm loading the font in public/ind ...

The appearance of all input forms in MaterializeCSS when used in an electron environment appears to

After following the instructions here on how to get started with Materialize CSS, I am having issues with my input form appearing strange: The contents of my current index.html file are as follows: <!DOCTYPE html> <html> <head> & ...