What is the best way to conceal an element using a smaller one?

I've been working on an AutoComplete feature and I've hit a roadblock. I'm trying to make the suggestions slide down into view with a smooth animation using CSS transform: translateY. The problem is that when the suggestions list is taller than the textbox, it doesn't fully hide behind it, clipping out of the top.

To illustrate the issue, I've set up a demo at this link and included the code below (try typing in the textbox):

document.querySelector(".autocomplete").oninput = e => {
  let value = e.target.value;

  if (value.length > 0) {
    document.querySelector(".suggestions").classList.add("open");
  } else {
    document.querySelector(".suggestions").classList.remove("open");
  }
}
.autocomplete {
  border: 1px solid #ddd;
  padding: 5px;
  width: 500px;
  background-color: yellow;
  z-index: 1;
  position: relative;
}

.suggestions {
  border: 1px solid #ddd;
  height: 125px;
  width: 500px;
  transform: translateY(-30px);
  transition: .25s;
  background-color: #eef;
}

.open {
  transform: translateY(0);
}
<p>Some text and a paragraph preceding the autocomplete</p>
<div>
  <input class="autocomplete" />
  <div class="suggestions">apple, banana, carrot, dog</div>
</div>

I'd like to change the transform: translateY(-30px); to transform: translateY(-130px); to have the suggestions hide completely behind the textbox. However, simply altering the value causes clipping issues. Additionally, I can't use scale because it distorts the appearance during animation.

While adding extra elements before the AutoComplete as a temporary fix may work, it's not a scalable solution for different application states. Is there another approach I can take to achieve the desired effect?

Thank you.

Answer №1

If you want to enhance user experience, consider adding a custom placeholder like

placeholder="enter text here"
for your input field. Then, in your CSS file, include the following:

input:not(:placeholder-shown)+div {
  display: block;
}

.autocomplete {
 display: none;
}

To see this code in action, check out this working demo on CodePen: https://codepen.io/yael-screenovate/pen/KKVxrrj

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

Creating a hexagon pattern within an array

I'm in need of assistance in writing a function that can generate a hexagon pattern within a 2D array. I only have the column size of the array, and the rest of the dimensions must be calculated. https://i.sstatic.net/WHuYF.png Unfortunately, I&apos ...

How can I stop jQuery from repeatedly appending content every time I click the button?

Hey there, I have a question about calling a JSON array using jQuery. Every time I press the button, it loads the list again instead of multiplying. Here is the JSON array: [{"denumire":"Q Club"},{"denumire":"Carul cu Flori"},{"denumire":"La Rocca"}] And ...

Sign-up Form - Capturing 'Undefined' values from the user interface and storing it in the database using Express and PostgreSQL

I've encountered an issue where every user entry from the registration form on the frontend results in a Null object in the terminal and a corresponding null row in the database: Terminal Output: {"customer_id":6, "first_name":null, "last_name":null ...

tutorial on adding a user-inputted value to an array in AngularJS

I need assistance with organizing my input fields that are categorized under different headings:- <label>Customer</label> <input type="text" ng-model="a.arr.customerName" name="arr[0]"/> <input type="text" ng-model="a.arr.customer ...

Arrange pictures and div elements in a horizontal layout

I am facing an issue with my code that is displaying 5 'cards' in a messed up layout instead of a straight line. I have tried to align them vertically, but some are still higher and not in the right positions. For reference, I want the layout to ...

Unexpected UTF-8 Encoding Issue Found on Website Featuring French Content

I am currently developing a NodeJS Web App with 5 different home pages for 5 different languages. Everything seems to be working smoothly except for the French version, which is displaying broken characters as shown in this image: https://i.sstatic.net/B6f ...

The FlatList component with a specified number of columns is failing to display the desired

Hey guys, I have a food list data stored in JSON format as shown below: [ { "id": 0, "key": " Viande hachee", "checked": false, "image": "https://i.imgur.com/RuVD8qi.png " }, { "id": 1, "key": " Escalope pane", "check ...

Displaying selected values in a Multi Select Listbox upon submission of the same form when an error occurs

When the page is first loaded: Retrieve the values from the table field and store them in a variable If the field is blank, do not take any action Populate the listbox with default custom values When the form is submitted (on the same page) and multipl ...

Why is it necessary for the initial item to be vertically aligned in the middle?

Within my HTML structure, there is a main div with three child divs. I am aiming to have the second and third child elements (text and button) centered within the parent container. <div class='border'> <div class='inline-bl ...

Creating types for React.ComponentType<P> in Material-UI using TypeScript

I am currently working with Typescript and incorporating Material-UI into my project. I am trying to define the component type for a variable as shown below: import MoreVert from '@material-ui/icons/MoreVert' import { SvgIconProps } from '@ ...

access various paths to distinct iframes

<?php // Specify the directory path, can be either absolute or relative $dirPath = "C:/xampp/htdocs/statistics/pdf/"; // Open the specified directory and check if it's opened successfully if ($handle = opendir($dirPath)) { // Keep readin ...

Is there a way to retrieve the id of an input field using PHP?

I am faced with a situation where I have an HTML input field that is specified with a unique id. My goal is to use this input field id to identify the field effectively. For example: <input type='text' id='float' name='attribu ...

The Order of Transitions Reversed in CSS

I have a unique situation that I'm hoping to get some help with. I've been trying to create an effect where I move text away and lift up a gray box, and then when my mouse moves away from the gray box, the box goes down first before the text move ...

Remove the parent element when clicked

I've been attempting to remove the li element when I click a button with a span, but unfortunately, I haven't been successful so far. //remove testCase row when delete button is clicked $("a[id^='delete-']").click(function() { cons ...

How can I reset the search input in v-autocomplete (multiple) after selecting a checkbox from the drop-down menu?

I am facing an issue with the v-autocomplete component from Vuetify. Currently, when I enter "Cali" in the search input and select "California" from the dropdown list, the "Cali" value remains in the search input field. I need the entered value to be clear ...

Issues with sending data using Jquery and AJAX

I'm having trouble sending arrays and data to a page as they are not being posted. I tried using print_r($_POST); but it only shows Array() output and the other data is not getting posted. Here is the script: <script type="text/javascript"> $ ...

I am seeking guidance on extracting specific data from a JSON file and presenting it in an HTML table using jQuery

I am currently working on extracting specific elements from a JSON file and displaying them in an HTML table. I have successfully retrieved the data from the JSON file, stored it in an array, and then dynamically created an HTML table to display the conten ...

Having issues updating cookies with jQuery in ASP.NET framework

On my asp.net web page, I have implemented a search filter functionality using cookies. The filter consists of a checkbox list populated with various categories such as sports, music, and food. Using a jQuery onchange event, I capture the index and categor ...

Every time an npm installation is attempted, the following error occurs: "npm ERR! Cannot read property 'resolve' of undefined."

Welcome Everyone! Currently, I am facing an issue on my dual boot system where Node and NPM were functioning smoothly on Windows 7. However, now that Windows 7 is not booting up, presumably due to hardware problems, I have resorted to using Windows 10. E ...

How to extract the value of an HTML text field by executing an SQL query in PHP

Sign In Page: Here is where users can enter their login credentials. Method used is PHP POST. <html> <head> <title> LOG IN </title> <style> body { text-align: center; } </style> </head> <body> <form ac ...