Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically setting line-height/keeping text vertically centered when parent div changes size in JavaScript

Tags:

javascript

css

I'm trying to achieve the following in HTML/Javascript:

  • have a coloured circle with a piece of text perfectly centred (both horizontally and vertically) within it;
  • dynamically from JavaScript, be able to alter the size of the circle, maintaining the text centred within it at all times.

The following achieves the first of these:

  • Create the circle using a DIV element whose style has appropriate background and border-radius;
  • Inside the DIV, put a P element whose style has "text-aligbn: center" and "line-height: ".

For example:

p.circlecaption {
  text-align: center;
  line-height: 128px;
}
...
<div style="background: #a0a0a0; margin: 0px; width: 128px;
   height: 128px; border-radius: 64px;" id="theCircleDiv">
     <p class="circlecaption" id="theText">TEST!</p>
</div>

This works fine for the initial, static, case. The problem comes when, from JavaScript, I attempt to set the line-height property in order to keep the text vertically centred as I change the size of the div. I expected something like the following to work:

var obj = document.getElementById('theCircleDiv');
var sz = '' + (rad*2) + 'px';
obj.style.width = sz;
obj.style.height = sz;
obj.style.margin = '' + (64 - rad) + 'px';
obj = document.getElementById('theText');
obj.style['line-height'] = sz;

However, while this code re-sizes and re-centres the circle perfectly, it doesn't vertically re-centre the text-- i.e. the attempt to dynamically set line-height appears to be ignored.

Can anybody offer any help on either how to set line-height dynamically, or else a way to achieve my desired goal of keeping the text centred within the circle? From my reading around, I've seen various other suggestions such as calling the property "lineHeight" or playing around with "vertical-align: middle", but none seems to work.

(I am currently testing in Safari on Mac OS which is likely to be most used among the site's target audience, but am also looking for a solution that is reasonably cross-browser compatible.)

like image 895
Neil Coffey Avatar asked Apr 10 '13 16:04

Neil Coffey


People also ask

How to vertically space text CSS?

Use the line-height property in CSS to do so. Browsers by default will create a certain amount of space between lines to ensure that the text is easily readable. For example, for 12-point type, a browser will place about 1 point of vertical space between lines.

When to use line-height property?

The line-height property defines the amount of space above and below inline elements. That is, elements that are set to display: inline or display: inline-block . This property is most often used to set the leading for lines of text.

What does line-height property do?

The line-height CSS property sets the height of a line box. It's commonly used to set the distance between lines of text. On block-level elements, it specifies the minimum height of line boxes within the element. On non-replaced inline elements, it specifies the height that is used to calculate line box height.

How to vertically center text in a Div using CSS?

The line-height property can be used to vertically center the text in div. Just add the line- height equal to the height of the div element. It will add equal spaces to the top and bottom of the div element which will vertically center the text. Here the value of line- height and height is the same for the div element.

How to set the width of a div element using jQuery?

Set the width of a div element using jQuery The content width of a div can dynamically set or change using width(), innerWidth(), and outerWidth() methods depending upon the user requirement.

Why can’t I set the height of a div element?

For a website where the content is dynamic, we cannot set the height manually because we may not be aware of how much content will be placed within the div elements. We will use a few lines of JavaScript code to resize the div elements so that they share the same height on the page.

How to vertically align text with the padding property in CSS?

When vertically aligning a text with the padding property, we must set the top and bottom padding of the parent element to be equal. When setting the padding, use % to help them grow dynamically. This method requires some calculations to understand what values are needed on the top and bottom, so as they can grow dynamically.


1 Answers

You can achieve that with pure css

#theCircleDiv {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

#theText {
    vertical-align: middle;
}

Working Example: http://jsfiddle.net/bZj52/

like image 125
Rodrigo Siqueira Avatar answered Sep 27 '22 23:09

Rodrigo Siqueira