Tips for positioning the popup ul in relation to its parent li

How can the pop-up ul be positioned in the center of the parent li using CSS?

* {
  margin: 0;
  padding: 0;
}

#track-nav {
  margin-left: 50px;
  margin-top: 50px;
  float: left;
  width: 100%;
  list-style: none;
  font-weight: bold;
  margin-bottom: 10px;
}

#track-nav li {
  float: left;
  margin-right: 10px;
  display: block;
}

.track-nav-menu {
  display: inline-block;
}

#track-nav ul {
  list-style: none;
  position: absolute;
  display: none;
  opacity: 0;
  -webkit-transition: 0.25s linear opacity;
  padding-bottom: 2px;
}

#track-nav ul li {
  float: none;
}

#track-nav ul a {
  white-space: nowrap;
  display: block;
}

#track-nav li:hover ul {
  display: block;
  opacity: 1;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.32);
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 5px;
}

#track-nav li:hover ul li {
  padding: 10px 10px 0 10px;
}

#track-nav li:hover ul:after {
  content: "";
  width: 0;
  height: 0;
  position: absolute;
  bottom: 100%;
  left: 35px;
  border-width: 0 6px 6px 6px;
  border-style: solid;
  border-color: #fff transparent;
}

#track-nav li:hover ul:before {
  content: "";
  width: 0;
  height: 0;
  position: absolute;
  bottom: 100%;
  right: 13px;
  border-width: 0 8px 8px 8px;
  border-style: solid;
  border-color: rgba(0, 0, 0, 0.1) transparent;
}

#track-nav li:hover a {
  white-space: nowrap;
}

.clearfix_track:after {
  content: "";
  display: table;
}

#track-nav li:hover ul a {
  text-decoration: none;
  -webkit-transition: -webkit-transform 0.075s linear;
  white-space: nowrap;
}
<div id="main">
  <div id="track" class="container">
    <nav class="nav-menu">
      <ul id="track-nav" class="clearfix_track">
        <li>
          <a href="/">Main</a>
          <span>&gt;&gt;</span>
        </li>
        <li>
          <a class="consum" href="/product/catalog">Matherials</a><span>&gt;&gt;</span>
          <ul>
            <li>First gdgsfgs dfghdsfh</li>
            <li>Second</li>
            <li>Third</li>
            <li>Fouth</li>
            <li>Fifth</li>
          </ul>
        </li>
        <li>
          <a href="/cat/zhestkie-listovye-materialy">Color pencils</a><span>&gt;&gt;</span>
          <ul>
            <li>White pencil</li>
            <li>Red pencil</li>
            <li>Green pencil</li>
            <li>Blue pencil</li>
          </ul>
          <span>&gt;&gt;</span>
        </li>
        <li>
          <a href="/cat/listovoi-pvh">Color paper</a>
          <span>&gt;&gt;</span>
          <ul>
            <li>fdfd</li>
            <li>dfdf</li>
          </ul>
        </li>
      </ul>
    </nav>
  </div>
</div>

Answer №1

To create the dropdown menu, you will have to apply the translateX and left properties to the ul element, as well as add position:relative to the parent li.

* {
  margin: 0;
  padding: 0;
}

#track-nav {
  margin-left: 50px;
  margin-top: 50px;
  float: left;
  width: 100%;
  list-style: none;
  font-weight: bold;
  margin-bottom: 10px;
}

#track-nav li {
  position: relative;
  padding-bottom: 15px;
  float: left;
  margin-right: 10px;
  display: block;
}

.track-nav-menu {
  display: inline-block;
}

#track-nav ul {
  list-style: none;
  position: absolute;
  display: none;
  opacity: 0;
  -webkit-transition: 0.25s linear opacity;
  padding-bottom: 2px;
  min-width: 150px;
  left: 50%;
  transform: translateX(-50%);
  top: 100%;
}

#track-nav ul li {
  float: none;
  padding: 0;
}

#track-nav ul a {
  white-space: nowrap;
  display: block;
}

#track-nav li:hover ul {
  display: block;
  opacity: 1;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.32);
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 5px;
}

#track-nav li:hover ul li {
  padding: 10px 10px 0 10px;
}

#track-nav li:hover ul:after {
  content: "";
  width: 0;
  height: 0;
  position: absolute;
  bottom: 100%;
  left: 50%;
  border-width: 0 6px 6px 6px;
  border-style: solid;
  border-color: #fff transparent;
  transform: translateX(-50%);
}

#track-nav li:hover ul:before {
  content: "";
  width: 0;
  height: 0;
  position: absolute;
  bottom: 100%;
  left: 50%;
  border-width: 0 8px 8px 8px;
  border-style: solid;
  border-color: rgba(0, 0, 0, 0.1) transparent;
  transform: translateX(-50%);
}

#track-nav li:hover a {
  white-space: nowrap;
}

.clearfix_track:after {
  content: "";
  display: table;
}

#track-nav li:hover ul a {
  text-decoration: none;
  -webkit-transition: -webkit-transform 0.075s linear;
  white-space: nowrap;
}
<div id="main">
  <div id="track" class="container">
    <nav class="nav-menu">
      <ul id="track-nav" class="clearfix_track">
        <li>
          <a href="/">Home</a>
          <span>&gt;&gt;</span>
        </li>
        <li>
          <a class="consum" href="/product/catalog">Materials</a><span>&gt;&gt;</span>
          <ul>
            <li>First item</li>
            <li>Second item</li>
            <li>Third item</li>
            <li>Fourth item</li>
            <li>Fifth item</li>
          </ul>
        </li>
        <li>
          <a href="/cat/color-pencils">Color Pencils</a><span>&gt;&gt;</span>
          <ul>
            <li>White pencil</li>
            <li>Red pencil</li>
            <li>Green pencil</li>
            <li>Blue pencil</li>
          </ul>
          <span>&gt;&gt;</span>
        </li>
        <li>
          <a href="/cat/color-paper">Color Paper</a>
          <span>&gt;&gt;</span>
          <ul>
            <li>Item 1</li>
            <li>Item 2</li>
          </ul>
        </li>
      </ul>
    </nav>
  </div>
