Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get height of "br" element using jquery?

I try to get the height of <br /> on my webpage. I use this snippet.

br_size = $('br').height();
console.log(br_size);

But only the Mozilla Firefox like this code, all other browsers give back 0. Is there a short JS code that gives me the correct height back?

PS: i use this method, because i know that the needed size X is X= other_size - 4*br_size

like image 591
LFS96 Avatar asked Sep 14 '16 07:09

LFS96


People also ask

How to get the height of an element in jQuery?

innerHeight() - Returns the height of an element (includes padding) outerWidth() - Returns the width of an element (includes padding and border) outerHeight() - Returns the height of an element (includes padding and border)

What is the height of BR tag?

You can't change the height of <br> tag as its not an HTML element, it is just an instruction which enforces a line break. br does not take up any space in the page. There is a way by which you can increase line break between lines, is by putting multiple br tags.

How to get height of a div using jQuery?

Method 1: The height() method returns the first matched element's height, But the height(value) method sets all matched elements height. // Returns the height of the first matched element $(selector). height() // Set the height of the all matched elements $(selector). height(value);

How to get height and width of an element in jQuery?

jQuery outerWidth() and outerHeight() Methods The outerWidth() method returns the width of an element (includes padding and border). The outerHeight() method returns the height of an element (includes padding and border).


1 Answers

The line-break depends on the line-height specified in the HTML/BODY tags, if one isn't specified then the browser will likely use their own. However, if you specify a line-height, say 20px then the line-break should be 20px. You could probably use JavaScript to determine the default line-height

(function() {
  var br = document.getElementById('foo');
  alert(br.scrollHeight);
  if (br.currentStyle) {
    alert(br.currentStyle['lineHeight']);
  } else {
    alert(document.defaultView.getComputedStyle(br, null).getPropertyValue('line-height'));
  }
})();
<br id="foo" />
like image 114
Tushar Avatar answered Sep 19 '22 20:09

Tushar