Arrange an element to appear hidden behind a text

I have a stylish horizontal navigation bar with links set up like this:

<div class="blackbar">
    <span class="blackbar-text"><a href="news.php">NEWS</a></span>
    <span class="blackbar-text"><a href="foo.php">FOO</a></span>
    <span class="blackbar-text"><a href="bar.php">BAR</a></span>
</div>

styled with the following CSS:

div.blackbar {
    margin: 0px;
    padding-left: 20px;
    padding-right: 10px;
    padding-top: 6px;
    text-align: center;
    vertical-align: middle;
    background-color: #888;
}
.blackbar-text {
    padding: 0px 12px;
    font-size: 17px;
    font-weight: 900;
    text-shadow: 0 -1px 0 #000;
}
.blackbar-text a:link,.blackbar-text a:visited {
    color:#e8e8e8;
}

Now, I want to create a dropdown effect that appears when hovering over one of these links. The dropdown should be positioned above the blackbar-div and under the link's text.

I know how to handle the JavaScript part for mouseover/out events with jQuery, but I need help with setting up the HTML and CSS structure to achieve this effect.

Check out this fiddle: https://jsfiddle.net/o4su1Lbd/

Here is an image to illustrate the desired effect:

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

Answer №1

To ensure proper alignment, replace all the spans with divs and set .blackbar-text to display: inline-block; to line them up. Additionally, include a div containing nested links under one of the .blackbar-text divs.

  <div class="blackbar-text dropdown">
    <a href="bar.php">BAR</a>
        <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
  </div>

Set the position of .dropdown-content as absolute and its display as none. The dropdown functionality is achieved by using the :hover pseudo-class on .dropdown .dropdown-content to make it display:block when hovered over.

.dropdown:hover .dropdown-content {
    display: block;
}

For more details on my solution, please visit: https://jsfiddle.net/tophermurphy/o4su1Lbd/19/

Answer №2

HTML:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- jQuery -->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<!-- Icon Library -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<script>
$('#cssmenu').prepend('<div id="menu-button">Menu</div>');
$('#cssmenu #menu-button').on('click', function(){
  var menu = $(this).next('ul');
  if (menu.hasClass('open')) {
    menu.removeClass('open');
  } else {
    menu.addClass('open');
  }
});
</script>
<title>Test</title>
</head>

<body>
<div id="cssmenu">
  <ul>
     <li><a href="#"><span><i class="fa fa-fw fa-newspaper-o"></i> NEWS</span></a></li>
     <li class="has-sub"><a href="#"><span><i class="fa fa-fw fa-bars"></i> FOO</span></a>
        <ul>
           <li><a href="#"><span>Sub link 1</span></a></li>
           <li><a href="#"><span>Sub link 2</span></a></li>
           <li><a href="#"><span>Sub link 3</span></a></li>
           <li class="has-sub"><a href="#"><span>Sub link 4</span></a>
              <ul>
                 <li><a href="#"><span>Sub link 4.1</span></a></li>
                 <li><a href="#"><span>Sub link 4.2</span></a></li>
                 <li><a href="#"><span>Sub link 4.3</span></a></li>
              </ul>
           </li>
        </ul>
     </li>
     <li><a href="#"><span><i class="fa fa-fw fa-ellipsis-h"></i> BAR</span></a></li>
  </ul>
</div>
</body>

</html>

CSS:

@import url(https://fonts.googleapis.com/css?family=PT+Sans:400,700);
#cssmenu {
  background: #4cb6ea;
  margin: 0;
  width: auto;
  padding: 0;
  line-height: 1;
  display: block;
  position: relative;
  font-family: 'PT Sans', sans-serif;
  box-sizing: content-box;
}
#cssmenu ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: block;
}

/* Remaining CSS code unchanged for better user experience */
For colors you can change them to fit your requirement 

On desktop:

https://i.stack.imgur.com/76U57.png

On smartphone:

https://i.stack.imgur.com/OGZIx.jpg

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

"Experiencing a callstack overflow due to multiple carousels on one page using Jquery or Vanilla

Currently, I am working on implementing a Jquery infinite autoplay multiple carousel. What I have done is created two separate carousel blocks on the same page within the body section. <div class="rotating_block"> <div class="bl ...

How can you use C# code to control the visibility of a table row in HTML?

I am trying to display or hide a table row based on the current month using DateTime.Now.Month in my HTML code, but I can't seem to remember the correct syntax. The code I have tried is not working as expected. Can anyone help me with the correct synt ...

Objective-C and the World of WebSockets

