Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing width using jQuery .css() and em

Tags:

jquery

width

I need to change the width of an div using jQuery.

When I use the following, it works:

$('#footer').css('width', '100%');
$('#footer').css('width', '-=239');

When I use this, it works:

$('#footer').css('width', '100%');
$('#footer').css('width', '-=239px');

But when I use this it doesn't do anything:

$('#footer').css('width', '100%');
$('#footer').css('width', '-=21em');

Is there a way to make jQuery work with em? or to calculate em to px and set a variable for example and subtract that value?

edit: thanks everybody for correcting my spelling and code!

like image 980
TD_Nijboer Avatar asked Nov 27 '12 10:11

TD_Nijboer


People also ask

What does CSS (' width ') and width () Change width of?

The width CSS property sets an element's width. By default, it sets the width of the content area, but if box-sizing is set to border-box , it sets the width of the border area.

How do I set the width of an element in jQuery?

jQuery width() Method The width() method sets or returns the width of the selected elements. When this method is used to return width, it returns the width of the FIRST matched element. When this method is used to set width, it sets the width of ALL matched elements.

What is the difference between width () vs CSS width in jQuery?

The difference between . css( "width" ) and . width() is that the latter returns a unit-less pixel value (for example, 400 ) while the former returns a value with units intact (for example, 400px ).

What does innerWidth () method do in jQuery?

jQuery innerWidth() Method The innerWidth() method returns the inner width of the FIRST matched element. As the image below illustrates, this method includes padding, but not border and margin. Related methods: width() - Sets or returns the width of an element.

How many ways are there to change the width of the element in jQuery?

In jQuery, we have two ways to change the width of any HTML element. The jQuery css() method and width() method, below both the methods are described properly with the example.


1 Answers

i tried it and it is working i think you should check your code i have checked it in all browser and it is working fine px and em both working well you can use this code put this code in a dummy page and u can see this works

<style>
#footer{
background: black;
height:50px;
}
</style>
<script type="text/javascript">
$(function(){
    $('#footer').css('width', '100%');
    $('#footer').css('width', '-=210em');
});
</script>
<div id="footer"></div>
like image 110
The Mechanic Avatar answered Oct 21 '22 10:10

The Mechanic