</div>

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 div needs to be positioned above another div, not beneath it

Just returning to the world of coding and struggling with a basic problem. Any help would be greatly appreciated. I am trying to position the 'header-top' (red) div at the top, and the 'header-bottom' (blue) div at the bottom. However, ...

Combining three input files in a single form and securely storing them in the database

After trying several solutions on Stack Overflow, I haven't found any that helped me or addressed the issue at hand. I have a form with three input fields, and while I know I can use the 'multiple' attribute on one field, I specifically nee ...

Is it possible to stop Angular requests from being made within dynamic innerhtml?

I have a particular element in my code that looks like this: <div [innerHtml]="htmlContent | sanitiseHtml"></div> The sanitiseHtml pipe is used to sanitize the HTML content. Unfortunately, when the htmlContent includes relative images, these ...

I'm curious, what height would be most suitable for a first impression?

I'm in the process of building a website and I want to ensure that all elements are visible within the div without requiring users to scroll. However, I know that screen resolutions vary between computers. What can I do to address this issue? Is ther ...

Is there a way to display a message in a div container instead of using the alert box when there is a successful ajax response?

Hey there, I'm currently working on implementing error validation handling for a custom form that I've created. I'm looking to display the error messages in a designated div rather than using the standard browser alert box. Since I'm fa ...

I'm having trouble with Material Design Slide Toggle as it lacks event.StopPropagation functionality. Any suggestions on what alternative I

When working with the Slide Toggle in Material Design, I noticed that it does not have a stopPropagation event. This is because the "MdSlideToggle.prototype._onChangeEvent" already includes a call to stopPropagation. So, what should be used instead? <m ...

Exploring the Differences in Table Resizing between IE8, IE7, and Chrome when using table-layout:fixed

For my HTML timeline view, I encountered an issue that I'm hoping to resolve: I have a table row with 12 cells representing the 12 months, each with a colspan based on the number of days in that month. Another row in the same table contains the indiv ...

The Everyday Explanation of Same Origin Policy

Could anyone simplify the concept of the Same Origin Policy for me? I've come across various explanations but I'm in search of one that a child can easily understand. I found this link to be quite helpful. Is there anyone who can provide further ...

Leveraging $this in conjunction with a jQuery plugin

I'm experimenting with a code snippet to reverse the even text in an unordered list: $(document).ready(function () { $.fn.reverseText = function () { var x = this.text(); var y = ""; for (var i = x.length - 1; i >= 0; ...

Styling with CSS to format a table so that each cell occupies one row when viewed on narrower

I've come across some HTML that resembles this structure <html><body><table> <thead><tr><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th></tr></thead ...

In Javascript, when trying to call Firebase database child(), it may sometimes result in the return value being "

Here is the current setup: Firebase Database: setores: { -KkBgUmX6BEeVyrudlfK: { id: '-KkBgUmX6BEeVyrudlfK', nome: 'test' } -KkDYxfwka8YM6uFOWpH: { id: '-KkDYxfwka8YM6uFOWpH', nome ...

Pressing Ctrl + S enables you to preserve the webpage for future

Is there a way to save an entire webpage, including all the HTML files, using Ctrl + S in Chrome? I have tried two different methods for saving the page, but so far, neither of them has worked: from selenium import webdriver from selenium.webdriver.common ...

Steps to adding a collection of links (stylesheets, javascript files) to each html document

Is it feasible to add a list of links to multiple HTML files? I was inspired by W3 School's W3 Include functionality, which allows you to import blocks of HTML code into various files simultaneously, making it convenient for making changes across many ...

Incorporating Admob into the Intel XDK platform

After creating a customized 2048 game app for Android with Intel XDK, I encountered difficulties when trying to integrate AdMob into my application. Lack of coding knowledge may be the reason behind my struggles. I have been using the original codes for th ...

Why am I not receiving any results from the communication between JavaScript and PHP using an HTTP GET request?

Currently, I have a small JavaScript program running within an HTML5 canvas and included a HTTP GET request function in my JavaScript code. The function itself is functioning properly as I tested it with multiple examples from the internet, all of which wo ...

Transferring data between two screens within an Ionic app by harnessing the power of angularJS

As a beginner in Ionic and Angular development, I am currently facing an issue with my Ionic application. I have two pages - one with drop-down selects and a submit button, and another page to process the user's choices and display the results. The pr ...

Float over a specific line in a drawing

I am looking to develop a unique rating system using css, html, and potentially js : My goal is for the user to hover over a specific section of a circular stroke and have it fill with a particular color, all while maintaining functionality. So far, I hav ...

Assign the two values to variables in the CSS script

My challenge lies in passing two values to the CSS within the <style> tags. The values are: (background-color: --placeholder; color: --placeholdtext;). I am able to pass either one of the values successfully, but not both at the same time. When I cop ...

Transform Unicode characters into HTML using Qt

I am facing an issue with a particular string that includes Unicode characters and special symbols. The string, for instance, looks like this: SYMBOLSU+2510\n The string contains the Unicode character U+2510 and a new line symbol \n. I have a lo ...

Tips for efficiently webscraping multiple pages with Beautiful Soup

I have been running my web-scraping script for quite some time, but it seems to be stuck on scraping only one URL. I intend to scrape five different URLs, and I am wondering if the issue lies with my loop getting stuck in an infinite loop. Furthermore, I ...