CSS navigation drop down menu styling

I am struggling to align the navigation menu on the right side in the center of the header area, similar to what is done on cyberchimps.

https://i.sstatic.net/2OeJe.png

Additionally, I want the hover state to remain active on the BGA sub-menu when hovering over it. Any suggestions on how to achieve this?

I've noticed the use of "*" in CSS on some examples, is it still considered a good practice today?

I'm not confident in my current approach, so any advice or recommendations are highly appreciated.

@charset "utf-8";
/* CSS Document */
body{
margin: 0;
padding: 0;
background-color: #CCC;
}

@font-face{
src: url(assets/fonts/oswald-v16-latin-regular.ttf);
font-family: oswald;
}

/* ~~ The header is given a width of 100%.
It will extend the full width of the browser. 
The rest of the layout will be fixed~~ */
header{
background-color: #004489;
min-width: 1024px;
height: 100px;
clear: both;
}
/*header::after {
  content: '';
  display: block;
  clear: both;
}*/
.navWrapper{
max-width: 1100px;
margin: 0 auto; 
}

nav{
float:right;
margin-top: 20px;
padding-right: 50px;
}

nav ul{
list-style: none;
padding: 0;
margin: 0;

}

nav ul li{
display: inline-block;
float: left;
position: relative;

}

nav ul li a{
display: block;
margin: 0;
padding: 10px 10px;
text-decoration: none;
font-family: oswald;
font-size: 25px;
color: #fff;
}


nav ul a:hover, nav ul a:active, nav ul a:focus{
background-color: #065BB0;
}

nav ul ul{
display: none;/**/
position: absolute;
}

