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 7964149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:48:51+00:00 2026-06-04T05:48:51+00:00

I need to center text in a select list. There is no cross browser

  • 0

I need to center text in a select list. There is no cross browser and device method using CSS only, so ive used JavaScript to set a text indent.

The code takes the text from the select list, adds it to a span (which with css makes the text the same size), and measures the width of the span. With that value it takes some simple maths to work out what the text indent needs to be.

$(window).load(function() {
  function textIndentFunc () {
     textString = $('#from-1 :selected').text();
     $('#hidden').text(textString);
     textWidth = $('#hidden').width();
     inputWidth = $('#from-1 select').width();
     indentMy = (inputWidth / 2) - (textWidth / 2);
     $('#from-1').css('text-indent',indentMy); 
}
$('#from-1 select').change(function() {
    textIndentFunc();
});
textIndentFunc();
});

Most of the time this code works fine but sometimes doesn’t appear to do anything, in which case refreshing the page normally fixes it. Why is this code being so temperamental? Is it to do with having to manipulation the dom?

Thanks

  • 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-06-04T05:48:52+00:00Added an answer on June 4, 2026 at 5:48 am

    Do $(document).ready() not $(window).load()

    When invoking $(document).ready() your function is called once the DOM is processed and ready to be manipulated while load() will callback once all the assets are loaded(images, sounds).

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

Sidebar

Related Questions

I need to center text in a select list cross device and browser. This
I need to center text in a select list. The only example ive found
I need to horizontally center text in a select list, specifically for android and
I need to center a CSS menu that has an unknown width. I found
I need to center some text inside a <div> or <span> tag. I've set
Is there a way to have left-, center-, and right-aligned text on the same
I need to center grid view horizontally in my android layout.xml. I have searched
I need to find a point where a line (its origin is ellipse' center)
I am trying to center a Window to the owner window. I also need
Need to choose optimal data structure for fast work with list of locations and

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.