Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set width of a div in percent in JavaScript?

Is it possible to set the height/width of an element in percent using JavaScript or jQuery?

like image 232
Omu Avatar asked Mar 17 '10 08:03

Omu


People also ask

Is a Div 100% width by default?

auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent. so setting the 'width' to 'auto' would be better? Yes, but that's the default anyway.

How do I change the width of a percentage in HTML?

Try It: Set the width of an image using a percentage and resize your browser window. For the width attribute, you can also use percentages, where 100% is the total space available. So, if you wanted an image to be one quarter the width of the window, you could set width to 25% (one quarter of 100% is 25%).

What does width 100% do in CSS?

width: 100%; will make the element as wide as the parent container. Extra spacing will be added to the element's size without regards to the parent.


1 Answers

document.getElementById('header').style.width = '50%'; 

If you are using Firebug or the Chrome/Safari Developer tools, execute the above in the console, and you'll see the Stack Overflow header shrink by 50%.

like image 146
Daniel Vassallo Avatar answered Sep 19 '22 23:09

Daniel Vassallo