Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the height of the baseline of a certain font?

I'm hoping to be able to find the height of the baseline of a piece of text in a div using javascript.

I can't use a predefined font, because many of my users will be using a larger font setting in their browser.

How can I do this in javascript?

like image 499
EPLKleijntjens Avatar asked Apr 20 '12 13:04

EPLKleijntjens


3 Answers

I made a tiny jQuery plugin for that. The principle is simple:

In a container, insert 2 inline elements with the same content but one styled very small . and the other very big A. Then, since there are vertical-align:baseline by default, the baseline is given as follow:

       ^ +----+ ^
       | | +-+| | top
height | |.|A|| v
       | | +-+| 
       v +----+

=======================
baseline = top / height
=======================

Here is the plugin in coffeescript (JS here):

$ = @jQuery ? require 'jQuery'

detectBaseline = (el = 'body') ->
  $container = $('<div style="visibility:hidden;"/>')
  $smallA    = $('<span style="font-size:0;">A</span>')
  $bigA      = $('<span style="font-size:999px;">A</span>')

  $container
    .append($smallA).append($bigA)
    .appendTo(el);
  setTimeout (-> $container.remove()), 10

  $smallA.position().top / $bigA.height()

$.fn.baseline = ->
  detectBaseline(@get(0))

then, smoke it with:

$('body').baseline()
// or whatever selector:
$('#foo').baseline()

--

Give it a try at: http://bl.ocks.org/3157389

like image 102
abernier Avatar answered Oct 14 '22 18:10

abernier


Following the discovery by Alan Stearns (http://blogs.adobe.com/webplatform/2014/08/13/one-weird-trick-to-baseline-align-text/) that inline-block elements change the baseline alignment of other inline elements, I put together a demo which I found more accurate than https://stackoverflow.com/a/11615439/67190, and actually more simple to derive a value in JavaScript: http://codepen.io/georgecrawford/pen/gbaJWJ. Hope it's useful.

<div>
  <span class="letter">T</span>
  <span class="strut"></span>
<div>
div {
  width: 100px;
  height: 100px;
  border: thin black solid;
}
.letter {
  font-size: 100px;
  line-height: 0px;
  background-color: #9BBCE3;
}
.strut {
  display: inline-block;
  height: 100px;
}
like image 45
George Crawford Avatar answered Oct 14 '22 20:10

George Crawford


Vanilla JS, no font info required tested on Chrome, Firefox and Edge :

function getBaselineHeight(el){
    let bottomY = el.getBoundingClientRect().bottom;//viewport pos of bottom of el
    
    let baselineLocator = document.createElement("img"); // needs to be an inline element without a baseline (so its bottom (not baseline) is used for alignment)
    el.appendChild(baselineLocator);

    baselineLocator.style.verticalAlign = 'baseline' ; // aligns bottom of baselineLocator with baseline of el
    let baseLineY = baselineLocator.getBoundingClientRect().bottom;//viewport pos of baseline
    el.removeChild(baselineLocator);
        
    return (bottomY - baseLineY) ;        
}

function positionLine(){
    let line = document.getElementById("line");
    let lorem = document.getElementById("lorem");
    let baselineHeight = getBaselineHeight(lorem);
    let newLinePos = lorem.getBoundingClientRect().bottom - baselineHeight ;
    line.style.top = newLinePos + "px" ;

}
#lorem{
    background-color: lightblue;
}
#line{
    position:absolute;
    left : 0 ;
    height:1px;
    width:100%
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
        <span id="lorem">Lorem ipsum dolor sit amet, consectetur adipiscing elit..</span>

        <br><br>

        <button class="btn btn-primary" onclick="positionLine();">position line on baseline</button>

        <br><br>

        <svg id="line">
            <line x1="0" y1="0" x2="100%" y2="0" style="stroke:rgb(255,0,0);stroke-width:1"></line>
        </svg> 
like image 27
Bob Avatar answered Oct 14 '22 20:10

Bob