Position dropdown elements using absolute alignment

I am attempting to replicate the drop-down menu style used on Twitter. Here is an example of what I am trying to achieve:

(Click on the language option).

To accomplish this, I have implemented the following HTML:

<a href="#language">English</a>
<div class="popup" id="language">
  <ul>
    <li>Portuguese</li>
    <li>English</li>
    <li>French</li>
  </ul>
</div>

Example: http://codepen.io/mdmoura/pen/upIjv.

I have two main objectives:

1 - Make sure the drop-down menus line up properly with their respective links;

2 - Allow for the option to align the drop-down menus to the left or right of their triggers.

Does anyone have any suggestions on how to accomplish this?

Thank you, Miguel

Answer №1

To make your popups work correctly, add the class 'relative' to your triggers (.popup{ position: relative; } and assign the class 'absolute' to the dropdowns (.popup ul{ position: absolute; }). This will allow you to position the drop downs within the boundaries of the triggers using properties such as left:, right;, etc.

Answer №2

To utilize absolute positioning, you need to ensure that the popup divs are placed inside the a tags.

  <a href="#language">English
  <div class="popup" id="language">
    <ul>
      <li>Portuguese</li>
      <li>English</li>
      <li>French</li>
    </ul>
  </div>
  </a>

Afterwards, you can position the a tags accordingly.

.wrap a {
  position: relative;
}

Next, adjust the positioning of the pop-up divs.

.popup {  
  border: 1px solid red;
  display: inline;
  display: none;
  background-color: #D0D0D0;
  color: #404040;
  position: absolute;
  padding: 0; 
  margin: 0;
  top: 100%;
}

Demo on Codepen

Answer №3

Give this a shot:

$('button').on('click', function (event) {
    $('.modal').hide();
    var modal = $($(this).attr('data-target'));
    var position = $(this).position();

    $(modal).toggle();
    $(modal).css({
        'left': position.left,
        'top': position.top + 10
    })
    return false;
});

See the demo here

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

Ensure to close the Ajax request from PHP before the script finishes executing

Is there a way to terminate an Ajax request from PHP before the script finishes executing? For instance, if a user requests php.php and it includes the line 'echo "phpphp"', how can we ensure that the Ajax request is completed with the data "phpp ...

How to invoke a Struts 2 action synchronously using JavaScript

I've been attempting to use JavaScript to make a synchronous call to a Struts 2 action. Despite finding multiple examples, none of them have worked for me. The only method that has worked so far is making an asynchronous call like this: function togg ...

Can a fixed position element disregard its parent?

I've been experimenting with a div that switches between position: absolute and position:fixed on scroll. You can view the issue with my code on JSFiddle: http://jsfiddle.net/g9NVj/2/ The problem areas are the pink and blue boxes that change color as ...

Calculate how frequently an element showcases a particular style

I attempted to tally the occurrences of a specific class on an element, but I keep encountering the error message: $(...)[c].css is not a function Does jQuery have it out for me? Below is the code snippet in question: // hide headings of empty lists le ...

showing text from chosen selection with an additional element included in the output

I could use some assistance with this code snippet. My goal is to showcase the options in the format: "Fried Rice = 10.000" as the outcome. However, the issue I am facing is that the select option box also contains the price. What I actually need is for th ...

Insert a new column at the start of the data table

Hey there! I'm currently sending my row data to a datatable using Json. My goal is to have the first column of the table always display a checkbox, with the following columns showing the data from the Json response. Is there a way to make my datatable ...

Vertical alignment with flexbox not functioning properly in Internet Explorer 11

I am attempting to use flex to center an icon within two divs. Below is the code snippet: .div-1 { width: 50px; height: 50px; display: flex; border-radius: 50%; background-color: #228B22; } .div-2 { margin: auto; } i { color: #ffffff; } &l ...

The second jQueryUI datepicker instance flickers and vanishes when the show() function is triggered

I currently have two jQueryUI datepickers integrated into my webpage. The initialization code for both is as follows: jQuery("#departureDate").datepicker({ beforeShow: function() { getDatesForCalendar("outbound"); }, numberOfMonths: 3 ...

Images that adapt to different screen sizes

I'm not seeking a revolutionary way to handle the images, as I have already consumed a lot of information about loading images. My focus is on basic responsive design, where I aim to load a large image and compress it as the window size reduces. Bel ...

Straining data in text input fields using bootstrap

Looking for a way to filter data with bootstrap/jquery without using tables, just div rows and columns. Check out an example of how my form looks Here's a snippet of my code: <div class="row"> <div class="col-md-4"> <input class= ...

Having trouble with $.post request to a different domain in a node.js/express app running on port 8081

Whenever I try to use $.post, I keep getting the error message POST https://thewebsite.com 400 (Bad Request). Here is the code snippet that's causing the issue: $.post("https://website.com/blabla", { domain: "infoinfo.com", room: "someInfo", ap ...

The navigation bar expands in height as the media width changes

I have a rather straightforward navbar: <nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header pull-left"> @Html.ActionLink("Contact ...

Exploring the Depths of HTML with Jquery

This is an example of HTML code I am working with: <tr> <td> <div><span id="temp" /> </div> </td> </tr> <tr> <td> <div><span id="temp" /> </di ...

Opening a dialogue box upon clicking a table row

I have a table in HTML where I want a dialog box to appear when a row is clicked. To update my database, I am using an AJAX call like this: $(document).ready(function() { $('.myrow').click(function () { $("#dialog").dialog({ ...

Blank vertical gap among columns within Bootstrap 4

Here is the current HTML setup: <div class="container"> <div class="row"> <div class="col-lg-9 description"></div> <div class="col-lg-3 sidebar"></div> <div class="col-lg-9 comments"></div& ...

"Enhance security with the choice of NFC login on our web-based

One of the features I am considering implementing in my web system is the ability for staff to log in by swiping a card against an NFC scanner, as opposed to the traditional username and password method. This would allow for a seamless and quick authentica ...

Accessing XML files locally via JavaScript on Chrome or Internet Explorer, with compatiblity for Android mobile devices as well

Looking to extract and display data from an XML file directly in an HTML page without the need for a web server. Ready to dive into using JavaScript, jQuery, or Ajax to get the job done. ...

What is the best way to instruct jQuery to disregard an empty server response?

After examining this piece of code: $.ajax({ type: "POST", url: theRightUrl, data: whatToPost, logFunction: whatever, suppressSuccessLogging: !0, dataType: "html" }); I encountered an issue where Firefox displays a "no element ...

Enhance your Morris.js charts by incorporating detailed notes and annotations

Is there a way to include annotations in my morris.js charts? I couldn't find any information about this on their official website. I specifically need to add notes to certain dates. ...

Sticking Table Headers in Bootstrap 4

I'm struggling with making the header of my Bootstrap 4 table fixed/sticky so that it doesn't scroll along with the content. I've tried adding custom CSS but haven't been successful so far. Here's a snippet of the code for my tabl ...