What are some methods to avoid clipping absolute positioned elements while maintaining a box shadow around the parent div?

Here is an example to illustrate my question: Example Code

The code provided above is a simplified version of my actual code. I am facing an issue where setting the wrap-div to overflow: visible prevents the menu from being cut off, but it also causes the box shadow not to encompass the box. On the other hand, using overflow:auto; makes the box-shadow work, but the menu gets cut off. How can I find a solution to this problem without specifying a fixed height?

Sample Code:

$('#menu').click(function() {
  $('#menu-list').toggleClass('hidden');
});
#wrap {
  width: 80%;
  height: auto;
  overflow: visible;
  box-shadow: 0 0 .2rem rgba(0, 0, 0, .4);
}

#content {
  width: 200px;
  height: 20px;
  margin: 0 auto;
}

#content2 {
  float: left;
}

.hidden {
  display: none;
}

#menu {
  position: relative;
  height: 20px;
  width: 100px;
  background-color: #ccc;
  float: left;
}

#menu-list {
  position: absolute;
  top: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="wrap">
  <div id="content">
    Some Content
  </div>
  <div id="content2">
    Some Content
  </div>
  <div id="menu">
    Open Menu
    <div id="menu-list" class="hidden">
      <div> bla </div>
      <div> bla </div>
      <div> bla </div>
    </div>
  </div>
</div>

Answer №1

To solve your specific issue, follow these steps:

1- Delete the overflow: auto; property from #wrap

2- Insert the following code into your CSS:

#wrap:after {
  display: table;
  content: "";
  clear: both;
}

By adding this, the height of #wrap will consider the floated element.

If you have multiple instances, create a class such as clearfix and apply it where necessary.

See demonstration: https://jsfiddle.net/85uqehz5/2/

Answer №2

To fix the issue of a cut-off menu in your website, make sure to clear floats using this code snippet: https://example.com/clearfloats

<div id="wrapper" class="clear-floats">

If you are facing an issue where your menu is not displaying properly, it could be due to missing clearing floated elements such as float: left, you can resolve it by adding the following code to your container:

.clear-floats:after {
    content: "\0020";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}

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

Preventing image overlap in Bootstrap when window size is reduced

Here is the code snippet: <div id="members"> <div class="container-fluid"> <div class="row"> <div id="members" class="col-md-12 text-center"> <h3>Text</h3> &l ...

Using Vue 2 with Webpack works seamlessly for one application but encounters issues when used for another

I've been struggling with this problem for days now. I'm working on a project that consists of two single-page Vue applications or a two-page application as some might call it. These apps have common functionalities which have been extracted into ...

Failing to send contact information using JSON in PHP

I attempted to transmit JSON data to PHP for email processing, but encountered an issue where the process veered into the 'else' condition during debugging. Here is the code snippet: HTML <form id="cbp-mc-form" class="cbp-mc-form" method="po ...

Writing this SQL query in Sequelize

I am attempting to write this SQL query in my Sequelize controller, but I need to ensure that the data_time is within the last 30 days. SQL: SELECT * FROM bankapplication_transactions WHERE id_sender = 1 OR id_recipient = 1 AND data_time BETWEEN NOW() - ...

SweetAlert: The title argument is not present

I encountered an error when the error function is triggered from sweet alert. How can I resolve this issue? Appreciate any help! SweetAlert: Missing "title" argument! Here is my JavaScript code: function DeletePost() { swal({ title: "Are you ...

Issue: Package 'cairo' not located on EC2 Bitnami MEAN server

Setting up my MEAN application on a Bitnami server has been quite a challenge. During the installation of dependencies, I encountered an error that I just can't seem to resolve. Despite following the instructions provided in the error message, I am st ...

Utilize the jQuery ajax post method within each loop to handle asynchronous requests, ensuring that the

I am currently working on a jQuery ajax function that is embedded within an each function as it needs to be executed for each post on the website. $('.post .button').each(function() { $(this).click(function() { $.ajax({ url: 'action ...

Proper method for calling a function within a specific scope

I am utilizing user-contributed modules that I aim to avoid editing in order to make upgrades easier. My goal is to enable users to browse for a CSV file on the local filesystem, parse it, and display it in a dynamic table. For this task, I am using PapaP ...

Attempt to generate a function in JavaScript that mirrors an existing one

How can I create a javascript function similar to loadAllOriginal, but with the value of the variable allEmployees being a list of employee objects (ID, FirstName, LastName)? I am attempting to implement this method in combination with autocomplete from ...

Ways to make normal material fade in THREE.JS

Recently, I have been exploring ways to create a smooth transition between different areas in THREE.JS. After some experimentation, I have come up with a method that I find quite effective. It involves manipulating the scene's fog by increasing its d ...

Can you please send me the understanding? (hubot-script)

After setting up hubot and running some tests, I came across something puzzling: Looking at the following code snippet (from the maps.coffee file in hubot-maps) robot.respond /(?:(satellite|terrain|hybrid)[- ])?map( me)? (.+)/i, (msg) -> mapType = ms ...

Is there a way to troubleshoot the "module not found error" that keeps popping up when I attempt to execute the code following a smooth installation of sqlite3?

Initially, I successfully installed the sqlite3 module but encountered errors like "module not found". However, upon attempting to reinstall sqlite3 (npm install sqlite3), more errors surfaced that required thorough post editing. The output is as follows: ...

Shift the <tfoot> table to the right using CSS

Is there a way to shift the red area (x1, x2) to the right using only CSS in a table where the <tfoot> tag cannot be edited? https://jsfiddle.net/qL03728p/ table { width: 100%; border-collapse: separate; } table td { border-top: 1px solid ...

What is the best way to send a function or callback to a child process in Node.js?

In this scenario, imagine having a parent.js file with a method called parent var childProcess = require('child_process'); var options = { someData: {a:1, b:2, c:3}, asyncFn: function (data, callback) { /*do other async stuff here*/ } } ...

Modifying the appearance of a CSS element with jQuery: Step-by-step guide

The code I have is as follows: $('.signup-form-wrapper').css("style", "display: block"); $('.login-form-wrapper').css("style", "display: none"); I'm not sure why it's not working. The current appearance of ...

Transforming a Java applet into an <object> tag

After updating my Java to the latest version, I encountered a problem with an applet I regularly use. Despite adjusting the security settings to the lowest level through the Control Panel, the applet still fails to run correctly. Here is how the applet was ...

Is there anyone available to assist me with this contact validation form?

I'm struggling to make this contact form highlight red and display 'not valid' inside the boxes. Despite my efforts, I just can't seem to get it to work! I'm unable to change the existing HTML tags, but I can add to the HTML. I wan ...

Resolving Problems with setInterval in jQuery Ajax Calls on Chrome

Seeking assistance in returning the value of a specific URL periodically using jQuery and setInterval. Below is my current code snippet: $("form").submit(function() { setInterval(function(){ $('#upload_progress').load(&ap ...

Troubleshooting Problem with MVC Ajax Requests

When working with an MVC view, I have a submit button that includes validation checks. If the validation fails, I return false to prevent the form from being submitted. In addition to standard validation, I also use an Ajax GET request to check for duplic ...

What is the best way to display the content below a bootstrap card while utilizing relative positioning?

Currently working on a bootstrap card design featuring an image. With the card body positioned above the image using position: absolute and top. The challenge arises when adding a new div below the card; it ends up being pushed onto the image instead of s ...