Creating a unique dropdown menu with HTML and CSS: Learn how to include a button in between each main link without disrupting the layout

I successfully implemented a navigation menu on my website by following this helpful tutorial.

Below is the CSS code I used:

ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
    background-color: #F2C777;
}

li a {
    display: block;
    padding: 10px;
    text-align: center;
    color: #7C785B;
}

li a:hover {
    background-color: #EC8C65;
}

li {
    display: inline-block;
}

.dropbutton:hover .droplinks{
    display: block;
} 

.droplinks {
    position: absolute;
    background-color: #F2D299;
    min-width: 140px;
    display: none;
}

.droplinks a {
    padding: 10px;
    display: block;
}

And here is the HTML code:

<nav>
    <ul>
        <li><a href="">Home</a></li>     
        <li class="dropbutton"><a href="">Products</a>
            <div class="droplinks">
                <a href="">Widgets</a>
                <a href="">Cogs</a>
                <a href="">Gears</a>
            </div>
        </li>
        <li class="dropbutton"><a href="">Services</a>
            <div class="droplinks">
                <a href="">Handshakes</a>
                <a href="">Winks</a>
                <a href="">Smiles</a>
            </div>
        </li>
        <li><a href="">Shop</a></li>
        <li><a href="">Contact</a></li>   
    </ul>
</nav> 

However, I encountered a problem with the navigation menu on smartphones. It is difficult to navigate to the drop-down links without clicking on the first-level link. I want to add a clickable area between each first-level link that allows users to access the drop-down menu without navigating away from the page.

I tried various solutions, including positioning a button after the link, but it affected the alignment. Altering the "display" attribute and using a "div" tag also did not resolve the issue.

How can I insert a clickable button between each first-level link in the multi-level drop-down menu while maintaining horizontal alignment and a single line layout?

My goal is to achieve a similar navigation bar to the one on this website, but without having the sub-link indicator inside the first-level link.

N.B: I am looking for a solution that does not involve JQuery. Please avoid providing solutions involving this library.

Here is the JSFiddle link for reference.

Answer №1

I have made some updates to your code on jsfiddle by implementing a pure CSS solution. I utilized a pseudoelement :after, which includes a unicode character that acts as an arrow to indicate the dropdown menu. The dropdown menu is shown when the arrow is clicked on a mobile device, triggered by the :focus selector.

.dropbutton:after {
    content:"\25BC";
    display:inline-block;
    vertical-align:middle;
    margin:0 15px 0 5px;
}
.dropbutton:after:focus > .droplinks {
    display:block;
}

Check out the updated jsFiddle here

Answer №2

Is the first level link on your website actually taking users to a relevant page, or is it just redirecting to a menu page? Maybe consider changing the code so that clicking the link opens the menu without redirecting.

By disabling the redirect behavior and allowing the menu to open on click, users on smartphones can easily navigate, while desktop users might find clicking to be less functional.

I wanted to leave this as a comment, but unfortunately, I don't have that privilege yet...

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

Maintain the fixed position of the Google Map <div> while allowing background <div>s to scroll

I'm having trouble keeping a Google Map div fixed on the screen while allowing my background, consisting of multiple scrolling divs with content inside, to scroll as normal. My attempt to wrap the map div in another fixed div resulted in the map disa ...

Having trouble with my JSFiddle code not functioning properly in a standard web browser for a Google Authenticator

Here is the code snippet that I am having trouble with: http://jsfiddle.net/sk1hg4h3/1/. It works fine in jsfiddle, but not in a normal browser. I have included the necessary elements in the head to call all the external files. <head> <scri ...

Methods for setting the value of a scope variable within a controller's function

