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

后端 未结 7 906
我寻月下人不归
我寻月下人不归 2020-12-09 08:09

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

相关标签:
7条回答
  • 2020-12-09 08:59

    The question is what do you want the div's height/width to be a percent of?

    By default, if you assign a percentage value to a height/width it will be relative to it's direct parent dimensions. If the parent doesn't have a defined height, then it won't work.

    So simply, remember to set the height of the parent, then a percentage height will work via the css attribute:

    obj.style.width = '50%';
    
    0 讨论(0)
提交回复
热议问题