最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

css - Javascript element style - Stack Overflow

matteradmin4PV0评论

I'm curious why this one

<div class = "overlay">
    fdsfsd
</div>
.overlay{
    width: 100px;
    height: 200px;
    background-color:red;
}
alert(document.getElementsByClassName("overlay")[0].style.width); 

is not alerting nothing. Of course I can write <div style = "width:100px"> then everthing works fine, but it is not good for me, I need css. Here you can find a jsfiddle demo

So exact question is: why this code is not alerting width of div and how alert it if width is given by css?

I'm curious why this one

<div class = "overlay">
    fdsfsd
</div>
.overlay{
    width: 100px;
    height: 200px;
    background-color:red;
}
alert(document.getElementsByClassName("overlay")[0].style.width); 

is not alerting nothing. Of course I can write <div style = "width:100px"> then everthing works fine, but it is not good for me, I need css. Here you can find a jsfiddle demo

So exact question is: why this code is not alerting width of div and how alert it if width is given by css?

Share edited Feb 22, 2015 at 13:20 kasper Taeymans 7,0265 gold badges33 silver badges51 bronze badges asked Feb 22, 2015 at 13:09 mondayguymondayguy 9913 gold badges13 silver badges35 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 10

As noted elsewhere, the problem is that HTMLElement.style retrieves the values from the style attribute of the element; as you're setting your style with CSS, you need to instead use window.getComputedStyle(element, null).width:

var elem = document.getElementsByClassName("overlay")[0],
  width = window.getComputedStyle(elem, null).width;

console.log(width);
.overlay {
  width: 100px;
  height: 200px;
  background-color: red;
}
<div class="overlay">
  fdsfsd
</div>

References:

  • HTMLElement.style.
  • window.getComputedStyle().

The JavaScript .style only relates to inline styles on the element.

See Documentation.

If you want to get the width of an element you should use offsetWidth.

document.getElementsByClassName("overlay")[0].offsetWidth

http://jsfiddle/9kwap3zy/7/

The style gives access only to information which is put into elem.style. In your example style doesn’t tell anything about the margin defined in CSS. Use getComputedStyle().

var putedStyle = getComputedStyle(document.getElementsByClassName("overlay")[0], null)

alert(putedStyle.width);

jsfiddle demo

The .style is inline style="width:100px;" only...

If it's in CSS (rather than inline) you need getComputedStyle -

https://developer.mozilla/en/docs/Web/API/window.getComputedStyle

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far