Learn how to implement Bootstrap 4's select styling to customize dropdown links, such as using it for a tags

My goal is to apply Bootstrap 4 form styling, like form-control and custom-select, to actual links (dropdown a tag).

For example:

<select class="custom-select" onchange="location = this.value;">
        <option selected>Choose a website</option>
        <option value="google.com">google</option>
        <option value="apple.com">apple</option>
        <option value="amazon.com">amazon</option>
</select>

The styling is perfect and works as intended, but the links are not actual links. What I really want is for them to be actual links with the select styling, using anchor tags:

<select class="custom-select"> # or class = form-control
        <a href>Choose a website</a> # same styling as options but actual links
        <a href="google.com">google</a>
        <a href="apple.com">apple</a>
        <a href="amazon.com">amazon</a>
</select>

I have found a way to do it, but it lags the page and is not as clean. Here's how I did it:

<div>
     <a href="#ChooseWeb" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle form-control">Choose a website</a>
           <style>
                 .form-control{
                  height: auto;
                 }
           </style>
                        <ul class="collapse list-unstyled form-control" id="ChooseWeb">
                            <li>
                                <a href="#">google</a>
                            </li>
                            <li>
                                <a href="#">neflix</a>
                            </li>
                            <li>
                                <a href="#">apple</a>
                            </li>
                            <li>
                                <a href="#">amazon</a>
                            </li>
                        </ul>
</div>

What is the most efficient and cleanest way to achieve this?

Answer №1

To create a dropdown menu using only a tags, you can utilize the bootstrap "Dropdown single button" component with "<a> elements". To customize the dropdown to resemble the bootstrap .custom-select class, simply copy the CSS styles of the .custom-select class from bootstrap and apply it to your dropdown menu. You can see an example here: https://jsfiddle.net/5nwjmt9h/1/

Below is the CSS style for .custom-select:

.custom-select {
   display: inline-block;
   width: 100%;
   height: calc(1.5em + .75rem + 2px);
   padding: .375rem 1.75rem .375rem .75rem;
   font-size: 1rem;
   font-weight: 400;
   line-height: 1.5;
   color: #495057;
   vertical-align: middle;
   background: #fff url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='4' height='5' viewBox='0 0 4 5'%3e%3cpath fill='%23343a40' d='M2 0L0 2h4zm0 5L0 3h4z'/%3e%3c/svg%3e") right .75rem center/8px 10px no-repeat;
   border: 1px solid #ced4da;
   border-radius: .25rem;
   -webkit-appearance: none;
   -moz-appearance: none;
   appearance: none;
}

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

The CSS file seems to be ineffective when building Vue in production

In my Vue project, I am using a single file component with a chart module. In order to customize the styles of the chart module's CSS, I created a custom CSS file in the assets folder where I added lines to override certain styles (such as changing th ...

Certain parts of the CSS grid are not aligning properly within the grid lines

I'm having trouble with my CSS grid. The red section fits in the first square, but the rest of the content seems to be out of place in the grid. I'm new to all this and I can't figure out what I'm missing. I've tried everything but ...

Creating CSS style rules for aligning table column headers in JavaFXML

Is it possible to style columns individually in a tableview? I have been attempting to make some column headers left-aligned and others right-aligned, as shown below: | hdr1 | hdr2 | hdr3 | hdr4 | In my css stylesheet, I experimented with the following ...

Is there a way to switch out the WordPress page title for an image instead?

I am trying to change up the appearance of my WordPress site by replacing the text on page titles with images. Working on a child theme locally, I need to customize each page individually as they will have different images. The current state is as follows ...

Is there a way to prevent automatically scrolling to the top of the page when submitting a form?

Resolved! By enclosing the input tag within an anchor tag, the issue has been resolved. The question may appear confusing at first, so let me provide some clarification. I created a mail form using PHP. This form is located at the bottom of the page. Whe ...

The selected jQuery plugin is not functioning properly within CodeIgniter framework

