Detecting Width: Auto in Jquery

Detecting width: auto in jQuery

I don't believe it's possible for the moment. At least not in any other browser than IE. IE implements element.currentStyle which represents styles at they were written in the CSS file. On the other hand, the rest of the browsers implement window.getComputedStyle which returns the computed values of those styles. That's what you receive there, a numeric value instead of auto.

The only way around it would be to parse CSS declarations from document.styleSheets.



JQuery: How to detect mobile width and change my css to it's with using jquery?

No need of using Javascript/jQuery, check the below points.


  1. Use width in percentages so, when resized it'll auto adjust
  2. Use media queries for resolution specific styles
  3. When setting properties from Javascript, use quotes around property name or use camelcase. Ex. maxWidth, or 'max-width'


@media (max-width: 600px) {
.page {
width: 200px;

  1. Create separate CSS file for various resolutions and load them for such resolutions


<!-- CSS media query on a link element -->
<link rel="stylesheet" media="(max-width: 600px)" href="mobile.css" />


If you still want to use Javascript, call your function on window resize event.

window.onresize = responsiveFn;

Use max-width in quotes

jQuery('.page').css('max-width', width);

JQuery - Get width: auto from inline style


Or for more jquery's way:


Auto detect screen height

You can set height dynamicly with codes below:

var height = $(window).height();
$('#FrontImg').css("height", height + "px");

jQuery: How to detect window width on the fly?

Changing a variable doesn't magically execute code within the if-block. Place the common code in a function, then bind the event, and call the function:

$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
var $pane = $('#pane1');

function checkWidth() {
var windowsize = $window.width();
if (windowsize > 440) {
//if the window is greater than 440px wide then turn on jScrollPane..
// Execute on load
// Bind event listener

Resize div and dynamically detect width

Worked it out in the end:

//DIV with .change class
$('#changelog').text(' W: ' + $('.change').width() + 'px , H:' + $('.change').height() + 'px ');

$(window).resize(function () {
$('#changelog').text(' W: ' + $('.change').width() + 'px , H:' + $('.change').height() + 'px ');

Thanks all.

Javascript auto width detect then add two pixels to it

You can use width as setter and getter. See the jQuery API:

$('.navbar-nav li a').each(function(index){ $(this).width($(this).width() + 2);});
<script src=""></script><ul id="main-menu" class="nav navbar-nav">  <li><a href="/" class="nav-link">Home</a></li>  <li><a href="/" class="nav-link">About</a></li>  <li><a href="/" class="nav-link">Contact</a></li></ul>

How to detect window width changes without resizing

You can detect both events and just execute code when it's a width change:

var width = $(window).width();
if($(this).width() != width){
width = $(this).width();

Related Topics

Leave a reply