line-height测量及使用

1、line-height定义

line-height表示行高,即两行文字基线间的距离。

以下是图示说明:

行高是2条红线之间的距离,即:1 2 3 4

在实际测量中,基线不好找,可测量顶线到顶线的距离来代替行高。

2、行间距

line-height 与 font-size 的计算值之差(在 CSS 中成为“行间距”)分为两半,分别加到一个文本行内容的顶部和底部。

示例代码:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>line-height行高测量</title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
                font-size: 14px;
            }
            
            .p {
                width: 200px;
                margin: 100px;
                line-height: 100px;
                border: 1px solid green;
            }
        </style>
    </head>

    <body>

        <div class="p">
            中文abc12345
        </div>
    </body>

</html>

效果:

3、line-height取值

 /*浏览器默认*/
                line-height: normal;
                /*设置数字,此数字会与当前的字体尺寸相乘来设置行间距*/
                line-height:100px;
                /*设置固定的行间距*/
                line-height: 1.8;
                /*基于当前字体尺寸的百分比行间距。*/
                line-height: 180%;

说明:line-height可以继承,但是后代元素会继承这个缩放因子而不是计算值。

4、inline box

inline元素所产生的inline box,就是容器中每个行级元素都会产生一个inline box,而多个行级元素排成一行就会有多个inline box,即inline boxes。

<p>
            <span>行级元素1</span><span>行级元素2</span><em>行级元素3</em>行级元素4
        </p>

以上HTML有4个inline box,解释如下:

  • p元素是一个容器,包裹了整个行级元素
  • 不带标签的文字也是一个隐藏的行级元素

所有行级元素(行级元素1、行级元素2、行级元素3和行级元素4)具有四个inline box,而每一行都会有一个line box,其实就是每一行所有inline boxes,inline boxes高度取的是最高的inline box的高度

即:每一行中,文字和图片都是inline box,它们共同组成了一个line box,line box的高度取决于inline box中最高的元素。

5、图片不受line-height影响

本示例图片尺寸为150*150px。

示例代码:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>line-height行高属性</title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
                font-size: 14px;
            }
            
            .p {
                margin: 100px;
                border: 1px solid red;
                line-height: 1.8;
            }
        </style>
    </head>

    <body>

        <div class="p">
            <img src="dist/img/1_jslang.jpg" alt="尺寸为:150*150" /><span>az123</span>
        </div>
    </body>

</html>

效果:

 

说明:上图的图片和文字下有一个间距,因为img的对齐方式默认为为基线对齐

将img的基线对齐改为底部对齐可去除下面的空白

img{
                vertical-align: bottom;
            }

效果:

 

此时line-height应该设置为图片的高度,即150px。

文字和图片垂直居中的示例代码为:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>line-height行高属性</title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
                font-size: 14px;
            }
            
            .p {
                margin: 100px;
                border: 1px solid red;
                /*设置为图片的高度了*/
                line-height: 150px;
            }
            
            img {
                /*图片对齐方式改为底部对齐*/
                vertical-align: bottom;
            }
        </style>
    </head>

    <body>

        <div class="p">
            <img src="dist/img/1_jslang.jpg" alt="尺寸为:150*150" /><span>az123</span>
        </div>
    </body>

</html>

效果:

 6、块级元素的高度和字体大小没有关系,是由行高决定。

示例代码:

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>line-height</title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
            }
            
            .test1 {
                line-height: 14px;
                font-size: 50px;
                background-color: #f00;
            }
            
            .test2 {
                line-height: 50px;
                font-size: 14px;
                background-color: #ccc;
            }
        </style>
    </head>

    <body>
        <br />
        <br />
        <p class="test1">
            我的行高是14px,字体大小是50px;
        </p>
        <br />
        <br />
        <p class="test2">
            我的行高是50px,字体大小是14px;
        </p>
    </body>

</html>

 

效果:

 

7、 行级元素元素的高度由字体大小决定,与行高无关。

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <title>line-height</title>
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
            }
            
            .test1 {
                line-height: 14px;
                font-size: 50px;
                background-color: #f00;
            }
            
            .test2 {
                line-height: 50px;
                font-size: 14px;
                background-color: #ccc;
            }
        </style>
    </head>

    <body>
        <br />
        <br />
        <br />
        <p>
            <span class="test1">我的行高是14px,字体大小是50px;</span>
        </p>
        <br />
        <br />
        <p>
            <span class="test2"> 我的行高是50px,字体大小是14px;</span>
        </p>
    </body>

</html>

 

 

效果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些用Python-OpenCV编写的机械零件尺寸测量的代码示例: 1. 通过轮廓检测测量物体的尺寸 ```python import cv2 # 读取图像 img = cv2.imread('part.jpg') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化图像 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 查找轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 绘制轮廓 cv2.drawContours(img, contours, -1, (0, 255, 0), 2) # 计算物体的尺寸 cnt = contours[0] x, y, w, h = cv2.boundingRect(cnt) print('Width:', w) print('Height:', h) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 2. 使用霍夫变换检测直线并测量长度 ```python import cv2 import numpy as np # 读取图像 img = cv2.imread('part.jpg') # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 边缘检测 edges = cv2.Canny(gray, 50, 150, apertureSize=3) # 进行霍夫变换检测直线 lines = cv2.HoughLines(edges, 1, np.pi/180, 200) # 绘制直线 for line in lines: rho, theta = line[0] a = np.cos(theta) b = np.sin(theta) x0 = a*rho y0 = b*rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) # 计算直线长度 length = np.sqrt((x2-x1)**2 + (y2-y1)**2) print('Length:', length) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 3. 使用模板匹配测量物体的尺寸 ```python import cv2 import numpy as np # 读取图像和模板 img = cv2.imread('part.jpg') template = cv2.imread('template.jpg') # 转换为灰度图像 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) # 获取模板的尺寸 w, h = template_gray.shape[::-1] # 进行模板匹配 res = cv2.matchTemplate(img_gray, template_gray, cv2.TM_CCOEFF_NORMED) # 获取匹配结果的坐标 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 绘制匹配结果的矩形框 top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 2) # 计算物体的尺寸 width = w height = h print('Width:', width) print('Height:', height) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值