placing a dropdown menu below the search bar

Let's say I have a container with an input search field. My goal is to position a select dropdown right below the search input, ensuring that the dropdown has the same width as the search input (similar to autocomplete functionality). Here's what I've attempted so far:

        <div class="container position-relative" id="wrapper">
    <div class="row mb-3">
        <div class="col">
            <div class="row float-right mr-0">
                <form class="form-inline">
                    <div class="input-group">
                        <input class="form-control ml-sm-2" type="text" id="searchUser" placeholder= "Search by Name or Username...." aria-label="Search">
                        <span class="input-group-append" id="searchBtn">
                         <div class="input-group-text bg-transparent"><i class="fa fa-search"></i></div>
                        </span>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="position-absolute" style="width:400px; z-index: 1; right:0; top:45px; ">
        <div class="form-group">
            <select multiple class="form-control" id="exampleFormControlSelect2">
                <option>1</option>
            </select>
        </div>
    </div>
</div>

Does anyone have suggestions on how to make this work?

Here's what it currently looks like:

https://i.sstatic.net/KNCy8.png

Answer №1

To achieve your desired result, you can utilize the power of css box-sizing. Below is a simple code snippet provided for better clarity:

#input1, #select1{
    width:100px;
    border:1px solid black;
    -ms-box-sizing:content-box;
    -moz-box-sizing:content-box;
    box-sizing:content-box;
    -webkit-box-sizing:content-box; 
}
<form>
<input type="text" id="input1" placeholder="input box"><br />
<select id="select1">
<option>1</option>
<option>2</option>
</select>
</form>

Answer №2

It might be helpful for you to explore Bootstrap CSS and familiarize yourself with the classes designed for forms. Forms can be styled as either inline or horizontal. By adding the form-inline class to your forms, it allows for the controls to align on the same line. Here's a snippet of code that should assist in resolving your issue:

<div class="container" id="wrapper">
 <form class="form-horizontal">
        <div class="row ">
                <div class="input-group col-5">
                    <input class="form-control " type="text" id="searchUser" placeholder= "Search by Name or Username...." aria-label="Search">
                    <span class="input-group-append" id="searchBtn">
                     <div class="input-group-text bg-transparent"><i class="fa fa-search"></i></div>
                    </span>
                </div>
            </div>
            <div class="row">
                <div class="form-group col-5">
                     <select multiple class="form-control" id="exampleFormControlSelect2">
                        <option>1</option>
                    </select>
                 </div>
            </div>
        </div>
       </form>
</div>

Answer №3

By utilizing Javascript, you have the ability to create a dynamic auto-complete feature. The process involves creating a div with an input field and a list of possible matches. Upon typing in the input field, the script compares the input value with the list items and displays matching options. Clicking on a match will populate the input field with the selected value. This implementation can also be achieved using vanilla javascript with Bootstrap. While this code serves its purpose based on existing knowledge, there are certainly areas for improvement.

// Capture input element

let input = document.querySelector("#userName");

let allListItems = document.querySelectorAll(".lists");

input.addEventListener("keyup", checkMatch);

