Prevent the parent from adjusting to fit the child's content

Recently, I've been working on creating a horizontal menu bar and I have condensed the CSS as much as possible. However, I am facing an issue where I do not want the ul li element to adjust its size based on the content of the ul ul. Any suggestions on how to prevent this behavior? Based on my knowledge, since the ul li elements have display:inline-block, they will always resize according to the content. Here is what I currently have: jsFiddle

body,
html {
  padding: 0px;
  margin: 0px;
  height: 200%;
}
.nav {
  postion: fixed;
  top: 0;
  width: 100%;
  margin: 0px;
  padding: 0px;
  margin: 0px;
  height: 50px;
  color: #F5F5F5 !important;
  font-size: 0px;
}
.nav ul {
  list-style-type: none;
  postion: relative;
  margin-left: -40px;
}
.nav ul li {
  display: inline-block;
  line-height: 50px;
  font-size: 15px;
}
.nav ul ul {
  visibility: hidden;
}
.nav ul ul li {
  display: block;
  font-size: 15px;
  top: -100%;
}
.nav ul li:hover>ul {
  visibility: visible;
}
.nav {
  background: #35404F;
  border-bottom: 4px solid #17A697;
  font-family: 'PT Sans', sans-serif;
}
.nav ul li {
  transition: background 250ms ease-in;
}
.nav ul li:hover {
  background: #17A697;
}
.nav ul ul li {
  transition: background 250ms ease-in;
  background: #35404F;
}
.nav ul ul li:hover {
  background: #17A697;
}
.nav a {
  margin: 0px 20px;
}
<div class="nav">
  <ul>
    <li><a>Hi there</a>
      <ul>
        <li><a>Here is </a>
        </li>
        <li><a>Awesome</a>
        </li>
      </ul>
    </li>
    <li class="aright"><a>Hi there</a>
      <ul>
        <li><a>Here is the awesomeness</a>
        </li>
        <li><a>Awesome</a>
        </li>
      </ul>
    </li>
  </ul>
</div>

Answer №1

Ensure that .nav ul ul has a position:absolute specified. If the position is not set to absolute, it will inherit its parent's relative position which can cause sizing issues. By setting the position to absolute, the ul becomes independent and prevents sizing conflicts with its child elements.

http://jsfiddle.net/fy6wggo1/1/

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

Ways to merge two select options in a form

I'm attempting to merge the selections from two dropdown menus in a form into one variable before submitting the form. Here is an overview of my code: In new.html.erb (for RoR): <%= form_for :character, url: characters_path, method: :post do |f| ...

Only the initial value is being processed in the for loop using Javascript

I am having issues trying to sum the values within a for loop sourced from my Django database using JavaScript. Currently, when I iterate through the loop, only the first value in the array is returned. I need assistance in finding a solution that allows ...

Utilize JQuery's .append() method to insert a new element into the DOM and then trigger an "on click" event within

I am attempting to use the .append() method to add a new radio button, and then when I click on the new radio buttons, trigger a new function. However, I am having trouble achieving this with my current code: HTML: <label>Where should the photo be ...

Building an expandable vertical dropdown with Bootstrap that opens with a click

My goal is to design a vertical menu that expands when clicked, revealing 3 links for each item. I currently have the following setup: This is the initial layout: After clicking all items: The code I've implemented so far looks like this: <!DOC ...

In Angular 4, when choosing an option, only the Display Id is shown, not the Name

I'm currently working with Angular 4 Web API to display packingtypename when an option is selected. Everything is working fine, but I am facing an issue where only the packingtypeID is saved when clicking the add button. I would like to save both the ...

Display and conceal specific table cells based on a user's search for a singular cell

I recently wrote a jQuery script to filter rows in a table based on user input. However, I encountered an issue with my current code when trying to search for a specific string within the table. Here's the scenario: a|b|c|d| e|f|g|h| i ...

Tips for enhancing a table with extra functionality in Angular 6

Hi there! I've created a table using Angular 6 and now I'm looking to enhance it with some extra functionality: Implement an overall table search feature Allow users to sort the table by columns Add a "Page x of n" option for pagination I woul ...

What are the steps to view my HTML webpage on a smartphone?

Recently, I successfully created an HTML webpage with CSS and JS that looks and functions perfectly on my PC. However, when attempting to access it on my phone, I encountered some issues. Despite transferring all the necessary files to my phone, only the ...

Utilizing Django to Retrieve HTML LIST Data Seamlessly Without Forms or Models

After creating an HTML page containing a series of list items, I wanted to implement a feature where clicking a button would read the data within the list and perform a transformation. I came across an example similar to this on Stack Overflow. However, t ...

JavaScript salary calculation function not functioning properly

Once the user inputs the employee's name and the number of hours they worked on the HTML form, the submit button captures this information and stores it in variables for calculating their pay. The script also factors in overtime pay. Despite feeling l ...

What is the best way to filter specific data types when using ngFor in Angular?

As I loop through the array named "fruits," which contains objects of type "FruitService" that I created, I want to display each element. However, when I delete them (I know it's strange, no database involved), they turn into type "undefined" and star ...

Unable to create property within array in model

I am facing an issue while trying to access the name property of an array called Model[] generated from my Model.ts file. When attempting to use it in my app.component, I receive an error stating that the property 'name' does not exist on type Mo ...

My attempts to load the .mtl and .obj files using THREE.js have been unsuccessful

I've been working on creating a 3D model viewer using three.js, but I'm encountering issues with loading .mtl and .obj files. Despite following a tutorial at , the only model that loads successfully is the female one. Here is the code snippet I ...

Adapting padding based on the height of the browser to ensure optimal layout

I'm interested in adjusting the padding value of a button element based on the height of the browser window, not its width. I want to make sure that the padding adjusts proportionally to the browser window's height. Is this achievable using CSS? ...

Unable to get the Gtranslate function to function properly within the drop-down menu

Currently, I am using Gtranslate.io languages on my website with flags displayed without a drop-down menu. Everything is running smoothly but now I am looking to enhance the user experience by placing the flags inside a drop-down list. I want English to ...

The filter search feature fails to return any results when data is inputted into the tables

I have implemented a filter search feature within two tables on my website. My goal is to retrieve the search results for a specific drink while keeping the table headings fixed in their positions. For instance, if I type "Corona" into the search box, I ...

What is the best way to position two divs side by side at the bottom of the

When trying to align my second div with the first one, I face an issue. If the text above the first blue box is longer, the second div appears to be outside the line of the box. Changing the relative position doesn't solve this problem either. When th ...

I am looking for a way to distinguish between mandatory and non-mandatory input fields in React

I've encountered an issue with the borders of the text fields in my React project. Here's how I want the text fields to look: https://i.stack.imgur.com/MP6DG.png Currently, the borders appear straight in the browser even though I want them rou ...

What is the best way to modify the colors of two specific elements using a combination of CSS and JavaScript

I am currently developing a browser-based game and have encountered an issue. Here is the relevant line of my HTML/PHP code: echo '<div id="div'.$n.'" class="d'.$rand.'" onclick="swapit(this.id, this.className)"></di ...

A guide on incorporating the CSS 'content' property with the 'option' tag

Is it possible to include CSS 'content' in the 'option' tag? For example, I want to display "Name: Volvo". <html> <head> <style> option:before { content: "Name: "; } </style> </head> <body> ...