1

I am trying to set the width and height of a div in javascript but it is not being set. I have tried a lot but nothing is working. Here is what i have tried,

<div id="d1">asdasd</div>

<script>
  var d = document.getElementById("d1");
  d.style.width = "100px";
  d.style.height = "100px";
  d.style.cssText = 'border:1px solid black;'
</script>

Please check what is the problem!

3
  • CAnt you fix directly in css? Commented Nov 5, 2012 at 6:53
  • 1
    No I have to do this in javascript. As i have to use values from js variables! Commented Nov 5, 2012 at 6:54
  • Have a look at this demo - it does what you need. Commented Nov 5, 2012 at 6:58

9 Answers 9

6

I think d.style.cssText overwrites width and height properties

Try this:

var d = document.getElementById("d1");
d.style.width = "100px";
d.style.height = "100px";
d.style.cssText += 'border:1px solid black;'
Sign up to request clarification or add additional context in comments.

1 Comment

Even if i don't write that line it doesn't set the height and width!
3

As karaxuna mentioned, your d.style.cssText is overwriting the width and height settings. Simply changing your code to either this:

var d = document.getElementById("d1");
d.style.width = "100px";
d.style.height = "100px";
d.style.border = '1px solid black';

or this:

var d = document.getElementById("d1");
d.style.cssText = 'border:1px solid black;'
d.style.width = "100px";
d.style.height = "100px";

works perfectly.

Comments

2

Your problem is with d.style.cssText = 'border:1px solid black;' . it is overriding the width and height set by JS .

Check this fiddle:-

http://jsfiddle.net/RZKnQ/1/

Comments

1

This works:

<div id="d1" style="border:1px solid black;">asdasd</div>​
<script>
var d = document.getElementById("d1");
d.style.height = "200px";
</script>

http://jsfiddle.net/dandv/emnMb/4/

Comments

1

An alternate would be to define a new style element in CSS and simply set d.className to that CSS element.

I guess

d.style.cssText 

is problem

Comments

0

Change sequence of your JS statements it will work i.e.

<script>
    var d = document.getElementById("d1");
    d.style.cssText = 'border:1px solid black;'
    d.style.width = "100px";
    d.style.height = "100px";
</script>

Better put width, height properties in style attribute only. i.e.

d.style.cssText = 'border:1px solid black; width:100px; height:100px'

or in CSS class

Comments

0
d.style.cssText = 'border:1px solid black;'
d.style.float="left";
d.style.width = "100px";
d.style.height = "100px";​

You neeed to use float:left attribute.

Comments

0

I don't know what your problem is but I've used this and this worked

    function fnc1()
{
    document.getElementById("d1").style.width="100px";
    document.getElementById("d1").style.height="100px";
    document.getElementById("d1").style.backgroundColor="red";
}

Comments

0

HTML:

<div id="d1">asdasd</div>​

Javascript

document.getElementById('d1').style.height = 90+'px';​

working demo here ( works fine with chrome, din't check with other browsers )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.