Set line-height as a percentage relative to the parent element

line-height calculator
line-height css
vertical line-height css
line-height crop
unset line-height
line height multiple
line-height position absolute
line-height baseline

I have a responsive element where it's width and height will both scale. Inside this I have some text which I want to center vertically.

How can I set the text's line-height to be the same as it's parent if I don't know the parent's height?

line-height: 100% is relative to the font's regular height so this doesn't help...

Here's another way to center an element vertically. I came across this technique some time ago. Basically it uses a pseudo element and vertical-align: middle.

.block::before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */

/* The element to be centered, can
   also be of any width and height */ 
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 300px;

line-height, How can I set the text's line-height to be the same as it's parent if I don't know the parent's height? line-height: 100% is relative to the font's regular height so this� If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to auto. A percentage height on the root element is relative to the initial containing block. Computed value: a percentage or auto or the absolute length: Animation type

Since it's 2019 already, you could also use flexbox to achieve this :)

To do so, add the following classes to the parent element:

display: flex;
flex-direction: column;
justify-content: center;

See this Fiddle

Set line-height as a percentage relative to the parent element, It is often set in a unitless value (e.g. line-height: 1.4;) so that it is. to use the background of an element as the background of text. A number that will be multiplied with the current font-size to set the line height: Play it » length: A fixed line height in px, pt, cm, etc. Play it » % A line height in percent of the current font size: Play it » initial: Sets this property to its default value. Read about initial: Play it » inherit: Inherits this property from its parent

I'd try putting the text inside another element, of which you know (or set) the size. Then setting relative positioning to it, top, left 50% and negative left and right margins.

See this Fiddle

The only problem is that this relies on a known/fixed textblock. If the text is variable, I'm afraid you will have to resort to using Javascript..

Fun with line-height!, The line-height property defines the amount of space above and below That is, elements that are set to display: inline or display: A percentage value is the font size of the element multiplied by the percentage. an arbitrary value from a parent that is more likely to need overriding. Related Properties. To make the child element positioned absolutely from its parent element you need to set relative position on the parent element AND absolute position on the child element. Then on the child element ‘top’ is relative to the height of the parent. So you also need to ‘translate’ upward the child 50% of its own height.

Regarding hyperlinks:

I was having this problem regarding links in main menu. And since it was <a> in <li> tags I needed some surface for the links to be clickable/touchable(see touch target size). So what I did was for the <ul> I set a fixed height(through it's parent in this case), the <li>-s are a percentage of it and the <a>-s have a min-height and line-height properties set to them and it's easy from there to set the top. The code:

position: fixed;
top: 0;
bottom: 160px;
.menu-header-main-container {
          height: 100%; }
          .menu-header-main-container li {
            height: 33.33%;
            max-height: 110px; }
            .menu-header-main-container li a {
              line-height: 40px;
              min-height: 40px;
              top: calc(50% - 20px);
              position: relative; } }

line-height, The `line-height` CSS property defines the space between two inline 'normal' is the default value if you don't set it to something different. rem is relative to the font-size of the root element and em is relative to body { font-size: 12px; } . percentage { font-size: 16px; line-height: 150%; /* line-height will be� Hi, I have a parent div with height: auto. In this div, there are two other divs. One with an icon, one with some text in it. These divs both have height: auto as well so the text mainly decides

Line-height CSS: How CSS line-height works and best practices, Percentage values: refer to parent element's width This is the height necessary before any relative offset of children. However, the height If the property is set on an inline element, it specifies the exact line height for the element's inline box. For example, if the parent element has a font size of small, a child element with a defined relative size of larger will make the font size equal to medium for the child element. Percentage values.element { font-size: 110%; } Percentage values, such as setting a font-size of 110%, are also relative to the parent element’s font size as shown

11.3 Line height calculations, Definition of Line height and how to set line height on web pages. In CSS, the line-height property can take numeric or percentage values. Because em is a relative measurement unit, it is based on the value of the parent element. 1.5em is� Relative-size keywords. The font will be larger or smaller relative to the parent element's font size, roughly by the ratio used to separate the absolute-size keywords above. <length> A positive <length> value. For most font-relative units (such as em and ex), the font size is relative to the parent element's font size.

Web Typography, Set line-height as a percentage relative to the parent element, The only least inline-block. vertical-align defines the vertical position of self related to parent, as � Because em is a relative measurement unit, it is based on the value of the parent element. 1.5em is the most common line-height; it states that this paragraph has a line-height of 150% of the current font-size (if the font-size is 12px, then the line-height is 1.5 × 12 = 18px).

  • isn't the em unit type used for these cases?
  • I doubt you can achieve this without JS
  • Alas, em will only scale relatively to the font-size, not the parent element dimensions
  • See this explanation of the problem:
  • Providing your actual code will surely help us to provide a specific-case answer
  • Since it's 2019 and people are still using IE, this will not work in all conditions in IE unfortunately.
  • Don't listen to the naysayers. Anyone using IE should expect the internet to look like crap in 2019.
  • Since this is web development, you should provide some kind of graceful degradation for your uses that don't have flexbox. Totally agreed that it can look like crap in a visual-flourish sense, but it should still remain functional, usable and meaningful.