Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5842169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:58:58+00:00 2026-05-22T11:58:58+00:00

How can I make all the elements from this list have the background in

  • 0

How can I make all the elements from this list have the background in center no matter of the width of the element.

<ul id="menu">
<li><a href="#">Name</a></li>
<li><a href="#">Name</a></li>
<li><a href="#">Name</a></li>
<li><a href="#">Name</a></li>
<li><a href="#">Name</a></li>
</ul>

My css is like this:

ul#menu li a { background:url(../images/header-img.png) 0 0 no-repeat; padding:40px 0 0 0; display:block; font-size:12px; color:#000;}

I tried like this, but no luck:

$("ul#menu li a").each(function(i){
    var value = $(this).width()/2;
    $(this).css('backgroundPosition', value + ' 0');
   });

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:58:59+00:00Added an answer on May 22, 2026 at 11:58 am

    Well Florescu,

    I finally figured it out.

    The problem is that links don’t have any width, firebug ’em and you’ll see that their width is computed as ‘auto’ unless you promote them to block level elements with explicit widths. So the solution? We find the width of the text contained in ’em.

    Here’s the solution, http://jsfiddle.net/aPuhP/1/; and the explanation,

    We insert a dummy span tag to be able to calculate the element’s width,

        var source = $(this).html();
        var sensor = "<span>" + source + "</span>";
        $(this).html(sensor);
    

    ..we then calculate the width and run calculations required to get the desired width (the -20 is to compensate for the sprite’s distance from the left edge of the image),

       var width = $(this).find('span:first').outerWidth();
       var v = width/2;
       $(this).css('backgroundPosition', v - 20 + 'px 0');
    

    ..and finally, we get rid of the dummy span tag,

       $(this).html(source);
    

    Hope this helps.

    PS: Kudos to Calculating text width.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I make a style have all of the properties of the style
If all tables I want to delete from have the column gamer_id can i
How can you make the experience with Eclipse faster? For instance: I disable all
Can someone help me figure out the following make file? BINS=file1 file2 file3 all:
We'd like to make a few basic hook scripts that we can all share
I have the following: <ul style=width: 300px; list-style-type:none> <li> <table style=width:100% border=0 cellpadding=0 cellspacing=0
I can make a DAO recordset in VB6/Access do anything - add data, clean
I can make Firefox not display the ugly dotted focus outlines on links with
In C# you can make a block inside of a method that is not
so that you can make your program concurrent easily in the future.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.