/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:list-item;
}
nav ul ul li{
display: block;
width: 115px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
nav ul ul li a{
color:#242323;
background-color: #8ec6ef;

}
nav ul ul a:hover, nav ul ul a:active, nav ul ul a:focus{
color:#fff;
background-color: #065BB0;
}
<body>
<header>
<div class="navWrapper">
<nav>
<ul>
<li><a href="#">HOME</a></li>
<li><a href="#">BGA</a>
<ul>
<li><a href="#">BOOKS</a></li>
<li><a href="#">COURSES</a></li>
</ul>
</li>
<li><a href="#">CONTACT</a></li>
</ul>
</nav>
</div>
</header>
</body>

Answer №1

It seems that achieving this with pure CSS is not possible; rather, you can accomplish it easily by utilizing jQuery's mouseenter event:

$('#bga-list').mouseenter(() => {
$('#bga-list > ul').show();
})
@charset "utf-8";
/* CSS Document */
body{
margin: 0;
padding: 0;
background-color: #CCC;
}

@font-face{
src: url(assets/fonts/oswald-v16-latin-regular.ttf);
font-family: oswald;
}

/* ~~ The header is given a width of 100%.
It will extend the full width of the browser. 
The rest of the layout will be fixed~~ */
header{
background-color: #004489;
min-width: 1024px;
height: 100px;
clear: both;
}
/*header::after {
  content: '';
  display: block;
  clear: both;
}*/
.navWrapper{
max-width: 1100px;
margin: 0 auto; 
}

nav{
float:right;
margin-top: 20px;
padding-right: 50px;
}

nav ul{
list-style: none;
padding: 0;
margin: 0;

}

nav ul li{
display: inline-block;
float: left;
position: relative;

}

nav ul li a{
display: block;
margin: 0;
padding: 10px 10px;
text-decoration: none;
font-family: oswald;
font-size: 25px;
color: #fff;
}


nav ul a:hover, nav ul a:active, nav ul a:focus{
background-color: #065BB0;
}

nav ul ul{
display: none;/**/
position: absolute;
}

/* Display Dropdowns on Hover */

nav ul ul li{
display: block;
width: 115px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
nav ul ul li a{
color:#242323;
background-color: #8ec6ef;

}
nav ul ul a:hover, nav ul ul a:active, nav ul ul a:focus{
color:#fff;
background-color: #065BB0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<header>
<div class="navWrapper">
<nav>
<ul>
<li><a href="#">HOME</a></li>
<li id="bga-list"><a href="#">BGA</a>
<ul>
<li><a href="#">BOOKS</a></li>
<li><a href="#">COURSES</a></li>
</ul>
</li>
<li><a href="#">CONTACT</a></li>
</ul>
</nav>
</div>
</header>
</body>

Nevertheless, continuously displaying a dropdown list on mouseenter without hiding it might not be the best approach. I hope this information proves helpful.

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

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

Having trouble with video playback in the HTML5 media player on Codeigniter platform

I'm attempting to incorporate a video into my web application using the HTML5 media player within the Codeigniter framework. Below is the code snippet I have been working on: public function view($settings, Screen $screen) { ...

What is the best way to integrate my custom JavaScript code into my WordPress theme, specifically Understrap?

I am looking to enhance my website with a sticky navbar positioned directly under the header, and I want it to stick to the top of the page as users scroll down. Additionally, I want the header to disappear smoothly as the user scrolls towards the navbar. ...

What causes the image to vanish once the CSS animation is activated?

Is there a reason why the image disappears after the animation completes? :/ .coin { width: 200px; height: 200px; background-size: cover; animation: CoinflipRoll 6s steps(199); animation-delay: .5s; animation-fill-mode: forwards; ...

Switching a div class in Angular 6 with a button click: A step-by-step guide

In this scenario, I have a div with the class name old. There is also a button that, when clicked, should toggle the div's class name between old and new. I am looking for a solution using Angular 6. Below is the code snippet: I am relatively new to ...

Pair of buttons triggering the submission of a form

I'm encountering an issue where I have 2 buttons in my form, one for submitting and one for going to the previous page, but both seem to be performing the same action. How is this happening? <div style="position:fixed; left: 50px; ...

Discovering the amount of attributes possessed by an element with jQuery

If I have an xml element like this: <fruit color="blue" taste="sweet" shape="round"></fruit> Without using jQuery, I could use the following code: fruit.attributes.length How can I achieve the same result with jQuery? ...

Bug with the animation of height in Jquery

Unfortunately, I can't share my entire source code here because it's quite lengthy. However, maybe someone can provide some insight into a common issue that may be happening elsewhere. The problem I'm facing is with a DIV element that has r ...

Obtain the URL of the chosen file from the input file in an HTML/AngularJS framework

Currently, I am developing a web app in MVC-5 using AngularJS. Below is the structure of the div that I am working on: <div class="row form-clearify" style="margin-top:3px;"> <div class="col-md-6 col-sm-6 col-xs-12" style="background-color:w ...

A step-by-step guide to setting up a Slick Slider JS slideshow with center mode

I am working on implementing a carousel using the amazing Slick Slider, which I have successfully used for images in the past without any issues. My goal is to create a 'center mode' slideshow similar to the example provided but with multiple div ...

Does the Parent Tag style override the child tag style when hovering?

I am currently dealing with some style issues. How can I apply the styling of the parent tag to the child tag when hovering over it? Let's illustrate this with a simple example. .outer:hover { background: yellow; z-index: 1; position: relativ ...

What is the best way to align the checkbox and label in a React form?

I've been struggling to align the labels next to the checkboxes on this form. I'm currently utilizing React with React-Hook-Form for validation and state management (you can find the code on GitHub: https://github.com/cipdv/ciprmt-mern/blob/main/ ...

Wrap the text within the contenteditable div with the <p> tag

By utilizing the code snippet below, I have managed to input text into a contenteditable div and instead of generating a new div on pressing the enter key, it generates <br> tags. Is there a way to enclose the text inside the contentedtiable div wit ...

What is preventing me from retrieving an ID value within an IF condition?

I'm trying to create a script that only allows devices with matching IP addresses, but I'm having trouble using an if statement. Whenever I include x.id inside the statement, it doesn't seem to work... any suggestions? <html> <sc ...

What could be causing my checkbox to become unresponsive and fail to reset?

I recently created a function to update my page title when the toggle button is clicked, but unfortunately, it seems to be getting stuck after the click. I can't seem to pinpoint the issue with this function, as I've tried coding it in both JQuer ...

Guide to switch background image using querySelector

I am currently trying to figure out how to set the background image of a div block by using querySelector. I have attempted various methods in my test code below, but unfortunately none seem to be working. Can someone please provide assistance? <!DOC ...

The Razor MVC does not support HTML syntax within an If condition

Here is the code snippet I currently have in one of my MVC views. It seems that within the specified if condition, the HTML code is not being recognized. <table class="table"> <tr class="row h4"> <td>Task</td> ...

Fixing a menu hover appearance

I recently encountered a small issue with the menu on my website. When hovering over a menu item, a sub-menu should appear. However, there seems to be a slight misalignment where the submenu appears a few pixels below the actual menu item. Check out the w ...

How can you reposition a component within the dom using Angular?

Just started learning Angular, so I'm hoping this question is simple :) Without getting too specific with code, I could use some guidance to point me in the right direction. I'm currently developing a small shopping list application. The idea i ...

Tips for positioning a text link alongside an image link within a table row using only inline styles

I am struggling to align text and an image link next to each other within a table row. The image keeps moving up and down despite my efforts to use various alignment and display block styles. I am only able to use inline CSS and need it to display correctl ...