Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the baseline of an HTML element?

Tags:

html

browser

css

Every browser's devtools will show you the content, padding, border, and margin boxes of an element. Is there any way to get a browser (any browser) to show me the baseline of an element? It would help considerably in debugging inline box alignment.

like image 648
Peeja Avatar asked Jul 31 '16 03:07

Peeja


People also ask

What is baseline HTML?

The baseline is a term used in European and West Asian typography meaning an imaginary line upon which the characters of a font rest. The descenders of characters like g and p extend below this line. Glyphs with rounded lower and upper extents like C or 3 slightly extend below it.

What is element baseline?

The traditional baseline is the line upon which most letters sit and from which the total height of elements should derive. To make matters worse, the CSS line-height property doesn't have an inherent concept of baseline, and each line of text is placed roughly in the middle of the element's total height.

How do I see the HTML element?

Finding HTML Elements by CSS Selectors If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method. This example returns a list of all <p> elements with class="intro" .


1 Answers

This won't answer your question completely, but I hope it can be at least of some use... or maybe it will even inspire someone to come up with a better one!

The trick is a) to calculate the position of the baseline, and b) to make this info available for all elements.

The method of calculating the baseline is surprisingly non-straightforward: we have to create two elements, one of which has font size 0, and then calculate the difference in top position between them.
To put this calculation into all HTML elements, we use the defineProperty method to add a new property to the prototype of the HTMLElement class.

// the main functionality
Object.defineProperty(Element.prototype, 'baselinePosition', 
{
  get: function() {
    var fs0 = document.createElement('span');
    fs0.appendChild(document.createTextNode('X')); fs0.style.fontSize = '0'; fs0.style.visibility = 'hidden';
    var fs1 = document.createElement('span');
    fs1.appendChild(document.createTextNode('X'));
    this.appendChild(fs1); this.appendChild(fs0);
    var result = fs0.getBoundingClientRect().top - fs1.getBoundingClientRect().top;
    this.removeChild(fs0); this.removeChild(fs1);
    return result;
  },
  enumerable: true
});

// and a test run.
var pos = document.getElementById('test').baselinePosition;
console.log('The baseline is at ' + pos + ' px.');
<p id="test">This is the test.</p>

The caveat here is that you will need a browser that can show all properties of HTML element nodes. So far, the only one I found that does that is SeaMonkey's DOM Inspector.

enter image description here

Other browsers either don't have the ability to show JavaScript node properties, or they show only the built-in ones, not the user-added ones.

Anyway, hope this helps a bit!

like image 195
Mr Lister Avatar answered Sep 18 '22 14:09

Mr Lister