Dropdown CSS navigation menu

I'm currently facing a challenge in designing a CSS menu. My goal is to have the menu open upwards instead of downwards, with the word "TEST" always staying at the top of the list even when the mouse hovers over it. I'm unsure of how to achieve this at the moment, so any suggestions or ideas would be greatly appreciated. Thank you and have a great day!

.span1 {
  background-color: #e83737;
  color: white;
  position: absolute;
  text-align: center;
  vertical-align: middle;
  line-height: 60px;
  top: 413px;
  left: 120px;
  width: 192px;
  height: 68px;
  font-size: 20px;
}

.span1:hover {
  background-color: #e83737;
  position: absolute;
  left: 120px;
  top: 123px;
  width: 192px;
  height: 68px;
  font-size: 20px;
}

.dropdown ul li:hover ul {
  height: initial;
  bottom: 100%;
  overflow: visible;
  background: lightgray;
}

.dropdown-child {
  display: none;
  background-color: #f28c8c;
  position: absolute;
  width: 192px;
  top: 190px;
  left: 120px;
}

.dropdown-child a {
  color: white;
  padding: 20px;
  text-decoration: none;
  display: block;
}

.dropdown:hover .dropdown-child {
  display: block;
}
<div class="dropdown">
  <span class="span1">TEST</span>
  <div class="dropdown-child">
    <a href="">abc</a>
    <a href="">abc</a>
    <a href="">abc</a>
    <a href="">abc</a>
  </div>
</div>

Codepen demo: https://codepen.io/tiboo__/pen/XWjoeQB

Answer №1

Perhaps you're aiming for a different outcome, but consider altering .span1:hover { to .dropdown:hover .span1 {.

Answer №2

To ensure proper positioning, a relative positioned parent is required for a child absolute block. To achieve this in your CSS, include the following:

.dropdown {
  position: relative;
}

Specify the positioning rules for the .span1 class as follows:

top: 0;
left: 0;

In addition, some selectors had unnecessary rules and selectors.

.dropdown {
  position: relative;
}

.span1 {
  background-color: #e83737;
  color: white;
  position: absolute;
  text-align: center;
  vertical-align: middle;
  line-height: 60px;
  top: 0;
  left: 0;
  width: 192px;
  height: 68px;
  font-size: 20px;
}

.dropdown ul li:hover ul {
  height: initial;
  bottom: 100%;
  overflow: visible;
  background: lightgray;
}

.dropdown-child {
  display: none;
  background-color: #f28c8c;
  width: 192px;
}

.dropdown-child a {
  color: white;
  padding: 20px;
  text-decoration: none;
  display: block;
}

.dropdown:hover .dropdown-child {
  display: block;
}
<div class="dropdown">
  <span class="span1">TEST</span>
  <div class="dropdown-child">
    <a href="">abc</a>
    <a href="">abc</a>
    <a href="">abc</a>
    <a href="">abc</a>
  </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

Is there a way to keep left-aligned text centered even when it wraps around?

I would like to center a block of left-aligned text within a div, ensuring that the visual width of the text block is centered rather than the block itself. In situations where the div is narrow and the text is too long to fit on one line, causing it to o ...

Transferring CSV information from a div container to an Excel spreadsheet

At this moment, when I have a <textarea> structured as follows: <textarea> 1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 </textarea> and then attempt to transfer the content to Excel, it prompts me to paste from a comma-separated source. However ...

Click to switch the content using ng-click

I have a query that iterates through an array of strings called wikiContent. After displaying the first three sentences from the array, I aim to insert a hyperlink labeled More, which when clicked will reveal the remaining sentences. This code snippet is ...

The compatibility of Datatables responsive feature with ajax calls appears to be limited

I recently started using the datatables plugin and have encountered an issue with responsive tables. While I successfully implemented a responsive table and an AJAX call on a previous page, I am facing difficulties with it on a new page for unknown reasons ...

