Unable to retrieve background position using jQuery .css() without needing !important in CSS

Could someone please explain why the jQuery .css() method is returning 0% 0% for elements with a background position defined in the inspector and style sheet?

I'm puzzled as to why adding !important to the rule makes it work, but not without.

The jQuery Code:

;(function stackoverflowExample(){
  "use strict";
  $('div').each(function(){
    var origPos = $(this).css('backgroundPosition');
    alert('original position: '+origPos); // Why?! false return value 0% 0%
// unless an !important used in the stylerule...
  });
})();

The CSS Styles:

#test1, #test2 {
    padding: 65px;
    margin: 50px;
    color: #fff;
    font-size: 3rem;
    text-transform: uppercase;
    background-position: 500px 500px !important;
    box-shadow: inset 1px 10px 10pc #000;
  }
  #test1 {
    background: url(http://placehold.it/450x250);
    margin-bottom: 900px;
  }
  #test2 {
    background: url(http://lorempixel.com/b/1800/800/);
  }

The Markup (jade):

main 
    h1 This is a parallax scrolling demo
    div#test1 test1
    div#test2 test2

In this demo, jQuery alerts a 0% 0% background position. Check out the lack of !important this time. Strange..

http://codepen.io/nicholasabrams/pen/qEVbpd

Now in the demo below, I've added !important to the background positions on #test1 and #test2, and lo and behold, jQuery now returns the correct values.

http://codepen.io/nicholasabrams/pen/ogoLyV

If you have any insights or need more clarification, feel free to let me know!

Answer №1

The reason for the issue is that you are defining the background image after specifying the background position, while also using the shorthand property "background" which covers all background attributes, including background-position.

Consider this alternative:

background-image: url(http://placehold.it/450x250);

This should resolve the problem.

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

How to create a stylish border line to separate rows using CSS

Having some trouble with HTML code... <table class="table table-condensed order-statistics"> <tr class="order-statistics-row"> <div class="row"> <div class="col-lg-4"> Order ID: </div> <div ...

Continuously update scrolling functionality

Could you please provide more information about the solution mentioned in the following question? I am facing a similar issue. jQuery’s css() lags when applied on scroll event Regarding solution #3 ->> To ensure continuous updates, it is suggeste ...

Guide to increasing the space between circle segments in a donut chart using CSS

I'm currently working on a donut chart in StackBlitz, the link to which can be found here. My goal is to create gaps between each circle segment of the SVG donut chart to improve accessibility and make it easier to distinguish different parts of the ...

Tips for incorporating a background image using bootstrap

I am facing an issue with a large background image of size 1600 X 3700. When I try to add bootstrap to my project, the background image disappears and only the first 100px are visible. Can anyone provide guidance on the correct way to use bootstrap with ...

What is the best way to apply a semi-transparent background to my navigation bar, while maintaining a blue background across my entire page?

Currently, I have a background set for my website along with a navigation bar that displays my name and additional information. I am looking to make the background of this navigation bar semi-transparent so that it is possible to see through it. I tried ...

Ways to reload a webpage from the bottom without any animation

It seems common knowledge that refreshing a webpage on mobile devices can be done by pulling down from the top. However, I am looking to shake things up and refresh the page by pulling up from the bottom instead. And I prefer no animations in the process. ...

The issue with mobile responsiveness is specifically occurring on the Samsung Note 21 and iPhone 14 devices, while functioning properly on all other devices. Attempts to reproduce the error using Chrome dev tools

I'm facing a unique issue at the moment. The landing page I've created is highly responsive on all mobile devices except for the web owner's device: . Interestingly, it appears differently on the web owner's Samsung and iPhone devices:S ...

designing a corner tab element

I'm aiming to create a triangular button in the upper-right corner of my website using fixed positioning. It will be an icon on top of a background color with a hover effect. I'm curious if it's possible to achieve this with an angled div or ...

The HTML code starts with a fluid middle column in a 3-column layout, while the other two columns have fixed widths

I am interested in creating a traditional 3-column layout similar to this: | | | | |L | M |R | | | | | I have been tasked with using the following html structure: as depicted, the Main div is the initial node of #container <body> ...

Images in CSS accordion not displaying correctly in WordPress website

It seems like I am overlooking a simple solution here, but it's escaping me. I'm attempting to make a CSS accordion function where each section links to a page. If you want to view the source refer to this link Here is my test site URL: this l ...

What is the best way to implement complex input filtering in JavaScript?

Is it possible to filter input content in this format? The input should accept 16 characters, separated into 4 groups by ":" like this: 0123:0055:02AC:01FA I divided them into 4 groups for later use, but I want to set a filter with the minimum value bei ...

Issue encountered while compiling CSS for a map with @each function in Sass

I am currently in the process of developing a set of classes to define image styles based on specific skills. Colors associated with each skill are stored in a map. $skills-color: ( default : #D8D8D8, 6 : #2E80EC, 5 : # ...

Utilizing jQuery to Trigger a Click Event on an Element Without a Class

What is the best way to ensure that the "click" event will only be triggered by an href element unless it does not have the "disablelink" class? Avoid processing the following: <a class="iconGear download disablelink" href="#">Download</a> P ...

What is the best way to extract a thumbnail image from a video that has been embedded

As I work on embedding a video into a webpage using lightbox, I'm looking for advice on the best design approach. Should the videos be displayed as thumbnails lined up across the page? Would it be better to use a frame from the video as an image that ...

Error message indicating that the jQuery script has not been properly defined in a Wordpress environment

Seeking assistance with a JavaScript issue on my WordPress site. I have added an external JS file but it's not functioning properly and throwing errors in the code below. Any help to resolve this would be greatly appreciated. var clock; jquery(docu ...

Harnessing the Power of JSON in Rails to Enhance Your jQuery UI Autocomplete Form

In the context of a rails application, I have been exploring different methods to fetch data from a JSON file. Despite my efforts, I have not been able to find a solution that suits my requirements. The static data I have is quite extensive, consisting of ...

The dynamically generated dropdown menu is not functioning properly when attempting to make a selection

My website features an input field where users can input usernames to add collaborators to a project. Through AJAX, I dynamically generate a dropdown list that fetches registered usernames from the database as the user begins typing: The HTML code for the ...

Adjust the marginLeft and marginRight values in a JavaScript statement within a Highcharts configuration

Is there a way to adjust the chart marginLeft and marginRight using Highcharts, and then redraw it in JavaScript? I am looking to change the chart margin dynamically at different points in my code. http://jsfiddle.net/ovh9dwqc/ I attempted to do this wit ...

Using JQuery to find and update elements in the DOM

My goal with JQuery is to have the select attribute removed from the Default.aspx li element and added to the Feedback.aspx li element when a user clicks on a link like Feedback.aspx. <ul id="main_menu" class="main_menu ui-sortable"> < ...

Tips for creating an expandable div with floated content

I am looking to create a flexible div that expands based on its content. Inside this div, there are two floated left divs with fixed widths. These inner divs should also expand according to their text content. To clear the floats, there is another div wi ...