I have a goal of presenting a page that calculates a balance in the background and displays it. To achieve this, I decided to separate the balance into its own directive. Here is the directive I have created: app.directive('smBalanceInfo', fun ...

Activate event in jQuery when clicking on a blank area, away from any div element

I need help figuring out how to hide a div when empty space on the margins of the page is clicked, as opposed to just any area outside of the div. I've managed to achieve this functionality when certain other divs are clicked, but I'm stuck on im ...

Picture shifting when adjusting window size

I have been attempting multiple times to prevent an image on my website from moving when resizing the window, but I have not been successful. All I want is for this image to remain in the exact same position, no matter the size of the window. The image " ...

Styling text using CSS depending on the displayed text

Utilizing Awesome Tables (AT), I am extracting data from a Google Sheets document to display on a website. The HTML template in the sheets is used for formatting the data, specifically focusing on the table output with inline CSS styling. Since the templat ...

Creating Spinning Text Effects in Internet Explorer with CSS

Inside a list item (<li>), I have some text that I want to rotate 270 degrees. Direct methods in FireFox, Safari, Chrome, and Opera work fine for this, but when it comes to IE, there is no direct support. I've tried using filters like matrix and ...

Exploring Next.js: A beginner's attempt at displaying basic JSON data

I'm having trouble updating and displaying the content of my JSON data in my React app. Despite trying various solutions, including adjusting the return value of functions and seeking help on forums, I still cannot get rid of the issue where an empty ...

Creating an oval cutout shape using CSS in a div element

I am currently facing some challenges creating the design depicted in the image above, particularly with the oval shape. Let me provide an explanation: The menu bar is designed as a div with a subtle linear gradient that transitions from dark grey to a l ...

Proper alignment of multiple elements with Bootstrap

I am currently incorporating Bootstrap panels into my project, and I have a design mockup that looks like this: https://i.sstatic.net/pHArl.png The Profile text and progress bar need to be aligned to the left, while the collapse icon should be aligned to ...

The significance of tabindex in CSS

Is it possible to manipulate tabindex using CSS? If so, which browsers support this feature and on what types of elements? UPDATE I am interested in capturing keydown events on a div element. After researching keyboard events on http://www.quirksmode.org ...

Hidden Password Field Option in HTML

My HTML password textbox has the input type set as password, but I can still see what is being typed. This shouldn't happen as password inputs should be masked. Can someone please advise me on how to resolve this issue? To replicate, copy the code be ...

Is there a way to temporarily toggle classes with jQuery?

Incorporating ZeroClipboard, I have implemented the following code to alter the text and class of my 'copy to clipboard button' by modifying the innerHTML. Upon clicking, this triggers a smooth class transition animation. client.on( "complete", ...

Switching the theme color from drab grey to vibrant blue

How can I change the default placeholder color in md-input-container from grey to Material Blue? I have followed the instructions in the documentation and created my own theme, but none of the code snippets seems to work. What am I doing wrong? mainApp. ...

What is the best way to store checkbox statuses in local storage and display them again in a JavaScript to-do list?

I'm currently working on a to-do list application using basic JavaScript. One issue I'm facing is saving the checked status of the checkbox input element and displaying it again after the page is refreshed. Since I'm still learning JavaScrip ...

What is the best way to ensure a PrimeVue TabPanel takes up the full vertical space and allows the content within the tabs to be scroll

I have created a sandbox demo to illustrate my issue. My goal is to make the content of tabs scroll when necessary but fill to the bottom if not needed. I believe using flex columns could be the solution, however, my attempts so far have been unsuccessful. ...

Unable to use addEventListener on media queries exceeding 768px

When testing the site on Firefox 49 and Chrome 63, I encountered the same issue. Clicking on each card worked fine on iPhone4, Galaxy 5, and iPhone 8 using Chrome. However, after switching orientations from 640px to 320px portrait view, the top card would ...

Splitting HTML content into nodes using PHP XPath (including empty nodes)

I am currently attempting to separate the HTML string into individual nodes, along with their text content if applicable. Here is the HTML string that I am working with: <p>Paragraph one.</p> <p><strong>Paragraph <em>two</ ...

What steps can I take to incorporate a dropdown feature into my existing menu design?

I am currently working on a navigation menu that includes various subjects. One of the subjects is titled Equipment, and when I hover over it, Throwables and Gear appear. However, I now want to add another dropdown menu that opens when I hover over Throwab ...

Is it necessary to convert SCSS into CSS in React?

I have a query about whether it is necessary to first compile our scss files into css and then import the css files into our react components, or if we can simply import the scss directly into the components? I've successfully imported scss directly ...