What is the best way to horizontally align an image beneath a navigation menu that is built using <ul> tags?

Seeking a way to center an arrow graphic under the selected item in a simple CSS drop-down menu? Considering using the :after selector, but currently being used for drawing dividing lines between menu options:

HTML:

<ul>
    <li>Option Zero</li>
    <li>Option One Is Longer</li>
    <li class='selected'>Option Two
        <ul class='sub-menu'>
            <li>Sub One</li>
        </ul>
    </li>
    <li>Option Three Is Not the Best</li>
</ul>
<div class='arrowholder'>
    <div class='arrow'></div>
</div>

CSS:

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing:    border-box;
    box-sizing:         border-box;
}

ul {
    list - style: none;
    width: 100 % ;
    background: red;
    height: 50px;
    padding: 10px 10px 0 10px;
    margin: 0px;
}
li {
    float: left;
    margin - top: 10px;
}
li: after {
    content: "|";
    margin: 0 8px;
}.sub - menu {
    display: none;
}

.arrowholder {
    clear: both;
    width: 100 % ;
    background: green;
    height: 50px;
}
.arrow {
    width: 50px;
    height: 50px;
    background: url(http: //upload.wikimedia.org/wikipedia/en/1/1c/Up_Arrow_Icon.png) no-repeat;border:solid 1px white;
}

JSFiddle: http://jsfiddle.net/pnoeric/MrW58/2/

Already have a jQuery-based solution implemented (seen on the fiddle), but facing cross-browser compatibility issues due to different return values from position() method in Chrome vs Firefox.

UPDATE: jQuery-based solution works perfectly without the Chrome JavaScript Console open. The position() method returns strange values when the console is open, requiring it to be closed for accurate results.

Preferably seeking a 100% CSS solution or alternatively, one that includes some jQuery support. How can this be achieved?

Answer №1

To add an arrow to the selected list item, you can utilize a pseudo-class. See below:

.selected:after {
    content:'';
    position:absolute;
    width:50px;
    height:50px;
    background:url(http://upload.wikimedia.org/wikipedia/en/1/1c/Up_Arrow_Icon.png) no-repeat; 
    top:30px;
    left:50%;
    margin-left:-25px;
    border:solid 1px white;
}

Remember to ensure that your list item has position:relative.

Check out this demo for reference: http://jsfiddle.net/9u2J6/4/

Answer №2

Instead of complicating things, just add a background image to each list item and adjust the position with background-position:bottom center;. Make sure to set background-repeat:no-repeat; and use padding-bottom to prevent the content from overlapping with the arrow.

Answer №3

After some troubleshooting, I discovered that my jQuery solution was working perfectly unless Chrome's JavaScript Console panel was open. Interestingly, when the panel was open, the position() method became confused and stopped functioning properly. It was frustrating to realize this after wasting so much time, but at least I was able to identify the issue.

To address this problem, I manually adjusted the arrow graphic's position to center it correctly.

$(function() {
    p = $('.current').position();
    w = $('.current').width();
    naw = $('.arrow').width();
    f = Math.floor(p.left + (w/2) - (naw/2))-20;
    $('.arrow').css('margin-left', f+'px');
});

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

"Unlocking the power of event handling with jQuery plugins: A beginner's

Hey there, I'm new to plugin development and diving in head first. The documentation has left me a bit confused, so I'm opting for a trial-and-error approach... My current project involves creating a basic form validation plugin. (function($){ ...

The CSS property 'clear:both;' is not functioning properly on IE7 browsers whereas works fine on IE8/IE9, Firefox, Chrome, and other browsers

I have been receiving feedback from visitors about some issues on one of my websites. Those who reached out to us are using IE7 on Windows XP. I was able to recreate the problem by using IE7 or by mimicking it in compatibility mode with IE7 document mode o ...

Error encountered while making a Jquery Ajax request to Rest service

I have created a RESTful Web Service: import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/todo") public class TodoResource { @GET @Produces({MediaType.APPLICATION_JSON}) public To ...

Creating a 3-column div with varying sizes of text in another div at the center position

I am attempting to create a 3-column layout using only DIVs, but I am facing some challenges. If I were to use tables in the traditional HTML 4 way, I could achieve this: <div style="width:100%"> <table width="50%" align="center"> ...

Establishing the REM value for all CSS properties

My goal is to implement the use of rem units throughout my website. However, relying on the standard rem-to-px conversion rate doesn't feel very intuitive: 10px = 0.625rem 12px = 0.75rem 14px = 0.875rem 16px = 1rem (base) 18px = 1.125rem 20px = 1.25r ...

The current layout of the div is hindering the ability to switch from vertical scrolling to horizontal scrolling

I'm currently utilizing this scroll converter tool to transform vertical scrolling into horizontal scrolling. However, despite correct script inclusion and initialization, the conversion is not working as expected. It seems like there might be an issu ...

Converting a PHP timestamp to a jQuery-compatible format

Can someone help me find the equivalent function in jQuery that will give me a time format similar to this: date( 'Y-m-d\TH:i:sP'); //the output is like this. 2013-10-30T18:10:28+01:00 I am looking for this specific format in jQuery to use ...

Is it possible to display a variety of color schemes in just one console.log()?

My task involves working with an array of hexadecimal values, "colors": ["#d5dd90","#e6bb45","#ef9770"] To log these out in different colors, I used the following method: colors.forEach((value)=>{ console.log(& ...

Creating a navigation bar that stays fixed at the top of

Hey there, currently I am utilizing a combination of jquery and css to affix my navigation menu at the top when scrolled. However, the problem is that my navigation menu is positioned beneath a div with a height set in viewport units (vh). The jquery scr ...

Is there a limit to the number of else if statements I can use? The final else statement

How can I enhance this code snippet? The last else if statement is not working, despite my efforts to fix it. var selectedDntTyp = $("#donationTypDD"); if (selectedDntTyp.length > 0) var DropInitValue = selectedDntTyp.val(); if(DropInitValue == &apos ...

Calculating the total amount by combining the quantity and price fields in

I currently have multiple tables generated dynamically using PHP <table class="table11"> <tbody> <tr> <td>Name</td> <td class="quantity">2</td> <td class="price">20</td> & ...

A dynamic jQuery approach for uploading multiple files on the fly

I am working on a project that involves uploading images through a form. I am unsure of the exact number of pictures that will need to be uploaded at one time. Is there a solution using jQuery/AJAX to add file upload fields dynamically to the form? ...

Every time I attempt to send a PUT request using AJAX, I encounter an error

After developing HTML and AJAX code to update a camera password within my network, I encountered an issue upon form submission. "error" (net::ERR_EMPTY_RESPONSE) Surprisingly, the PUT request functions perfectly when tested with POSTMAN, showing a stat ...

Is it possible to assign the margin-bottom property of one element to be equal to the dynamic height of a sibling element?

I am in the process of creating a website that features a fixed (non-sticky) footer placed behind the main content using the z-index property. The footer only becomes visible when the user scrolls to the bottom of the page, achieved by assigning a margin-b ...

What is the best way to add or delete data when specific radio buttons are chosen?

Hey there, I'm facing an issue where the data is being appended regardless of which radio button is selected. Can someone help me with a solution on how to properly add and remove data based on the selected radio button? $( document ).ready(functio ...

What is the reason behind the overflow in the HTML body?

I am currently honing my skills in front-end development by attempting to replicate the functionality of . Everything seems to be working fine, except for the fact that there is an overflow issue identified within the <body> element when inspecting w ...

How to dynamically change the position of an input field within a form using JavaScript and jQuery

I have a form displayed on my website that looks like this: input a input b input c I am wondering if it is achievable to rearrange the order of inputs using jQuery, like so: input a input c input b However, it is important that the data is still ...

The total height of an HTML element, which takes into account the margin of the element itself

Is there a way to accurately calculate the height of an element including margins, even when dealing with child elements that have larger margins than their parents? HTMLElement.offsetHeight provides the height excluding margin, while this function from ...

Display fewer search results initially and have the option to view more when hovering

I am working with a PHP function that generates a ul element. <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> I am looking to display the list in a li ...

Tips for increasing the height of a popover when clicked

When a user focuses on the password input, a popover displays information. At the bottom of the popover, there is a link. How can I make the popover expand when the user clicks on this link? I have tried adding an !important class for the height value, us ...