CSS triangle dropdown menu that stretches to 100% width and is positioned relative to its parent

Currently, I am working on a menu that has drop-down items that appear when the user hovers over them.

Here is what I am aiming for:

  • I want the dropdown to be contained within the parent element so I can use the :hover pseudo-class to show it.
  • The dropdown should span 100% of the window width, not just its parent's width.
  • I do not want the contents of the dropdown to be positioned relative to the parent menu item.
  • The CSS triangle should point towards the middle of the parent menu item.
  • I also want to add a CSS transition to smoothly fade in the menu and move it into position.

For reference, here is a quick layout similar to what I am working on: http://jsfiddle.net/kn3Z3/2/

In attempting to keep the dropdown inside the parent element, I set the parent element's position: static. However, this presents an issue as I now lack a reference point to position the CSS triangle.

I cannot hard code the position of the dropdown for each menu item due to the dynamic nature of the menu and changes in font size with media queries (as seen in the fiddle).

I have explored other solutions where the triangle was integrated into the parent menu item itself. This approach hindered the animation synchronization with the menu and led to poorer performance.

If anyone has any ideas on how to achieve this or if you believe this animation method is not feasible, please share your thoughts.

As a side note, I also tested the 100% width solution mentioned here: Dropdown Menu - Make the <ul> submenu 100% width; however, I require the contents of the dropdown to not align relatively to the parent menu item.

Answer №1

html,
body {
  font-family: "Fira Sans", sans-serif;
}

.nav {
  display: flex;
  position: relative;
}
.nav div {
  padding: 6.25px 12.5px;
  background-color: #ddd;
}

.item {
  position: relative;
}
.item:hover:before {
  content: "";
  border-right: 16px solid transparent;
  border-left: 16px solid transparent;
  border-bottom: 16px solid #ccc;
  position: absolute;
  bottom: -16px;
  left: 50%;
  transform: translateX(-50%);
}
.item:hover .subnav {
  opacity: 1;
  transform: scale(1);
}

.subnav {
  position: fixed;
  top: 60px;
  left: 10px;
  right: 10px;
  background-color: #ccc;
  opacity: 1;
  transform-origin: top left;
  transform: scale(0);
  transition: opacity 0.2s ease-out, transform 0.2s ease-out;
}
<div class="nav">
  <div class="item">
    <div>Item 1</div>
    <div class="subnav">
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
      </ul>
    </div>
  </div>
  <div class="item">
    <div>Item 2</div>
    <div class="subnav">
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
      </ul>
    </div>
  </div>
  <div class="item">
    <div>Item 3</div>
    <div class="subnav">
      <ul>
        <li>Item 1</li>
      </ul>
    </div>
  </div>
</div>

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 hideCol method does not work on jqGrid

Encountering a problem with the hidecol method in jqGrid. Despite calling the method, nothing seems to happen. Using version 4.5 of jqGrid. The table is created as follows: // Table creation using jqGrid $('#shipTable').jqGrid({ data: tabl ...

Managing the reset functionality in Orbeon XForms when the escape character is triggered twice

I've noticed a peculiar issue with IE 5 - when the escape character is pressed twice, all form fields are automatically reset. This behavior does not occur in Mozilla. To address this, I've added a simple JavaScript alert that notifies the user w ...

What is the best method for adding a line break in the body of an email when we include the recipient, subject, and message?

Do you have a requirement where information needs to be sent to an end user via Gmail (preferred) and they need to click on a button in the email body to respond? The button response is set up with predefined to, subject, and body fields, but there are dif ...

Is it possible to execute a server-side click on a div element?

I have a div with an image and name inside. How can I make this div trigger a server-side click event without changing its structure? Here is my code: <div class="tutorial" style="margin-left:5px;"> TUTORIAL<div class="firstico" style=" ...

Encountering an error in a Javascript function: Property 'style' is unreadable because it is undefined

