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!
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.
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.
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 ).
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With