Steps to position a dropdown within a panel while keeping the dropdown content visible using an anchor tag

I'm struggling to display a dropdown menu within a panel without it being hidden. I was advised to use position:absolute in the dropdown's css, but that solution isn't working for me.

I need the dropdown to appear on hover over the anchor tag and also when clicking the button, without hiding any of the content.

<body id="body">
  <div class="col-lg-5 col-md-4">
    <div class="panel panel-default" id="panel2">
      <div class="panel-heading-fd">
        <h3 class="panel-title-fd">Our Products</h3>
      </div>

      <div class="panel-body">
        <div class="media">
          <a class="pull-left" href="#">
            <img class="media-object" src="img/2.jpg" style="width: 150px; 
     height: 70px;">
          </a>
          <div class="media-body">
            <h4 class="media-heading"><a href="#">Dropdown</a></br>
              <div class="dropdown">
                <button class="btn btn-default dropdown-toggle" type="button" data- toggle="dropdown" data-hover="dropdown">
     Dropdown <span class="caret"></span>
     </button>
                <ul class="dropdown-menu">
                  <li><a href="#">Action</a></li>
                  <li><a href="#">Another action</a></li>
                  <li class="dropdown">
                    <a href="#">One more dropdown</a>
                    <ul class="dropdown-menu">
                      <li><a href="#">Action</a></li>
                      <li><a href="#">Another action</a></li>
                      <li class="dropdown">
                        <a href="#">One more dropdown</a>
                        <ul class="dropdown-menu">
                          ...
                        </ul>
                      </li>
                      <li><a href="#">Something else here</a></li>
                      <li><a href="#">Separated link</a></li>
                    </ul>
                  </li>
                  <li><a href="#">Something else here</a></li>
                  <li><a href="#">Separated link</a></li>
                </ul>
              </div>
            </h4>
            Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum <a href="">Read More</a>
          </div>
        </div>
      </div>
    </div>
  </div>

To see a complete example, check out this fiddler link: https://jsfiddle.net/td3ogahk/11/

Answer №1

Make sure to include the following CSS in your dropdown menu styling:

.dropdown-menu {
   position: fixed;
   top: auto;
   left: auto;
}

Instead of using position absolute, switch to position fixed for improved alignment.

Don't forget to enclose the text after the dropdown in a p tag. It's best practice to always wrap text in a paragraph tag:

<p class="text">Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum  <a href="">Read More</a></p>

Hopefully, this advice proves to be useful to you!

View the codepen link here for a visual example.

ADDITIONAL TIP: To automatically close the dropdown when scrolling, implement the following JavaScript code:

jQuery(window).on('scroll',function() {
  jQuery('.dropdown.open').removeClass('open');
});

If you want the dropdown to also work for the link tag, make sure to replicate the structure you have for the button beneath it. Otherwise, the link will function as a standard hyperlink.

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

A division element continually broadens to its maximum width, regardless of whether there is enough space to accommodate additional elements on the same

Whenever the browser window is resized while displaying the code provided below, I am facing an issue where the div expands to the max-width even when I do not want it to do so. When everything fits on a single line, the layout appears fine. However, upon ...

What is a method to adjust the height of a paragraph element in AngularJS based on user interaction?

Is there a way to dynamically adjust the height of a paragraph element in Angular when clicking on a "Show More" button? I want the button text to change to "Show Less" and shrink the paragraph back down when clicked again. What would be the most effective ...

The integration of ag-grid webpack css is not reflecting on the website as intended

I'm facing an issue with ag-grid in my react application where I can't seem to get the CSS working properly with webpack. The grid is currently displaying like this: image: https://i.sstatic.net/RPKvH.png const path = require("path"); var webp ...

I would like a div element to slide up from the bottom of the page