I recently downloaded the jQuery Chosen plugin to use the simple "multiselect" version on my website. I followed all the necessary steps and even copied and pasted the code into CodeIgniter. Despite my experience with jQuery, I am facing an issue where the ...

Storing the closed state of a pop-up box in localStorage for future reference

I'm struggling to get localStorage working properly on my website (). Here's what I need to achieve: A newsletter subscription pop-up on every page - this part is functioning correctly An option for users to click 'X' to close the po ...

What is the most efficient method to match a list of titles with a distinct list of their associated links using Beautiful Soup 4 (bs

Update: I've found the solution! list_c = [[x, y] for x, y in zip(titleList, linkList)] Initial inquiry: In my project to scrape a recipe website using bs4, I encountered a challenge where the titles and links were not directly paired. After extracti ...

Displaying the chosen array in a Material UI Table within a React application does not show the desired checkboxes

After days of hard work and research, I finally figured out how to achieve what I needed. In my React App, I have a Material UI table that I want to load with pre-rendered checks in the DOM based on entries in a selected array. The selected array contains ...

Protecting an AJAX interface against unauthorized exploitation by external websites

We are in the process of creating a website that utilizes a basic JSON API (RoR) for displaying information on the page. This data is accessible to our clients, but crucial to our service, so we are taking precautions to prevent competitors from accessin ...

What is the best way to extract a value from a string using JavaScript?

In my string labeled as 'content', I have the phrase "data-RowKey=xxx" embedded within it. I attempted to extract the 'xxx' portion using the code snippet below: var val = content.substring(12 + content.indexOf("data-RowKey="), 3); Un ...

Stop the print dialog box from causing the page to refresh

I have implemented a print button for an invoice: </script> <!--Function for printing invoice--> <script> function printpage() { window.print() } </script> <button id="print" name="print" class="btn btn-info" onClick="pri ...

Error: The function res.getHeader is not recognized within the Next.js API environment

I am currently working on a web application using NextJS 13, TypeScript, Next Auth v4, Prisma (using SQLite for development), and OpenAI. While accessing the API endpoint, I encountered an error in the console with the following message: error - TypeError ...

Issue encountered while serializing the `.product` object retrieved from the `getStaticProps` function in NextJS, in conjunction with

I ran into an error message saying "Error: Error serializing .product returned from getStaticProps in "/products/[id]". Reason: undefined cannot be serialized as JSON. Please use null or omit this value." This issue occurred while I was attempting to crea ...

How can I extract a substring from a URL and then save it to the clipboard?

Hi there! I'm working on a project for my school and could really use your expertise. I need help extracting a substring from a URL, like this one: URL = https://www.example.com/blah/blah&code=12432 The substring is: 12432 I also want to display ...

The dynamic drop-down menu is giving incorrect values when the onchange function is called

Trying to implement Google Analytics tracking on my dynamic dropdown menu in WordPress has been a bit tricky. I want to be able to track when users click on any of the options and display the name of the selected value, not just the ID. However, I've ...

Looking for assistance with a JQuery accordion component?

Struggling with integrating a JQuery accordion into my website. Despite following the correct steps, it's not functioning as expected. I have uploaded the site with some placeholder Lorem Ipsum text in the accordion. Any assistance would be highly app ...

"An unexpected compilation error (800A03EA) has occurred in the JavaScript syntax during

My computer is facing a frustrating compilation issue with JScript and node. Despite trying various solutions found online, the problem persists. After navigating to CD>PROJECT: npm init successful npm install -g globally and locally installed corr ...

Refresh the database records by clicking the submit button

Is there a way to update a database by entering text into a text box and submitting it, with the data being sent to the database with a specific id? The code snippet below outlines what needs to be done, but it seems to trigger a 403 error: Access forbid ...

Comparing Fluid-Framework to Signal-R: Understanding the Distinguishing Factors

Query: Recently, Microsoft unveiled the Fluid-Framework on GitHub. What are the steps to replace Signal-R with Microsoft's Fluid-Framework and what are the main differences? Scenario: It appears that Fluid supports collaboration and data synchronizat ...