Show a concealed dropdown menu within a CSS grid column utilizing clip-path styling

Working on a Django admin header that makes use of CSS grid to create two columns. I have added a JavaScript dropdown effect to the user icon for displaying options like "Change Password" and "Log Out". However, encountering an issue where the dropdown remains hidden inside the column and does not show outside.

Important to note that the drop-down is contained within a container with clip-path: polygon applied.

Seeking suggestions on how to resolve this issue?

Thank you in advance,

A newcomer to web development

Images linked below provide visual context of the described situation: https://i.sstatic.net/fMOnT.png

Header height adjustment to display the dropdown: https://i.sstatic.net/xvJX8.png

Partial Django code snippet provided below:

{% load i18n static %}<!DOCTYPE html>
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
{% block extrastyle %}{% endblock %}
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}">{% endif %}
{% block extrahead %}
    {{ block.super }}
    <script>
        /* Dropdown functionality */
        function openDropdown() {
          document.getElementById("myDropdown").classList.toggle("show");
        }
        // Close dropdown on outside click
        window.onclick = function(event) {
          if (!event.target.closest('.dropbtn')) {
            let dropdowns = document.getElementsByClassName("dropdown-content");
            let i;
            for (i = 0; i < dropdowns.length; i++) {
              let openDropdown = dropdowns[i];
              if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
              }
            }
          }
        }
    </script>
{% endblock %}

Django header code excerpt:

