using image as background instead of a button for dropdown menu

I have a dropdown already set up with the code below:

Note: The CSS class 'bg_r' is used for the background image (url) globe

<span class="bg_r">     
           
    Language: <a id="Lang" href="#">DropUp▲</a>
</span>
                <div id="language_menu" class="lang_switch_panel">
                    <div class="lang_switch">
                        <ul>
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Action</a></li>
                            
                        </ul>
                    </div>
                </div>

The output of the above code is displayed in figure 1 below:

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

Now, I'm trying to achieve something similar to the above using bootstrap 4. However, I am struggling to override the button style using a background image. I tried replacing the button type with an anchor tag, but it did not work.

Here is the code I have tried so far:

<span class="bg_r">
                 Language:
                 <button type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropup
                  </button>
            </span>
             <!--<span class="bg_r">Language:
            <a type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Dropup
              </a>
            </span>-->
            <div class="dropdown-menu">
                <!-- Dropdown menu links -->
                <a class="dropdown-item" href="#">Action</a>
                <a class="dropdown-item" href="#">Action</a>
                <a class="dropdown-item" href="#">Action</a>               
            </div> 

The output of the code using bootstrap is shown in figure 2 below:

https://i.sstatic.net/9vo8n.png

Any suggestions on how I can override the button type with a background image to achieve a similar output to figure 1?

Thank you in advance.

Answer №1

Apply the btn-link class to the button element

Also, include these CSS styles to center align it:

.bg_r {
  background-color: aqua;
  display: inline-flex;
  align-items: center;
}

Alternatively, add the classes d-inline-flex align-items-center to the bg_r span

.bg_r {
  background-color: aqua;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

<span class="bg_r d-inline-flex align-items-center p-1">
             Language:
             <button class="btn btn-sm btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false>
                Dropup
              </button>
        </span>

<div class="dropdown-menu">
  <!-- Dropdown menu links -->
  <a class="dropdown-item" href="#">Action</a>
  <a class="dropdown-item" href="#">Action</a>
  <a class="dropdown-item" href="#">Action</a>
</div>

You can also apply the bg-transparent class to remove the background-color from the button, while retaining focus styles on click. However, using just the btn-link class is recommended.

Answer №2

The solution provided above is effective, but another option is to make the substitution on this particular line

<button type="button" class="btn btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false>Dropup</button>

with

<a href="" class="btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropup</a> 

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

How can I make Div elements disappear based on conditions using If/else Statements in a JSP Page?

I've implemented a JSP function that manages user login by verifying their name and password input. However, even if the user enters incorrect information, they can still view the page. I want to hide all content on the page so that the user cannot se ...

Python code to retrieve historical data in CSV format from a website that mandates authentication

Despite my thorough online search, I have been unable to uncover a solution to my dilemma. Specifically, I am interested in downloading daily csv files of historical data from a sunnyportal website that requires login. The login page can be found here: ht ...

Hidden background image not shown

While working on a component in vue.js, I encountered an issue where the image is not being displayed within the specified tag: <b-icon v-if="displayShowHistory && checkRole('showHistory')" class="backlight show-modal-ic ...

Is it feasible to create a full-page text gradient clip without it repeating on each individual element?

.gradient { background-color: #00ccaa; background: linear-gradient(0deg, #00ccaa, #f530a9); background-size: cover; background-clip: text; box-decoration-break:slice; -webkit-background-clip: text; -webkit-text-fill-color: transparent; -web ...

Angular JS: Grabbing Text from a Specific div and Copying it to Clipboard

I recently developed a Random Password Generator using Angular JS. Here is how the output appears in the application: <div data-ng-model="password" id="password"> <input class="form-control" data-ng-model="password" id="password" placeholder=" ...

Adjust the Material UI Select component to accommodate the length of the label

Is there a way to automatically adjust the size of Material-UI's Select input based on the label? In the official demo, minWidth is added to the formControl element. However, I have multiple inputs and do not want to set fixed sizes for each one. Whe ...

Sending input values to controller action parameters in CakePHP

I am working on an HTML form in which I need the URL output to be structured as follows: "example.com/mycontroller/action/mike/apple" Upon submitting the form using "post" method, the form values are not visible and can only be accessed through "_POST" ...

Resolution of JSP/HTML Pages

Looking for guidance on adjusting the height and width of JSP/HTML pages to accommodate various monitor resolutions. How can I ensure that my page looks good and fits properly on all types of monitors? Any tips on setting height and width in a JSP would ...

Can JavaScript's innerHTML method capture <script> tags?

If you were to execute the following code: var data = document.getElementById('foobar').innerHTML; and within #foobar, there are <script> tags present, will those <script> tags be captured? UPDATE: Yes, they will. You can verify th ...

Start the initialization process of the Javascript code for Material Design WEB

Currently, I am utilizing Material Design WEB components in my project. Below are the CSS and JS files that have been included: <head> <link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="styl ...

(CSS) Unusual phantom white outline surrounding menu with no visible border

I've just finished creating my first multi-level drop-down menu, but I'm encountering two white border issues. The first white border appears at the bottom of the menu and seems to be related to the padding of the a-elements. The specific area in ...

Cleaning up HTML5 video content

I have been on the search for a solution that would allow me to "scrub" through HTML5 video. So far, I have not come across one and was considering developing my own. However, before diving into that process, I wanted to seek advice from the community here ...

Creating a seamless vertical marquee of several images that continuously scroll from bottom to top without interruption can be achieved by following these

Currently, I am seeking to design a mobile-friendly HTML page that features a border with multiple color images moving smoothly from bottom to top on both the right and left sides of the mobile screen. The transition should be seamless without any gaps o ...

Utilizing Bootstrap Columns for Image Alignment: Struggling to Position an Image in the Center

For my latest project, I used Bootstrap to create a template that adapts well to different device breakpoints. Everything is working smoothly except for the positioning of the arrows on the sides of the image. I want them to be centered halfway down the ...

How a child element in CSS can create a margin that spans the entire width of its parent

Constructing an overlay with a stylized container for text, but encountering an issue where the container is creating a margin that, when combined with the normal width of the elements, occupies the entire width of the parent element. Chrome dev tools indi ...

Retrieving user data from a client-side button using Node.js

I am currently in the process of developing a blog platform where users can create their own blogs and explore content from others. On a specific page, users can view a list of available blogs and access each one individually. To display this list, I am ut ...

What is the specific use of the second app.css file in Laravel?

I'm currently delving into Laravel 8, and I'm perplexed by the role of Resources\css\app.css. While every other component in the Resources directory serves a clear function, the app.css file appears to be empty. Even when I attempt to a ...

The dropdown feature is malfunctioning. (Using Angular 8 and Bootstrap)

I'm encountering an issue with displaying a dropdown and its options using the bootstrap directive 'ngbDropdown'. When I follow this example from the documentation: <div ngbDropdown class="d-inline-block"> <button class="btn ...

When using the JQuery click() function, the condition inside the if statement will only trigger after 2 clicks, while any code outside the if block will execute after

As I delve into learning JQuery, I've encountered a strange issue while trying to implement an if condition within the click() function. It seems that when I include an alert statement right before the if condition, the alert pops up on the first clic ...

Issue with navigational button layout

I am facing an issue while designing my navigation menu with 4 circular items spaced about 20 px apart on the top right of the screen. I have managed to style each nav item as a circle, but when I try to position them in the upper right corner, only the 4t ...