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

  • SEARCH
  • Home
  • 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

The following is a cut from my registration page. How can i make all
now I can make it work with user-profile.tpl.php page,all this page does is to
how can I make this jquery script close all previously opened children when entering
How can i make a SQL query from actionscript? and render all the results
I have to list several elements that are coming from a database source A
All I can make out is that one of them is the BC for
How can we make universal light box i mean same code for all light
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
I am trying to make a movie with openCV. I can put all the
I have the following: <ul style=width: 300px; list-style-type:none> <li> <table style=width:100% border=0 cellpadding=0 cellspacing=0

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.