Set element width or height in Standards Mode

JavascriptSizeStandardsElementQuirks Mode

Javascript Problem Overview


Is it possible to set width or height of HTML element (ex. <div>) in JavaScript in Standards Mode?

Note the following code:

<html>
<script language="javascript" type="text/javascript">
    function changeWidth(){
        var e1 = document.getElementById("e1");
        e1.style.width = 400;
    } 
</script>
<body>
    <input type="button" value="change width" onclick="changeWidth()"/>
    <div id="e1" style="width:20px;height:20px; background-color:#096"></div>
</body>
</html>

When user presses the change width button, the <div>'s width should change.

It works fine when doctype declaration determines Quirks Mode. In Standards Mode I'm unable to change the element's size this way

Is it possible to manipulate the size of an element in Standards Mode? How to bypass this disfunctionality?

Javascript Solutions


Solution 1 - Javascript

Try declaring the unit of width:

e1.style.width = "400px"; // width in PIXELS

Solution 2 - Javascript

The style property lets you specify values for CSS properties.

The CSS width property takes a length as its value.

Lengths require units. In quirks mode, browsers tend to assume pixels if provided with an integer instead of a length. Specify units.

e1.style.width = "400px";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrafalryView Question on Stackoverflow
Solution 1 - JavascriptAlexandre PerezView Answer on Stackoverflow
Solution 2 - JavascriptQuentinView Answer on Stackoverflow