Make the child DIV's width equal to the width of its parent DIV, even when the parent DIV's width is not

I am experiencing an issue with a dropdown menu. I have created this code (with the assistance of w3schools)... :)

The problem is that the sub-division that appears when I hover over a division does not match the width of the parent.

I attempted to assign it to "select_checkbox".

I can resolve the issue by setting the width of "multi_select" fixed in pixels and then setting the width of "select_checkbox" to 100%, but I prefer not to use a fixed width for "multi_select".

Is there a way to achieve this without using JavaScript?

Thank you

.multi_select {
  display: inline-block;
  width: auto;
}

.div_select {
  font-size: 14px;
  border: 1px solid black;
  background-color: #EEE;
  padding: 0px 5px 0px 3px;
  cursor: pointer;
}

.div_select::after {
  font-family: FontAwesome;
  content: "\f0d7";
  padding-left: 5px;
  padding-right: 5px;
}

.multi_select_cat:hover .select_checkbox, 
.multi_select_num:hover .select_checkbox{
  display: block;
}

.select_checkbox {
  position: absolute;
  display: none;
  background: #EEE;
  border: 1px solid black;
  border-top: 0px;
  width: inherit;
}

.select_checkbox div {
  display: block;
  width: 100%;
}

.select_checkbox div:hover {
  cursor: pointer;
  background-color: #F60;
}

