Setting width as a percentage using jQuery

How to set div width as a percentage using jQuery?

+67
jquery width
Jan 23 '10 at 19:13
source share
3 answers

Using the width function:

$('div#somediv').width('70%'); 

:

 <div id="somediv" /> 

at

 <div id="somediv" style="width: 70%;"/> 
+104
Jan 23 '10 at 19:15
source share

Hematn

If your variable is a percentage:

 var myWidth = 70; $('div#somediv').width(myWidth + '%'); 

If your variable is in pixels and you want the percentage to occupy the parent:

 var myWidth = 140; var myPercentage = (myWidth / $('div#somediv').parent().width()) * 100; $('div#somediv').width(myPercentage + '%'); 
+11
Jul 31 '15 at 3:10
source share

Here is an alternative that worked for me:

 $('div#somediv').css({'width': '70%'}); 
+10
Aug 24 '15 at 13:54 on
source share



All Articles