function checkMatch(){
   
   if (input.value !== ``) {
      // Iterate through each list item
        allListItems.forEach(function(value){
            if (value.innerHTML.toLowerCase().includes(`${input.value.toLowerCase()}`)) {
                value.classList.add("showMe")
                value.addEventListener("click", complete);

                function complete(){
                    input.value = value.innerHTML;
                   allListItems.forEach(function(value){
                       value.classList.remove("showMe");
                   })
                }
            }else{
                value.classList.remove("showMe");
            }
        })

   };

}
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.box-container{
    width: 400px;
    height: auto;
    margin-left: auto;
    margin-right: auto;
    margin-top: 100px;
}
#userName{
    width: 100%;
    height: 45px;
    background-color: transparent;
    border: 0.5px solid #212121;
    font-family: "Open Sans";
    font-size: 18px;
    padding: 0px 10px;
    border-radius: 2px;
}
.box-container ul{
    width: 100%;
    height: auto;
    list-style-type: none;
}
.lists{
    width: 100%;
    height: 45px;
    padding: 0px 10px;
    text-align: left;
    line-height: 45px;
    color: #000;
    font-size: 18px;
    font-family: "Open Sans";
    display: none;
    border: 2px solid #000;
    cursor: pointer;
}
.showMe{
    display: block;
}
p{
    margin-top: 50px;
    text-align: center;
    font-size: 18px;
    
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>AutoComplete</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="box-container">
        <input type="text" id="userName" placeholder="Enter username here">
        <ul>
            <li class="lists">Alex</li>
            <li class="lists">Mox</li>
            <li class="lists">Karl</li>
            <li class="lists">NightRider</li>
            <li class="lists">Yk</li>
            <li class="lists">heroX</li>
            <li class="lists">ari</li>
            <li class="lists">hant</li>
            <li class="lists">tin</li>
        </ul>
    </div>

    <p>Try Alex , Mox , Karl</p>

    <script src="main.js"></script>
</body>
</html>

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

When encountering a 404 redirect, CSS and JS files may fail to display

I created a unique error message using .htaccess, incorporating CSS and JS in separate folders. Although it functions properly when accessed directly, integrating these resources into the 404 Error message results in only the HTML text being displayed with ...

Use the controller to set the body's background image

Seeking help to update the image URL within the CSS code snippet provided. I would like to accomplish this from the controller and can work with either LESS or SCSS. Is there a solution for this? .background{ height: 100%; width: 100%; backg ...

Unlocking the Power of jQuery's toggle Method for Dynamic Functionality

For my project, I require numerous jQuery toggles to switch between text and icons. Currently, I am achieving this by using: $("#id1").click(function () { //Code for toggling display, changing icon and text }); $("#id2").click(function () { //Same co ...

Unusual gaps of white space appearing between React components

Currently developing a small web application with multiple components. The backend functionality is all set, but encountering an unusual styling issue. Specifically, noticing a white space appearing between each component without any clear reason. This ga ...

HTML file unable to connect with CSS stylesheet

I'm facing an issue where my CSS stylesheet and HTML files aren't linking properly. Here is the code snippet from my HTML file: <head> <title>Website Sample</title> <meta charset="UTF-8"> <meta http-equiv= ...

pressure exerted on the body

Recently, I've been working on a project for the website . One of the main issues I've encountered is that the <body> element is consistently 20px lower than it should be at the top. This has led to visible problems with the background grad ...

Can I programmatically retrieve a comprehensive list of all global HTML attributes available?

Exploring the extensive list of global HTML attributes can be overwhelming, especially when considering how it continues to evolve with browser updates. Can JavaScript be leveraged to dynamically generate a complete list of these attributes without the ne ...

The rendering of HTML DOM is being obstructed on iPhone devices running iOS 13

Currently, I'm developing a web-based URL stream music player using JavaScript. The player functions smoothly on Android devices, but I'm encountering an issue with DOM rendering being blocked on iPhone devices. Despite rearranging the JavaScript ...

Retrieve data from an online JSON file

I am still learning about working with json and would appreciate some guidance. The data file I need to access is located at this url - I would like to display it in the following format: <ul id="smenu"> <li></li> </ul> Cou ...

In order to decrease the dimensions of the calendar

In the code snippet provided below, there is a date picker feature. When the date picker is clicked to open, the Date label moves down and it is desired to reduce the size of the calendar. Any assistance or guidance on how to achieve this would be greatly ...

Having trouble aligning a div vertically inside another div?

I am struggling to vertically align a div within another div. I have experimented with vertical-align, position: relative; top: 50%, and margin: auto; without success. Take a look at the code below: .main { font-family: "adobe-garamond-pro"; pad ...

Resetting the value of a radio button input option to null using Angular2 ngModel

In my Angular2 application, I am attempting to implement radio button inputs using ngModel and value. The goal is to have three options: true, false, and null. However, I am struggling to assign a value of null to one of the inputs. Ideally, when nothing ...

Inconsistent functionality of HTML5 BrightCove videos on Android devices

Encountered an issue with the Brightcove HTML5 video embedded on my website showing a black screen when clicked to play. The video plays sporadically and only on Android Devices (testing on KitKat only). In instances where it doesn't work, the templa ...

How to easily incorporate images after text in Bootstrap 4 beta

I seem to be having trouble inserting an image in the "menu" section for the last item "Kava so sebou" after the text. I want the picture to be centered vertically and the row to maintain consistency with the other items above it. Any advice or suggestions ...

Maintain the style of the main navigation menu when hovering over the sub-navigation items

I am currently in the process of redesigning the navigation bar for a specific website. The site utilizes Bootstrap, with a main navbar and a secondary navbar-subnav. While I have been able to style both navs accordingly, I am encountering difficulties whe ...

What is the best way to use Javascript in order to automatically open a designated tab within SharePoint 2013?

Currently, I am in the process of developing a website project which incorporates Bootstrap tabs utilizing jQuery. While these tabs are functioning excellently on various pages, I am faced with the challenge of linking specific icons to corresponding tabs ...

How to incorporate Bootstrap carousel into WordPress without relying on a plugin?

I've successfully integrated the bootstrap carousel into my Wordpress site. The slides are sourced from pages, with a specific page called "carousel" containing subpages where post-thumbnails are extracted. I'm still learning the ropes of using W ...

IIFE and the Twitter share button are a powerful duo

As I experiment with this quick creation of mine, two questions have arisen. The first one is, how can I encapsulate all the JS code within an IIFE without breaking it? The second question is about finishing the twitter button to enable sharing the act ...

Using jQuery to close a DIV with a delay

At the top of my website, I have set up an alert with a close button. The jQuery function for closing the alert is as follows: /* CLOSE ALERT BAR WITH BUTTON */ $('a.ctp-alert').click(function () { $("div.ctp-alert").slideUp(); }); While this f ...

Customizing Bootstrap tooltip appearances with CSS

After successfully setting up Bootstrap 4.5.0, I decided to experiment with customizing the styles of tooltips. The code snippet below shows how I attempted this. Initially, I included the necessary stylesheets at the top of the page: <link rel=&q ...