Sometimes with CSS values they have extensions like px when you get their values using jQuery. This is how you might go about getting the CSS value without the px. If you want to get the css propery values of a dom element such as margin then it will return the px aswell as the value.

For example:


Would return say: “360px”.

To get just the number value from the css property simple use this:

$('#content').css('left').replace(/[^-d.]/g, '');
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

Free Guide:

How to Choose the Right Charting Library for Your Application

How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.

  • dods


  • Frank

    great tip, used it :)

    Info/Memo: When using this code on a template that changes, i.e. sometimes the element is not in the html, you need to check against undefined and call replace afterwards, like that:

    if( typeof ($(‘#elementX’).css(“height”)) != “undefined”) {
    $height = $(‘#elementX’).css(“height”);
    // now the replace to get the pixel value
    if($height.replace(/[^-d.]/g, ”) > “16” ) {
    $(‘#elementX’).css(“margin-top”, “-15px”);

    If not some other scripting will break!

  • Monkey Raptor

    Thanks! This is sweet.

  • Matt Willett

    It should be: .replace(/[^-d.]/g, ”)

Special Offer
Free course!

Git into it! Bonus course Introduction to Git is yours when you take up a free 14 day SitePoint Premium trial.