Could someone please assist me in creating a popup div that appears from bottom to top when a specific button is clicked? I would like the div to be fixed without affecting the overall page content. Any help would be greatly appreciated. Thank you! $( ...

What causes the width of unrelated cells to change when the contents of colspan'd cells expand in IE7?

Before we delve into the details, I invite you to take a look at this fiddle using IE7. It's quite intricate and not something I want to repeat here. The main issue is that clicking on the red block should display additional information. The top row ...

"Effortlessly create a disappearing effect for your Bootstrap modal: Fade in upon opening, and instantly

When it comes to Bootstrap 3 modals, I have a question regarding the fade effect. On my outer modal div, I am using class="modal fade" to achieve the default fade in/out effect. However, what I really want is for the modal to fade in when opened, but disap ...

Instructions for utilizing img.shape() in Javascript

import cv2 open image file img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED) fetch image dimensions dimensions = img.shape image details height = img.shape[0] width = img.shape[1] channels = img.shape[2] print('Image Dimensi ...

Preventing default CSS styles in ASP.NET: A step-by-step guide

When working with multiple CSS files for different themes, I noticed that the Login Page is calling one extra CSS file. However, during runtime, some classes from the default CSS are still being used. How can I resolve this issue? ...

Apply a chosen style to the element that was clicked on, while removing the style from the rest

Here is an example of my ul li structure: <div id="categoryTree"> <ul> <li id="cat_15"> <a class="hasSubCat" href="javascript:void(0);"><img src="images/icons/folder.gif" border="0" alt="Folder" title=" Folder ">N ...

Change the JavaScript code to output HTML rather than just plain text

I've created a small plugin for tinymce4 that adds an additional dropdown menu with a list of headers (e.g. h1, h2...). The issue I'm facing is that I'm trying to display these header elements with their corresponding styles (e.g. <h1> ...

PHP and MySQL form is not being updated with new data

In my database, the fields include: id,name,email_id,address,phone_no,username,password,category,date <?php include_once('connect_to_mysql.php'); if(isset($_POST["submit"])){ $a=mysql_real_escape_string($_POST["name"]); ...

What is the best way to transition to a new page within my application with React Material UI Select?

I am looking to implement a feature where a new page is displayed based on the selection made in a Material UI Select component. Below is the code for SelectFoodChange.js : import * as React from 'react'; import InputLabel from '@mui/materi ...

Executing 'npm start' to launch an HTML file along with a CSS stylesheet - A step-by-step guide

As a newcomer to the world of coding, I've found that asking questions on platforms like Stack Overflow enables me to connect with fellow developers. I usually code using VS Code and rely on the "live server" extension to quickly test my code. Howeve ...

Update the min-width for col-xl @media in Bootstrap 4 by modifying the HTML styles exclusively

Is there a way to set the min-width for .col-xl to 1600px in @media using only inline HTML styling, without having to modify any Less or Sass files? I simply want to keep Bootstrap's official CDN URLs for loading the library. ...

Enhancing Time Precision in CakePHP: Introducing Quarter-Accurate Time Input

I'm currently implementing time inputs using the following code snippet: $form->input("time") However, the select boxes generated have minute-accuracy, which is unnecessary for my project. Is there a way to restrict the options in the select lis ...

How can we efficiently load a JSON file from a local path into an HTML document in a way that is compatible with all browsers?

I am attempting to retrieve JSON data from a local path and display it on a basic HTML page. I have tried various methods such as fetch, ajax in order to load the data and update the corresponding values on the web page. The goal is to host this page so t ...

Every time I refresh the page, new data is inserted into the MySQL database

After clicking the button, I expect data to be inserted into my MySQL database. However, it only inserts the data when I manually reload the page. Below is the code snippet: <script> function minuscredits() { alert("hah"); <?php mysql ...

Tips on effectively showing padding and margins in django templates and minimizing empty space between rows using bootstrap

Still fairly new to the world of html, Django, and Bootstrap. I find myself struggling with grasping the concept of padding and margins. Is there a clever trick or CSS technique to visually display the padding and margins? I've noticed that margins do ...

The persistent issue of the modal box disappearing persists, despite my efforts to remove additional instances of bootstrap.min

I have been struggling to prevent my modal box from disappearing, despite trying various solutions found online. How can I ensure that it stays visible? Here is the code in question: <html> <head> <title> Test Slides </titl ...

Issue with Bootsrap 4 Dropdown Menu Displaying Incomplete Links

There seems to be an issue with the Bootstrap 4 dropdown menu not displaying all the links. Please refer to the image below: https://i.sstatic.net/ZS2t2.png The dropdown menu is not showing beyond the red band. I can't seem to figure out what I&apos ...