Issue with positioning hover effects on CSS links

I created a basic menu and I'm struggling to understand why the second level links always appear to the right of the first level links. Since the code is quite lengthy, here's a jsfiddle link for reference.

Here's what's happening in essence:

                <nav>
                    <ul>
                        <li><a href='#'>Link 1</a></li>
                        <li>
                            <a href='#'>Link 2</a>
                            <ul>
                                <li><a href='#'>Sub Link 1</a></li>
                            </ul>
                        </li>
                        <li>
                            <a href='#'>Link 3</a>
                            <ul>
                                <li><a href='#'>Sub Link 1</a></li>
                            </ul>
                        </li>
                        <li><a href='#'>Link 4</a></li>
                        <li><a href='#'>Link 5</a></li>
                        <li><a href='#'>Link 6</a></li>
                        <li><a href='#'>Link 7</a></li>
                        <li><a href='#'>Link 8</a></li>
                        <li>
                            <a href='#'>Link 8</a>
                            <ul>
                                <li><a href='#'>Sub Link 1</a></li>
                            </ul>
                        </li>
                    </ul>
                </nav>

The issue is that Link 2 and its Sub Links hover to the right side, while I want Link 8 Sub Link to instead hover on the left. Currently, it extends beyond the boundaries of the wrapper div. Ideally, if there's enough space (256px) to the right, the hover should be on the right. If not, then the hover should be on the left. Also, top links at the far right should have their sub links hover on the left side.

Answer №1

You might find this solution helpful: http://jsfiddle.net/y7q4ptwv/8/

Here is the HTML code:

<ul class="right" >
 <li><a href='#'>Sub Link 1</a>
   </li>
    </ul>

And here is the CSS code:

.right{
 left:0;
}

Answer №2

My choice

 nav ul li:last-child ul { right: 0; }

was motivated by my lack of concern for IE8 or older versions.

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

Conceal elements when they are too small without the use of JavaScript

I want to be able to hide content when the window size is below a certain point. After finding an example at (http://css-tricks.com/dynamic-page-replacing-content/), I still couldn't figure out why it actually works. <div class="container"> ...

Encountered a problem with Vuetify's sass variables

I am currently working on my Vuetify3 project where I need to customize a select dropdown from a library to match the style of Vuetify's select dropdown perfectly. After inspecting Vuetify's select box, I decided to replicate its styles by using ...

Error message "Module not found in Vue" occurs when changing image URL to an online image

I am currently working on a dynamic image display feature where I retrieve image names as strings from a backend. To manage a large number of images, I plan to store them online while maintaining the same logic for displaying them. Previously, I achieved t ...

Scrolling to the bottom of the page triggers jQuery to load additional content

Is your jQuery script not working properly when attempting to load more content upon reaching the bottom of the page? It seems to be functional on Safari/iPad and Android Mozilla browsers, but encountering issues on default Android and Chrome browsers. ...

Utilize the Appy Wordpress Widget in your designated widget region

This is my first time working with WordPress, and I am attempting to add a custom Widget to an HTML box. Here is the HTML: The blank News box Following some tutorials, I created a new Widget area inside my functions.php file using the following code: &l ...

When it comes to printing, the @media rule for portrait orientation will not be effective if landscape orientation is also indicated

My goal is to create a div that will occupy the entire A4 page when printing, regardless of whether portrait or landscape mode is selected in the print dialog window. test.html: <!DOCTYPE html> <html lang="en"> <head> <me ...

Tips for updating the content of a div with the click of another div

Is there a way to dynamically change the content of a div upon clicking on a menu item? Here is the current layout for reference: https://i.stack.imgur.com/nOnlQ.png Below is the CSS and HTML code snippet: body { background-color: #555657; ...

Align the bottom of an image with the top of text to achieve perfect vertical alignment

My goal is to arrange numerous images in a row, with text displayed below each image. Each block of text may consist of multiple lines and should be centered. While the heights of the images may vary, their widths will remain consistent. It is important th ...

Is it possible to create an HTML interface with Google Workspace Add-ons?

Recently, I've been on a mission to create a Google Workspace Add On that can display an iFrame. My journey led me to explore the documentation for Google Apps Script and Google Add Ons. From what I've gathered so far, it seems that I'll ne ...

The div is set to a position of absolute, causing the overflow-y property to be set to auto. As a

I am facing an issue with a div that has absolute positioning nested inside other divs. If you take a look at the code snippet below from http://jsfiddle.net/d8GYk/, you'll notice the styling properties applied to the div. <div style="position: ...

The alignment of flex items is not consistent in Webkit browsers

One of the issues I am facing involves a text field that contains a cogwheel symbol which opens a popup menu with different search options. The layout is set up using a flexible box model for certain elements. After testing in Firefox, the display is as e ...

The JavaScript function is not functioning properly, whereas another function is working as intended

I have created a HTML form with 2 buttons and a table. Each row in the table consists of a checkbox and 2 text fields. The buttons allow users to add and remove rows from the table. The remove button only applies to rows where their checkbox is checked. T ...

Adding a CSS shadow to an element positioned beneath another

I'm attempting to recreate a shadow effect in HTML similar to one I created in Photoshop: https://i.sstatic.net/fBcBl.png I believe a basic HTML structure like the one below should suffice, but I'm unsure: <div id="anotherMask"> <di ...

Adjusting the slider with jQuery and CSS to extend beyond 100% while remaining within the div boundaries

Currently, I'm working on customizing a jQuery slider. My goal is to achieve the same functionality as the jQuery UI slider where you can set a max value like "2500" and the slider will smoothly adjust without going outside of the specified div or scr ...

steps for applying a vertical-align:bottom with an additional 10px offset

Can you show me how to have two inline-blocks with the second one positioned 10 pixels up from the bottom? I'm not sure how to do this. Here is a snippet of my code: .test1{ display:inline-block; border: 1px solid red; min-height: 50px; } .tes ...

Get rid of the folder from the URL using an <a> tag

I have both an English and French version of my website located at: *website.com/fr/index.php *website.com/index.php Currently, I have a direct link to switch between the two versions: -website.com/fr/index.php -website.com/index.php. However, I ...

Arrange two internal divs on top of one another with the option to maintain the float

While searching for a solution to my issue, I noticed that most responses involved removing the float property. However, since I am new to CSS, I would like to explore using float and see if it's the best approach. Here is the fiddle I've been w ...

Concealment Based on Surroundings

I'm currently working on implementing a new feature called 'context-based hiding' which involves hiding content based on mouse actions. I've been struggling to figure out how to create this feature and came across a website that has it ...

Event Listener for Spelling Quiz Buttons: Check Correct and Incorrect Answers

I am currently in the process of developing a spelling quiz for a project website using HTML, CSS, and JavaScript. The idea is to present the user with a word that has two missing letters indicated by underscores. The user then selects the correct answer ...

Is there a way to customize the hover effect on this navigation link in Bootstrap that includes a span and an icon from Bootstrap?

<li class="nav-item mt-3"> <a href="#" class="nav-link px-2 d-flex justify-content-start align-items-center"> <i class="nav-icon bi bi-calculator-fill fs-4"></i> ...