<div id="header">
        <div id="branding">
        {% block branding %}{% endblock %}
        </div>
        {% block usertools %}
        {% if has_permission %}
        <div id="user-tools">
            {% block welcome-msg %}
                {% trans 'Welcome,' %}
                <strong>{% firstof user.get_short_name user.get_username %}</strong>.
            {% endblock %}
            {% block userlinks %}
{#                {% if site_url %}#}
{#                    <a href="{{ site_url }}">{% trans 'View site' %}</a> /#}
{#                {% endif %}#}
                {% if user.is_active and user.is_staff %}
                    {% url 'django-admindocs-docroot' as docsroot %}
                    {% if docsroot %}
                        <a href="{{ docsroot }}">{% trans 'Documentation' %}</a> /
                    {% endif %}
                {% endif %}
            <div class="dropdown">
                <button onclick="openDropdown()" class="dropbtn"><img src="{% static "admin/img/user.svg"%}" alt="User Menu" style="height: 30px;"></button>
                <div id="myDropdown" class="dropdown-content">
                    {% if user.has_usable_password %}
                    <a href="{% url 'admin:password_change' %}">{% trans 'Change password' %}</a> /
                    {% endif %}
                    <a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a>
                </div>
            </div>
            {% endblock %}
        </div>

CSS styling for the dropdown menu:

/* Navbar Dropdown */

.dropbtn {
  background-color: #0071ce;
  color: white;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #005ba6;
}

.dropdown {
  position: relative;
  display: block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
  z-index: 1;
  right: 0.5rem;
}

.dropdown-content a {
  color: black !important;
  padding: 1rem 1rem !important;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

Answer №1

To properly display dropdown content, it is important to position it absolutely and ensure it has a higher z-index than the content it overlays.

Sharing a codepen or similar demo would greatly assist in providing assistance.

Answer №2

I have pasted and commented your code in the following snippet, removing overflow: hidden; from #header. It appears to be working fine, doesn't it?

/* Function for toggling dropdown visibility */
function openDropdown() {
const myDropdown = document.getElementById("myDropdown");
const show = document.getElementById("myDropdown").classList.toggle("show");

if(show){
const {top, left, width} = myDropdown.getBoundingClientRect();
myDropdownClone = myDropdown.cloneNode(true);
myDropdownClone.style.width = `${width}px`;
myDropdownClone.style.top = `${top}px`;
myDropdownClone.style.left = `${left}px`;
myDropdownClone.style.position = 'fixed';
myDropdownClone.id = 'myDropdownClone';
document.body.append(myDropdownClone);
} else {
document.getElementById("myDropdownClone").remove();
}

}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.closest('.dropbtn')) {
let dropdowns = document.getElementsByClassName("dropdown-content");
let i;
for (i = 0; i < dropdowns.length; i++) {
let openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
#header {
width: auto;
height: auto;
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 40px;
background: #417690;
color: #ffc;
/* overflow: hidden; */
clip-path: polygon(5% 0%, 100% 0%, 100% 100%, 0% 100%);
}

#header {
background: #FFF;
-moz-box-shadow: 0 3px 3px 0 rgba(148, 148, 148, 0.5);
box-shadow: 0 3px 3px 0 rgba(148, 148, 148, 0.5);
padding: 0;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-areas: "logo navbar";
}

.dropbtn {
background-color: #0071ce;
color: white;
/* padding: 16px; */
font-size: 16px;
border: none;
cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
background-color: #005ba6;
}

.dropdown {
position: relative;
display: block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
/* overflow: auto; */
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
z-index: 1;
right: 0.5rem;
}

.dropdown-content a {
color: black !important;
padding: 1rem 1rem !important;
text-decoration: none;
display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
<div id="header">
<div id="branding">

</div>

<div id="user-tools">

<strong>My name</strong>.

<a href="#">View site</a>
<a href="#">trans Documentation</a> /

<div class="dropdown">
<button onclick="openDropdown()" class="dropbtn"><img src="" alt="
User Menu" style="height: 30px;"></button>
<div id="myDropdown" class="dropdown-content">

<a href="#">Change password</a> /
<a href="#">Log out</a>
</div>
</div>
</div>
</div>

[EDIT] I have made adjustments to the code snippet considering the

clip-path: polygon(5% 0%, 100% 0%, 100% 100%, 0% 100%);
mentioned in the comments

Answer №3

After conducting thorough research, it has been determined that the most effective strategy for incorporating a dropdown within a container utilizing a clip-path is to introduce an extra div element, as exemplified in this insightful solution: Discover the method for positioning a dropdown div over a clip-path 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

The AJAX call in PHP is echoing JavaScript code that seems to be malfunctioning

Currently working on an AJAX page using php, where I plan to output a section of JS code upon completion. Despite having the JS code within the HTML page, it is not functioning properly. Any suggestions on how to get it to work? ...

Successful AJAX Post request functioning exclusively within the Web Console (Preview)

When a user clicks on an element on the website, I have a simple AJAX request to send the ID of that element. However, the script only works in the Web Console under the Network -> Preview section and this issue occurs across all browsers. Here is the ...

Tips for transferring an array between two applications using AngularJS

I have two applications, appA for front end and appB for admin end. In appA, I am building an array called queries using a service through a controller. However, when I try to retrieve this array list in a controller in appB, it appears empty. Everytime ...

Experiencing difficulties in transmitting images/files to API through reactjs and Material UI upload component

Recently, I tackled the task of creating an image upload component by utilizing an upload component from Material UI. While I have experience with this process using a simple HTML file input in the past, I found myself feeling a bit perplexed this time aro ...

What is the significance of [Function: bound ] in the context of node debugging?

So I was trying to debug my .js file using node debug and when I used catch(error) { It only displayed [Function: bound ] when I tried console.dir(error) What is causing this issue? How can I access the full error object? And how do I retrieve the stack ...

The reason behind the successful execution of the final .then in promise chaining

I am new to JavaScript and have encountered an interesting problem with the following code. Even though I haven't returned anything from the .then method, the last .then (blue color) works instead of the first one (red color). Can someone explain why ...

Using the <video /> tag on a Next.JS page generated on the server side leads to hydration issues

My Next.js app's index page is rendered on the server side by default. During development, I encountered an error that stated: Unhandled Runtime Error Error: Hydration failed because the initial UI does not match what was rendered on the server. Wa ...

Tips for implementing jQuery overlay to display radio buttons in a popup window

Following a tutorial, I created an alert window with radio buttons added for user input. You can view the online demo here. The modified source code with the radio buttons is provided below. Below you'll find where I added the radio buttons and how I ...

Instead of using colons, display the separation of hours, minutes, and seconds with underscores

Currently, I am utilizing moment.js to retrieve the present date and time with the intention of formatting it in the specific format below 'MMMM Do YYYY, h:mm:ss a' Unfortunately, the problem arises as the delineation between hours, minutes, and ...

Utilizing multiple instances of date picker components in React.js

Is there a way to update the date on react-datepicker when using multiple instances of the datepicker component? I have encountered an issue with this. Here is the code for the Date picker Component: <DatePicker selected={this.state.from} onChange ...

Once a value is saved in a Positive Integer field in Django Models, it cannot be edited at a later time

I am currently developing a Django project where I have a positive integer field in the models.py file. My goal is to ensure that this field becomes unchangeable once it has been saved, and cannot be edited after saving the value. Here is an example code ...

Sending a massive video file to a node.js server and saving it in AWS S3 with ReactJS

I am currently in the process of creating an OTT platform, but I have encountered a problem while attempting to upload large files to the server. I initially attempted to store the file in a temporary folder using multer and then utilize aws-sdk s3.upload. ...

Transferring session data through AJAX in PHP

I'm currently developing an app using PhoneGap. However, PhoneGap only supports HTML, CSS, and JS, not PHP. This led me to the workaround of placing the PHP file on a remote server and using AJAX to call it via the server's URL. My issue now is ...

Does anyone have any tips on how to configure django-registration to use email addresses as usernames?

Currently in the process of setting up email registration activation for my upcoming site launch. I have begun exploring the django-registration option, but am also open to other email activation systems within the django framework. The main requirement f ...

Using jQuery Modal Dialog with ASP.NET MVC 2

In my ASP.NET Mvc 2 application, I have a grid of user info. When a user is clicked, a jQuery Modal dialog opens allowing me to edit and save the user's information successfully. I am now looking for assistance on implementing validation on this moda ...

What is the procedure for adjusting the padding in Material UI's datepicker?

Click here to access the codesandbox link function InlineDatePickerDemo(props) { const [selectedDate, handleDateChange] = useState(new Date()); return ( <Fragment> <MuiPickersUtilsProvider utils={DateFnsUtils}> <Keyboa ...

Interactive Django table using AJAX

This marks the second question in a series regarding my Django project. The code utilized here contains sections borrowed from this post: Stack Overflow The goal is to implement a dynamic table that cycles through objects in a list (currently set at an in ...

Is it possible to analyze the performance of NodeJS applications using Visual Studio Code?

I have managed to establish a successful connection between the VS Code debugger and my remote NodeJS target through the Chrome protocol. I am aware that this protocol allows for profiling and performance measurements within the Chrome Dev Tools, but I am ...

Tips for successfully transferring values or parameters within the Bootstrap modal

How can I create a cancel button that triggers a modal asking "Are you sure you want to cancel the task?" and calls a function to make an API call when the user clicks "Ok"? The challenge is each user has a unique ID that needs to be passed to the API for ...

What causes the "v-col" width to suddenly alter after adding the "bg-red rounded" class?

I'm struggling to organize the data into blocks using Vuetify. When I apply the "bg-red rounded" class to the "v-col", it alters the width of the column, resulting in an undesired grid structure. Here is the template section of my Vue file. Some data ...