How can I ensure that the size of the Dropdown Menu Item matches its parent in both Bootstrap and CSS styles?

I am working on a navigation bar that has a dropdown menu which appears on hover. I want the size of the dropdown menu items to match the size of the parent element.

Here is an image for reference:

https://i.stack.imgur.com/oNGmZ.png

Currently, the "One" & "Two" elements in the dropdown menu are slightly larger than the parent element "This is a dropdown." When resizing the window, the parent element changes size and I would like the "One" & "Two" elements to resize accordingly to maintain consistency in width.

Code

The structure of my navbar includes various elements laid out as follows:

<div class="container-fluid nopadding navbar"> <!-- NAVBAR -->
  <div class="row">

    <div class="container-fluid col-xs-12 col-sm-12 col-md-10"> <!-- MENU -->
      <div class="row">

        <div class="dropdown col-xs-12 col-sm-2">
          <div class="container-fluid">
            <div class="row">
              <button class="dropbtn col-xs-12">This is a dropdown</button>
              <div class="dropdown-content col-xs-12 nopadding">
                <a href="#">One</a>
                <a href="#">Two</a>
              </div>
            </div>
          </div>
        </div>
...more navbar elements follow here

    </div>
   </div>

  </div>
</div>

Below is some of the CSS code used for styling:

/* Dropdown Button */
.dropbtn {
    background-color: #F6F8FB;
    border: none;
    cursor: pointer;
    font-family: AlegreyaSansSC-Light;
    font-size: 16px;
    color: #637F92;
    letter-spacing: 0.52px;
    height: 81px;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #F6F8FB;

  z-index: 1;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {
  background: rgb(221, 232, 241); /* Fallback for older browsers without RGBA-support */
  background: rgba(221, 232, 241, 0.95);
}

/* Links inside the dropdown */
.dropdown-content a {
  padding: 30px;
  text-align: center;
  text-decoration: none;
  display: block;
}

/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
    display: block;
    top: 81px;
    position: absolute;
}

I have provided extra details just to ensure clarity in understanding the issue at hand.

Answer №1

If you want cleaner styling, consider eliminating the padding in the .dropdown-content a selector:

.dropdown-content a {
  /*padding: 30px;*/
  text-align: center;
  text-decoration: none;
  display: block;
}

Answer №2

One way to ensure all elements match the parent's width is by including the style width: 100%; in both the .dropbtn and

.dropdown:hover .dropdown-content
classes.

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

Issue with sending multiple files using FormData and axios in Vuex with Laravel. Server side consistently receiving null. Need help troubleshooting the problem

After trying the solution from my previous question Vuex + Laravel. Why axios sends any values but only not that one, which come's with method's parameter?, I realized that it only works when passing a single argument in axios. I managed to succe ...

Tips for receiving feedback when uploading multiple images

I've been facing challenges with receiving a response when uploading multiple images using the API. Despite getting a valid response when uploading a single image, I'm not getting the expected response for multiple uploads. Below is my code: fil ...

Differentiating Typescript will discard attributes that do not adhere to an Interface

Currently, I am working with an API controller that requires a body parameter as shown below: insertUser(@Body() user: IUser) {} The problem I'm facing is that I can submit an object that includes additional properties not specified in the IUser int ...

Capable of generating accounts using Postman, experiencing difficulties with creating accounts from React

Currently, I am working on a project that utilizes a React/Spring Boot/MYSQL stack and I have encountered an error message stating "POST 415: SyntaxError: Unexpected end of input at line 67". Line 67 is as follows: }).then(res => res.json()) This sect ...

Setting up IIS to send compressed JSON data is essential for optimizing web performance

My ASP.Net 2.0 website is currently hosted on IIS6. Within the code-behind classes, I have defined webmethods that are accessed using jQuery Ajax. However, when the website is hosted on a server with gzip compression enabled, the client is receiving a con ...

Take away limitations from JavaScript code

I stumbled upon this code snippet online and I'm attempting to eliminate the Name must be letters only and min 3 and max 20 restriction. The name provided can have less than 3 characters and may include numbers. My JavaScript skills are still in the l ...

Tips for saving the status of an accordion controlled by an angular directive

I am currently utilizing the accordion directive from angular-bootstrap. My goal is to save the is-open attribute of this accordion, so that when users navigate to another page on the website, the state of the accordion (i.e. is-open) remains unchanged. ...

What is the best way to access and iterate through JSON data?

I am trying to extract data from my Json file, however, I cannot use a specific 'key' because it changes on a daily basis. https://i.sstatic.net/sZySk.png My attempted solution is as follows: template: function(params) { const objects ...

Exploring MaterialUI withStyles to customize disabled switches with a CSS override

As I work on my project using React and MaterialUI, I've encountered a problem with customizing inputs. Despite trying various selectors, I can't seem to change the black color of this particular input. It would be helpful to have a clearer way o ...

How well does position:fixed function in Internet Explorer versions 7, 8, and 9?

Before diving into using position:fixed, I'd like to confirm if it works well in Microsoft browsers. Thank you. ...

Utilize Knex - incorporating withGraphFetched and a filter condition in your query

My task involves querying the database with Knex to retrieve sales data from a specific city. Here are the two tables I am working with: sale: id drugstore_id drugstore: id name city Below are my model definitions: class Sale extends Model { static mo ...

The npm package installation process encountered difficulties in accessing the Chart.Js library

Currently, I am in the process of developing a web application that tracks and logs hours spent on various skills or activities. The data is then presented to the user through a bar graph created using Chart.js. Initially, I was able to display a mock grap ...

How to use jQuery to retrieve and display an image from a JSON object

My current task involves extracting content from a JSON object and showcasing it on a webpage. While I am successful in retrieving the object and cycling through to extract different content, I encounter an issue with displaying images alongside first and ...

Testing units with Jest using ES6 import syntax instead of module.exports

There's a script I created that contains all the logic in a single const variable, similar to Moment.js. I'd like to test the functions from this script using Jest. Unfortunately, using module.exports won't work when I publish the script. ...

Blending HTML template and WordPress PHP file

I am facing a challenge while attempting to implement a Wordpress template on one of the pages of my website, specifically at http://www.windowblindshadeshutters.com/blog/. The blog has been successfully created, however, I am having trouble preserving our ...

Using WebDriver to select and interact with a hyperlink on a website

I am currently attempting to select a specific link on a webpage, which happens to be a Home button consistently located in the same position no matter where I navigate. I have been using the driver.findElement(By.xpath("//span/ul/li/a")).click(); command ...

Is it possible for jQuery datepicker to choose a date over a decade in the past?

Recently, I encountered an issue with jQuery-UI's datepicker where it was restricting me to select birthdays only within the last 10 years. This basically meant that users older than 10 years couldn't be accommodated :) I attempted to override t ...

What is the best way to stop a jQuery event listener on a button within a form from triggering upon the user hitting the Enter key?

Here is an example of a form containing a button: <form> <input type="text"> <button>Click Me</button> <input type="submit" value="Submit"/> </form> Using the following JavaScript: $('form').subm ...

Utilize the client-side JavaScript file with ejs framework

Recently, I have been working on creating a website using Express and EJS. I discovered that using just one JavaScript file for all my EJS (view) files was causing issues. If I target a DOM element in one view page and it doesn't exist in another, I w ...

Create a set of n randomly generated points, represented as latitude and longitude pairs, originating from the central point of a polygon while remaining within the boundaries

I am currently trying to plot data within a polygon (District), but unfortunately, I do not have specific coordinates for each of the data sources in that district. My goal is to accurately display all the results from a district within its boundaries, wh ...