Home >Web Front-end >HTML Tutorial >Detailed explanation of line and vertical-align examples in css
The formatting sequence of inline elements: font-size-->em box-->content area-->inline box-->place the inline box according to the baseline-->determine the height of the line box
Related concepts:
leading (line spacing): refers to the lead bar filled between two lines of text, equal to line-height and font The difference between -size, half of the leading is added above the text, and the other half of the leading is added below the text, only used for non-replaced elements.
emBox: The em box indicates the distance between baselines when there is no line spacing. The boundaries between characters are not specified. The actual glyph may be longer than its em box. Tall or shorter.
font-size :Determines the height of the em box (em box) of a given font, but there is no guarantee that the actual displayed characters will be this size. The specific correspondence between the font-size attribute and the actual font size is determined by the font designer.
Baseline: The line upon which most letters "sit" and below which descenders extend is not the lower edge of the Chinese characters, but the lower edge of the English letter "x".
Content area (content area): Non-replacement elements are boxes composed of em boxes strung together, indirectly determined by font-size ;Replacement element: that is, the inherent height of the element plus possible margins and boxes.
Inline box (inline box): Virtual rectangular box, cannot be displayed, the size is the content area plus leading. For non-replaced elements, it is equal to the line-height value; for replaced elements, it is equal to the height of the content area. Several elements within the same line can have different line heights and inline box heights.
Line box (line box): The smallest box that contains the highest and lowest points of all inline boxes in the line, the line box The height is only related to the line height of the element in this line, and has no direct relationship with line-height, and has nothing to do with the height (height) of the parent element . Inline boxes are aligned vertically within the line based on the value of their vertical-align property.
Anonymous text: refers to all strings that are not contained in inline elements.
line-height: refers to the distance between the baselines of text lines, which determines how much each element box increases or decreases. The default is 1.2 times the font size of the element itself.
When the line-height value is inherited from the parent element, it inherits the value calculated on the parent element, not the value calculated on the child element (em, etc.). If it is a scaling factor (a pure numeric value without units), the scaling factor is inherited, not the calculated value. The product of the scaling factor and the font-size of the child element is eventually calculated.
Figure 1:
Figure 2: Line in css
Figure 3: OK Line box height
Replacement element
The margins and borders of the replacement element will affect the inline box of the element, which in turn affects the height of the line box .
Elements whose content is not controlled by the CSS visual formatting model, such as , ,
Non-replaced elements
All other elements except replaced elements are non-replaced elements. The actual content is in the document flow, and the visual formatting model of CSS is responsible for the non-replaced elements. render.
The margins and borders of non-replaced elements will not affect the height of the inline box of line elements.
vertical-align: Vertical alignment, only applies to inline elements, replacement elements and table cells, and cannot be inherited.
The default value is baseline, which aligns the baseline of the in-line element with the baseline of the line; the text is aligned according to the baseline. If a vertically aligned element has no baseline, such as an image, form input element, or other replacement element, align the bottom edge of the element to the baseline of its parent element.
There is space below that line for the descenders you find on
letters like f, j, p and q. Repair method: set the vertical-align attribute value of the image or change display:block; or modify the font-size/line-height of the parent element to make the height of the inline box smaller than the image. In the extreme case of the parent element's font-size:0;, the centerline and baseline will coincide.
Bottom: Align the bottom of the element’s inline box with the bottom of the line box.
middle: Align the center line of the inline element box with a point 0.25em above the baseline, which is also equal to the distance from the baseline being half the height of the lowercase letter x (i.e. 0.5ex). The midpoint of the x character is not the absolute center line of the content area, because the x character will sink somewhat.
Percentage: Calculated relative to the line-height of the element. The function of line-height of inline replaceable elements is to help calculate vertical-align.
Specific length value: Raise or lower an element by a specified distance relative to the baseline of the parent element. Text aligned vertically does not cover the text of other lines; it only affects the line height of the current line so that it is large enough to contain the top of the highest inline box and the bottom of the lowest inline box.
Inline block: inline-block, the inline block element will be placed in the line as a replacement element, that is, the bottom of the inline block is placed on the baseline of the text line by default. For ie6/ie7, {display:inline; zoom:1;...} must be used to take effect. Usually used for horizontal menu lists or unequal height list elements to be arranged neatly. However, the space between the li tags will be treated as an inline element and a 4px blank will be displayed on the page. You can eliminate the blank space by passing the parent element font-size:0 and the child element font-size:12px; For ie6/ie7/safari, word-spacing and letter-spacing need to be used to adjust.
#Inline blocks are aligned to the baseline by default, but there are special regulations for determining the baseline. The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge. So the layout shown below will appear, which can be fixed by changing vertical-align:top;.
##
The above is the detailed content of Detailed explanation of line and vertical-align examples in css. For more information, please follow other related articles on the PHP Chinese website!