Vue and Nuxt: Concealing a Variable in .env File Post-Build

     Within my Nuxtjs project, I have implemented a process in which I encrypt requests before they are sent to my Laravel API. Once the response is received, I decrypt it using specific global functions that have been defined... function encryptDataLa ...

KendoGrid encountered a JavaScript runtime error due to an invalid template

Having some trouble navigating the Kendo world and encountering an issue with a grid that is set to a JSON array data source. An error message stating "JavaScript runtime error: Invalid template" is displayed, specifically referencing null values in the d ...

How to perfectly align an element within a div using Bootstrap

Utilizing bootstrap here. Currently trying to center the h1 element within the div, but so far I have been unsuccessful. It consistently remains aligned to the left. Attempts have included using bootstrap's center-block helper class, as well as the fl ...

Create a PDF on-the-fly by leveraging Vuejs to dynamically pull data and design elements

Is it possible to create a form that captures name, expiry date, and picture, and upon submission generates a PDF document with the provided details? The PDF should be based on a design created by me and have the input data displayed in a specific location ...

Position the label to the right and left of the input field for alignment

I've been struggling to create an input form with labeled using a dl, dt, dd list. I want the first label on the left, the second on the right, and the input in the center. However, no matter what I try, I can't get the second label to align corr ...

An issue has arisen with loading chunks in Ionic 5/Angular, possibly due to an un

I am currently working on enhancing the offline capabilities of my Ionic 5 app. To achieve this, I have implemented a strategy where data is stored in SQLite while the app is connected, and then retrieved from SQLite when offline instead of making HTTP req ...

Menu/navigation bar designed with floating lines and an array of color options

I'm currently working on implementing this specific menu into my Wordpress site. My main concern is figuring out how to customize the hover effect for each navigation item. Currently, the float line changes to red (background-color:#800; height:2px;) ...

Exploring the properties within a MongoDB document using mapping functionality

I am trying to retrieve data from a document using Node.js and encountering an issue where the map operator is returning data with similar names twice. I am wondering if I can use filter instead of map to address this problem. Here is the code I am using t ...

The object is given a value in the form of a string, even though it is a strongly-typed variable

I'm encountering something unusual in my code. In the following example, there is a (change) event that captures the current value selected by the user from a dropdown menu. <div style="padding-right: 0; width: 100%;"> <label st ...

Node.js server encountering a cross-domain error

As a beginner exploring node.js, I am embarking on setting up my very first installation. In my server.js file, I am defining a new server with the following code: var app = require('http').createServer(handler), io = require('socket.io&a ...

Unusual class exhibiting peculiar async/await patterns

Node 7.9.0 The situation goes like this: class TestClass { constructor() { const x = await this.asyncFunc() console.log(x) } async asyncFunc() { return new Promise((accept) => { setTimeout(() => accept("done"), 1000) }) ...

Ways to access the return value for each state change in a different component within a React application

If a React Component receives a value and then changes its state, how can it return a new value from the component? If the ScrollDetector's state changes, how can this component return a value? <ScrollDetector/> export default class ScrollDet ...

Issue with loading Angular CSS files arises when managing two partials using a single controller

I am currently developing a web application using the MEAN stack. My project utilizes Bootstrap as the CSS library, with some CSS overrides present in two of my own files named "app.css" and "mio.css". Everything was running smoothly until I encountered an ...

Transfer information to an ExpressJS server in order to display a fresh view

I'm struggling to figure out how to transfer data from the client side to an ExpressJS server in order to generate a view based on that information. When users choose different parameters on the client side, they update the 'data-preference&apos ...

The border of the cell in the top row only partially overlaps with the margin area

Have you noticed how the left border of the first cell doesn't line up with the margin area of the table? This peculiar phenomenon seems to occur only in the first row: https://i.stack.imgur.com/qMWCh.jpg In all other rows, the border aligns proper ...

The Angular Http Interceptor is failing to trigger a new request after refreshing the token

In my project, I implemented an HTTP interceptor that manages access token refreshing. If a user's access token expires and the request receives a 401 error, this function is designed to handle the situation by refreshing the token and re-executing ...