When attempting to run this Javascript function, an error message appears stating, 'Cannot read property 'style' of undefined at showSlides' var slideIndex = 1; showSlides(slideIndex); // Next/previous controls function plusSlides(n) ...

involving a variable in css styles using javascript

Is it possible to set a margin-left using a variable in JavaScript? If so, how can this be accomplished? The margin left should be equivalent to the clientWidth of the text: let hoverBubble = document.getElementById(id); let linkWidth = link.clientWidth ...

Increase the spacing at the top of an image placed within an HTML table

Here is my HTML structure showcasing details within a table: <table spacing="15"> <tr> <td> <img src="Images/user1.png" /> <label> User</label> </td> ...

jQuery Slider not appearing on the page

I am attempting to incorporate the Basic jQuery Slider into my website. Below is the code I have used: For HTML file: <div id="slides"> <ul class="bjqs"> <li><a href="somelink"><img src="someim ...

When uploading a file using the file input in a table row, the text input will only appear in the first row and not in

I have a new application available for testing: VIEW APPLICATION Kindly follow the steps outlined below: Click the Add Question button twice to add 2 file inputs to the table below: Using the file input in TABLE ROW 1, upload 2 images (one at a time ...

The CSS focus-within and hover properties are not functioning as expected

How can I make the search tags visible (the buttons above the text input field) when the search bar below them is clicked? I've tried using :hover, :focus, and :focus-within, but none of them seem to be working. Can someone help me figure out the issu ...

Simply upload a file and send it to a specific URL by using the drag and drop functionality in HTML

After successfully implementing the drag and drop functionality, my next challenge is figuring out how to upload and post the file to a URL with specific parameters. Usually, if I had a form available, I could do it like this: <form enctype="multipart ...

Typescript input event

I need help implementing an on change event when a file is selected from an input(file) element. What I want is for the event to set a textbox to display the name of the selected file. Unfortunately, I haven't been able to find a clear example or figu ...

Utilize LESS Bootstrap as a guide for incorporating content

I have been struggling to properly integrate Bootstrap into my LESS file. Currently, I am adding Bootstrap to my project using NPM. If I simply use the following import statement in my LESS file: @import (reference) 'node_modules/bootstrap/less/boot ...

Encountering Problem Importing HTML Table Data into R

I am looking to import the data table located at the bottom of a specific webpage into R, either as a dataframe or table. The webpage in question is . Initially, I attempted to utilize the readHTMLTable function from the XML package: library(XML) url <- ...

Is it possible to target the sibling of the currently selected or focused element with CSS?

I'm attempting to add button functionality to show and hide errors using CSS. By clicking the CSS button or link, I can target the siblings of the focused element as shown below. This method is only effective in IE8+ browsers. #ShowErrors:focus + a ...

When importing both shared-lib and MUI in my ReactJS application, I noticed that my custom MUI styles were being overwritten by the MUI styles

I have developed a shared library using React and Storybook. Within this library, I am utilizing the MUI V5 Button and making some style modifications using CSS modules. I executed the "build&pack" script in my library, which generated a tgz package that ...

Adjust mouse coordinates to be relative to the coordinates of the <canvas> element

I'm currently facing the challenge of determining the exact location of the mouse on a canvas grid while still maintaining resizability. As of now, I have the mouse coordinates based on its position on the screen (x and y). The issue arises from the ...

Tips on customizing the selected icon color in Material-UI's BottomNavigationAction styling

I'm facing an issue with Material-UI styled components and could use some assistance. My goal is to create a BottomNavigation bar in React using Material-UI v5 where the selected option's icon displays in red (#f00) while the unselected icons sho ...

Can display-inline-block support top margin and text alignment?

Can you please explain if the top margin and text-align properties work for both display: inline and display:inline-block? If they do, could you provide some insight as to why? ...

Leverage jQuery for dynamically changing CSS transform properties

Is it possible to update CSS transform values using jQuery with a click event? If not, does anyone have any suggestions on how I could achieve the same effect? I attempted the following code but it did not work for me... $('.reset').on('c ...