Possible Duplicates: Comparison of WebSockets, TCP/IP, and JavaScript/AJAX for iPhone chat Integrating WebSockets into a Cocoa application Hello all, our team is embarking on creating a bespoke iPhone chat app and deliberating the use of WebSocket ...

What is the process for eliminating the Gravatar link in Wordpress?

Is there a way to remove the gravatar link from WordPress admin while still keeping the profile picture intact? I'd like to know how to achieve this. ...

What is the best way to configure a basic firebase ajax call?

I wanted to explore using AJAX instead of set to interact with Firebase. However, when I attempted to do so with the code below in my test.html file, I encountered an error message in the console: XMLHttpRequest cannot load . No 'Access-Control-Allow ...

Exploring jQuery AJAX Attributes during ajaxStart and ajaxStop event handlers

With my project consisting of over 40 ajax webservice calls, I am looking to incorporate additional debugging features. One such feature is a timing method which I have already developed using my Timer class/object in Javascript. I'm seeking assistan ...

Adjust the background color of alternate elements

I am looking to customize the background colors of every other element within the structure provided below: <div class="dets"> <div>Simple Layout</div> <div>Few Pictures/Links</div> <div>Element Uniformity</div> ...

Remove an uploaded image utilizing an ajax uploader

I successfully implemented an AJAX loader for file uploads to the server. Everything is working well, but now I want to add a delete option for each image so that users can remove images before saving. The current layout of my images is as follows: To upl ...

Submitting a form from outside the form using a button in HTML: A step-by-step guide

I'm looking to submit a form using Angular on the functions next() and saveStep(). I'm unable to place next() and saveStep() within the form itself. I have set up my ng-click for next() and saveStep() below the form. When I submit the form fro ...

Integrate a variable called "counter" with the jQuery ID

I currently have the following code, which is functioning well. $('.clickable').click(function(e){ e.preventDefault(); $('#toggle').slideToggle(); }); $('.clickable1').click(function(e){ e.preventDefault(); $('# ...

Unable to apply a dotted border to a horizontal rule when a Bootstrap 5 link is present

As I strive to add a dotted border-style to the horizontal rule on my website, an interesting challenge arises when the Bootstrap-5 CDN is included. The border-style does not take effect as expected. Even on Codeply, the border-style works perfectly until ...

Generate a jQuery iframe once more by requesting it

How can I use jQuery to create an iframe tag and set it as the only content of a specific div? If the target div already has content, I want to replace it with a dynamically created iframe tag. This is what I have attempted so far: $(".someClass").click ...

Error occurred due to 'NoneType' object lacking the 'split' attribute while attempting to send a post request through Ajax to Django

I recently created a method in my views.py file to serve as an API for user login using Django 2.1. The method works perfectly fine when tested with POSTMAN, but I encountered an issue when trying to use AJAX to send requests to the API. Instead of receivi ...

Delaying Ajax request

I've encountered some strange behavior. After the initial ajax call triggered by selecting a city from a dropdown menu, I then have a continuous ajax call on a delay. The first call stores the selected city value in a global variable. $('.sele ...

What is the most effective method for developing and hosting a Python web application that can securely collect user input and store it in SQL databases?

Currently, I am embarking on a project to streamline data entry for myself. I have SQL tables set up in an Azure database, and I can effortlessly write Python code to add data to them. However, my next endeavor is to develop a web application that allows ...

Display a snippet of each employee's biography along with a "Read More" button that will expand the content using Bootstrap, allowing users to learn more about each team

I have successfully developed a Google App Script that links to a Google Sheet serving as a database. The application iterates through each row, and I showcase each column as part of an employee bio page entry. ex. Picture | Name | Title | Phone | Email ...

Connecting multiple promises using an array

After making an ajax call to retrieve an array of results, I have been attempting to process this data further by making additional ajax calls. However, when using Promise.all() and then continuing with .then(function(moreData){}), I noticed that the moreD ...

What obstacles may hinder the loading of CSS with PHP variables?

Visit this website for agriculture updates: I have utilized PHP to generate specific color schemes and styles. It seems to function correctly on all tested desktop laptops, however, it fails to load the CSS files (only loads HTML) on a Galaxy Note runnin ...

Can a small white pop-up be triggered by clicking a button?

While exploring the website , I noticed that clicking on Availability Zones opens a small window with text on the right side of the page. Is it possible to achieve a similar feature on a leaflet map without using JavaScript? This functionality would be tri ...

Refining an Ajax populated table

I am currently working on building a JavaScript filter for a table. The code snippet I am using is: var $rows = $('tr').not('#tr'); $('#search').keyup(function() { var val = '^(?=.*\\b' + $.trim($(thi ...