跳到主要内容

line-height

2023年02月28日
柏拉文
越努力,越幸运

一、认识


line-height 属性用于设置多行元素的空间量,如多行文本的间距。对于块级元素,它指定元素行盒(line boxes)的最小高度。对于非替代的 inline 元素,它用于计算行盒(line box)的高度。

二、语法


/* Keyword value */
line-height: normal;

/* Unitless values: use this number multiplied
by the element's font size */
line-height: 3.5;

/* <length> values */
line-height: 3em;

/* <percentage> values */
line-height: 34%;

/* Global values */
line-height: inherit;
line-height: initial;
line-height: unset;

可选值:

  • normal: 取决于用户端。桌面浏览器(包括 Firefox)使用默认值,约为1.14,这取决于元素的 font-family

  • <数字>: 该属性的应用值是这个无单位数字<数字>乘以该元素的字体大小。计算值与指定值相同。大多数情况下,这是设置line-height的推荐方法,不会在继承时产生不确定的结果。

  • <长度>: 指定<长度>用于计算 line box 的高度。

  • <百分比>: 与元素自身的字体大小有关。计算值是给定的百分比值乘以元素计算出的字体大小。百分比值可能会带来不确定的结果。

三、计算


3.1 lineHeight radix

function getCssValue(value) {
value = String(value);
if (value.endsWith('%')) {
return parseFloat(value) / 100.0;
}
return parseFloat(value);
}

function getLineHeightRadix(value, fontSize) {
if (value === 'normal') {
return 1.14;
} else if (value.endsWith('%')) {
return getCssValue(value);
} else if (
value.endsWith('px') ||
value.endsWith(/(vw|vh)/) ||
value.endsWith('em') ||
value.endsWith('rem')
) {
return (getCssValue(value) / getCssValue(fontSize)).toFixed(2);
}
return value;
}

const containerDOM = document.querySelector('.container');
const style = getComputedStyle(containerDOM);
const radix = getLineHeightRadix(style.lineHeight,style.fontSize);
console.log("radix",radix)

3.2 lineHeight value

function getCssValue(value) {
value = String(value);
if (value.endsWith('%')) {
return parseFloat(value) / 100.0;
}
return parseFloat(value);
}

function getLineHeightRadix(value, fontSize) {
if (value === 'normal') {
return 1.14;
} else if (value.endsWith('%')) {
return getCssValue(value);
} else if (
value.endsWith('px') ||
value.endsWith(/(vw|vh)/) ||
value.endsWith('em') ||
value.endsWith('rem')
) {
return (getCssValue(value) / getCssValue(fontSize)).toFixed(2);
}
return value;
}

const containerDOM = document.querySelector('.container');
const style = getComputedStyle(containerDOM);
const radix = getLineHeightRadix(style.lineHeight,style.fontSize);
const value = radix * getCssValue(style.fontSize);
console.log("value",value)

3.3 lineHeight lines

function getCssValue(value) {
value = String(value);
if (value.endsWith('%')) {
return parseFloat(value) / 100.0;
}
return parseFloat(value);
}

function getLineHeightRadix(value, fontSize) {
if (value === 'normal') {
return 1.14;
} else if (value.endsWith('%')) {
return getCssValue(value);
} else if (
value.endsWith('px') ||
value.endsWith(/(vw|vh)/) ||
value.endsWith('em') ||
value.endsWith('rem')
) {
return (getCssValue(value) / getCssValue(fontSize)).toFixed(2);
}
return value;
}

const containerDOM = document.querySelector('.container');
const style = getComputedStyle(containerDOM);
const radix = getLineHeightRadix(style.lineHeight,style.fontSize);
const lineHeight = radix * getCssValue(style.fontSize);
const row = Math.ceil(containerDOM.clientHeight / lineHeight);
console.log("row",row);