ZL
About Articles Contact
Published on Jul 29, 2020
Filed under:
#javascript

Getting the horizontal and vertical centers of an element

I often find myself needing to calculate the horizontal center and vertical center of an element.

One example is a popover.

. Your browser doesn't support the HTML video element. Click here to watch the video.

To position the popover perfectly, I need to know the horizontal and vertical centers of the button that triggers the popover. Here’s one example of a calculation I had to make.

One of the popover calculations.

Another example: When I built this spinning pacman, I need to get the center of the pacman to calculate the angle of rotation.

. Your browser doesn't support the HTML video element. Click here to watch the video.
Need the center to calculate the angel.
Note

I taught people how to build these two things step-by-step in Learn JavaScript. You may find it helpful if you want to learn to build things from scratch.

Getting the horizontal and vertical centers

It’s easy to get both the horizontal and vertical centers.

First, we use getBoundingClientRect to get information about the bounding box.

  • To get the horizontal center (which I call xCenter), we use the average of the left and right values from this bounding box.
  • To get the vertical center (which I call yCenter), we use the average of the top and bottom values of the bounding box.
const box = element.getBoundingClientRect()
const xCenter = (box.left + box.right) / 2
const yCenter = (box.top + box.bottom) / 2

Function to simplify everything

I made a function to streamline this calculation. I call it getBoundingBox. It returns all values getBoundingClientRect plus xCenter and yCenter.

The function look like this:

function getBoundingBox(element) {
const box = element.getBoundingClientRect()
const ret = {}
// Loops through all DomRect properties.
// Cannot spread because they're not enumerable.
for (const prop in box) {
ret[prop] = box[prop]
}
ret.xCenter = (box.left + box.right) / 2
ret.yCenter = (box.top + box.bottom) / 2
return ret
}

Using it:

const box = getBoundingBox(element)
const { xCenter, yCenter } = box
Previous What's the difference between an Interface and an API? Next How I set up my Moonlander

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is one of those rare people who commands tremendous knowledge and experience but remains humble and helpful. They want you to know what they know, not just be impressed by it.

In other words, Zell is a natural teacher. You’re lucky to have him because he feels lucky to be able to help you in your journey.

Heydon Pickering
Heydon Pickering — Web & Accessibility Extraordinaire
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2025 Zell Liew / All rights reserved / Terms