.select_checkbox_show {
  display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<div class="multi_select multi_select_num">

  <div class="div_select noselect">
    Number of elements to show
  </div>

  <div class="select_checkbox">
    <div>10</div>
    <div>20</div>
    <div>30</div>
  </div>

</div>

<div class="multi_select multi_select_cat">

  <div class="div_select noselect">
    Filter by category
  </div>

  <div class="select_checkbox">
    <div><input type="checkbox"> Systems</div>
    <div><input type="checkbox"> Tools</div>
    <div><input type="checkbox"> Automotive</div>
  </div>

</div>

Answer №1

To enhance the existing properties, simply include this additional CSS:

.multi_select {
     position: relative;
}

.select_checkbox {
    width: 100%;
}

Answer №2

Ensure that .multi_select is made relative

.multi_select {
  display: inline-block;
  width: auto;
  position: relative; // Make sure this line is included
}

Then adjust the positioning of .select_checkbox to have left and right set to 0

.select_checkbox {
  position: absolute;
    display: none;
    background: #EEE;
    border: 1px solid black;
    border-top: 0px;
    width: inherit;
    left: 0; // Ensure this line is added
    right: 0; // Also include this line
}

Answer №3

To simplify the process, you can specify the widths of the container divs manually.

.dropdown_width {
    width: 180px;
}

.dropdown_height {
    width: 120px;
}

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

Is there a way to emphasize a particular day on the calendar obtained from the URL?

I have implemented FullCalendar functionality to enable users to select a specific date, retrieve data from a database, and then reload the page with that data. The page is updated with the selected date in the URL. However, when the page reloads, althoug ...

Embedding content within various ng-template elements

I'm currently working on developing a button component (app-button) that can utilize multiple templates based on the parent component using it. <div class="ds-u-margin-left--1 ds-u-float--left"> <ng-container *ngTemplateOutlet="icon">< ...

The issue of jumpy CSS background on full screen mode for mobile devices is causing

While developing a web app, I encountered an issue with the responsive (parallax) full screen background. On mobile devices, the background does not extend below the toolbar, causing a jumpy effect when scrolling up and down in Firefox. I have tried differ ...

Is it possible to arrange JSON Objects vertically on a webpage using tables, flexboxes, divs, and Javascript?

Within my JSON data, I have multiple products defined. My goal is to loop through this JSON and display these products side by side on a web page for easy comparison. Initially, I envision structuring them in columns and then rows: https://i.sstatic.net/K ...

Function for calling a CSS callback with jQuery

I am currently working on enhancing my search bar using jQuery, and I am also looking to hide the navigation links. Here is a snippet of the jQuery code I have been using. The code functions properly when focused. $(".searchBox input").focus(function(){ ...

When a specific JavaScript function is triggered, the value of an HTML control will reset to its original default value

I have a form with a specific requirement. I need to allow users to input data in a text field and press enter, which should dynamically create new controls like another text field, a dropdown menu, and another text field using jQuery. While the functional ...

ngAnimate - Animation not activating on recurring custom directive

I am trying to implement ngAnimate on a custom directive that is repeated using ng-repeat in my AngularJS application. The animations function correctly when I use ng-repeat on a simple HTML list-item. However, when I replace the list-item with a custom di ...

Adjustable <a> component

There is this element: <a id="lnkViewEventDetails" class="psevdo calendar-event event" style="height: 126px;" href="/someurl/895?responseType=5" onclick="event.stopPropagation();"> I am trying to make it resizable using JQuery UI: $("#lnkViewEvent ...

Place the image at right:0px position, but it won't display

I am trying to position an image at the end of a sentence within a div element. No matter what I try, the image always appears next to the end of my text. Here is the code I am using: <div id='xi' class='xc'>Text comes here and ...

Printing a JSON array in Angular: A step-by-step guide

Take a look at this Stackblitz example https://stackblitz.com/edit/angular-parse-object Response format received from REST API is [{"id":123,"name":Test,"value":{"pass": true, "verified": true}}, {"id":435,"name":Test12,"value":{"pass": false, "verified" ...

Transforming the pen into a creative assortment of animated social media icons for top-notch production

My goal is to incorporate animated social media icons on my website using only CSS without any JavaScript. I came across a pen called "Yet Another Set of Animated Social Icons" that I'm attempting to modify. The issue at hand is that instead of the c ...

Trigger click functions sequentially to display elements after specific actions are taken

Is there a way to make jQuery listen for clicks only at specific times? It seems that when I add event listeners, like $("#box").click(function(){, they are executing before the code above them finishes running. Let's say I have two boxes that should ...

Issue with dropshadows in Chrome: Instead of applying the shadow effect to the graphic itself, it is mistakenly being added to the container

Having trouble adding a Gaussian drop shadow to an SVG path. The shadow is not applying correctly in Chrome - instead of just on the graphic, it's being added to the container. Works fine in ff though. Any suggestions on how to make this work properly ...

Animation using CSS keyframes for simultaneous manipulation of various properties

I am facing a challenge with animating both the bottom and left properties of an object inside a container div simultaneously. How can I make it move diagonally? Despite using keyframes, the following code does not seem to achieve this effect: #containe ...

Unable to Load CSS Template in Django Version 1.8

My CSS file is located at /ecomstore/static/css.css I have added the CSS file to the base.html file by linking it within the head tags: <link rel="Stylesheet" type="text/css" href="/static/css.css" /> In my urls.py file: from django.conf.urls imp ...

I have come across this building error, what do you think is the cause of it?

My attempt to launch my company's React.js project, downloaded from Tortoise SVN, is encountering an issue. PS C:\Projects\Old EHR> yarn start yarn run v1.22.19 $ next start ready - started server on 0.0.0.0:3000, url: http://localhost:30 ...

The click interactions of SVG elements are altered when switching between <img> and <object> elements

I currently have 4 SVG buttons featured in the main menu of my personal website. https://i.stack.imgur.com/gA7En.png /----------------------------------------------------------------------------------------------------------------------------------/ Upo ...

Deciphering click event parameters in an AngularJS application

Currently, I am in the process of improving a feature in an existing application that has already been deployed. Unfortunately, all the JavaScript code is minified, and I only have access to the HTML files. I need to implement a function that triggers when ...

Python Selenium: Troubleshooting the get_elements method not retrieving li items within ul tags

I am facing an issue while trying to retrieve li items within a ul element using the following code: driver.get('https://migroskurumsal.com/magazalarimiz/') try: select = WebDriverWait(driver, 10).until( EC.presence_of_element_locate ...

What is the most effective method for data binding using [innerHTML] in Angular 4?

One issue I've encountered is that in Angular 4, there are limited ways to perform data binding between HTML and TypeScript, such as {{myText}}, [], (), and [innerHTML]="myText". Which method is the most effective for binding a simple variable to HTM ...