1

I want to get the line height of an element, It's that simple.

I know this works

console.log(document.getElementById("myDiv").style.lineHeight);

console.log($("#myDiv").css('lineHeight'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv" style="line-height:30px;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>

But my real question is - can we get the line height of an element if the style is applied to the parent element ?

console.log(document.getElementById("myDiv").style.lineHeight);

console.log($("#myDiv").css('lineHeight'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="line-height:30px;">
  <div id="myDiv">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>

Or How to get it If it's default value from the browser .?

console.log(document.getElementById("myDiv").style.lineHeight);

console.log($("#myDiv").css('lineHeight'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myDiv">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>

But in all these cases jQuery returns the value I need, but I need to get the value using javascript.

Any help would be appreciated.

Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69
  • Well, jQuery is JavaScript. You might be after [`getComputedStyle`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle) method ..? – Teemu Mar 14 '18 at 13:12
  • @Justinas please read a Question before flagging it as a duplicate. – Jithin Raj P R Mar 14 '18 at 13:15
  • 2
    @weBBer I read the question and it appears to be a duplicate of that one. [edit] your question to make it clearer why it is not a duplicate. – Heretic Monkey Mar 14 '18 at 13:19
  • @weBBer, in jQuery, `.css()` gets the value of a **computed (means the current, actual) style property** for the first element in the set of matched elements or set one or more CSS properties for every matched element. (source: http://api.jquery.com/css/) - It answers to your "real question" (c.f. your post) … And in vanilla JS, the `getComputedStyle()` method does the same thing. – Takit Isy Mar 14 '18 at 13:23
  • @MikeMcCaughan answer may work for me but both are different Question. In there he wants the CSS style applied in the stylesheet but in my case, I want the default behaviour of the element or the style inherited by the element from the parent.! – Jithin Raj P R Mar 14 '18 at 13:26

1 Answers1

1

You can get the style using the getComputedStyle method... this is actually used in jquery...

//console.log(document.getElementById("myDiv").style.lineHeight);

//console.log($("#myDiv").css('lineHeight'));


var elem = document.getElementById("myDiv");
var view = elem.ownerDocument.defaultView;

console.log(view.getComputedStyle( elem )['line-height']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myDiv">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37