In front-end design, what do the baseline of child elements and the baseline of parent elements mean respectively? And use Bootstrap's align-items-baseline class to achieve baseline alignment of child elements within their parent container.

The baseline of child elements and the baseline of parent elements are important concepts used for text alignment. Let me explain what each of them refers to:

  1. Baseline of Child Elements :

    The baseline of a child element is the bottom edge of the text within the child element, specifically the bottom edge of letters. Within an element containing text, each letter has a baseline that determines the vertical alignment of the text. For example, the baseline of the letter "x" is the line at the bottom of the letter "x".

  2. Baseline of Parent Element :

    The baseline of a parent element is the lowest point among the baselines of all child elements within the parent element. In a container that contains multiple child elements (such as lines of text or inline elements), their baselines form a baseline within the parent element. This parent element's baseline is often used to vertically align multiple lines of text or inline elements to a common baseline to achieve consistent text arrangement.

In web development, especially when dealing with text content and vertical alignment, it's important to understand the baseline of child and parent elements. Understanding baselines helps ensure that text and elements are aligned in a consistent manner on the page for better visuals.

In Bootstrap, you can use the align-items-baseline class to achieve baseline alignment of child elements within their parent container.

Here is a sample code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>基线对齐示例</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="bootstrap-4.5.3-dist/css/bootstrap.css">
    <script src="jquery-3.5.1.slim.js"></script>
    <script src="bootstrap-4.5.3-dist/js/bootstrap.min.js"></script>
</head>
<style>
    .box{
      
      
        width: 100%;   /*设置宽度*/
        height: 70px;   /*设置高度*/
    }
</style>
<body class="container">
<h3 align="center">基线对齐示例</h3>

<h4>垂直方向上对齐到父容器的顶部</h4>
<div class="d-flex align-items-start bg-warning">
  <div class="p-2 bg-primary">元素 1</div>
  <div class="p-2 bg-success" style="font-size: x-large;">元素 2</div>
</div>

<h4>子元素基线与父元素的基线对齐</h4>
<div class="d-flex align-items-baseline bg-warning">
  <div class="p-2 bg-primary">元素 1</div>
  <div class="p-2 bg-success" style="font-size: x-large;">元素 2</div>
</div>

</body>
</html>

The running effect is shown in the figure below:
Insert image description here
From the above running effect, we can clearly see that if the baseline alignment is easily set, then the bottom of the text of each element will be aligned.
Insert image description here

Guess you like

Origin blog.csdn.net/wenhao_ir/article/details/133343442