Home > Web Front-end > CSS Tutorial > Why Are My Inline-Block Elements Vertically Misaligned?

Why Are My Inline-Block Elements Vertically Misaligned?

DDD
Release: 2024-12-18 09:45:11
Original
879 people have browsed it

Why Are My Inline-Block Elements Vertically Misaligned?

Unveiling the Mystery of Vertical Misalignment in Inline-Block Elements

Encountering a puzzling CSS issue where an inline-block element's content appears misaligned vertically? The culprit may be the default vertical alignment value.

Inline-block elements have a default vertical alignment of baseline, which aligns the baseline of the box with that of its parent element. This can lead to unexpected alignment when the element contains no in-flow line boxes or has an overflow value other than visible.

Consider the following HTML and CSS:

<div>
Copy after login
#divBottomHeader {
  height: 43px;
}
.divAccountPicker {
  width: 200px;
  height: 40px;
}
.divAccountData {
  width: 400px;
  height: 40px;
}
Copy after login

In this example, the ".divAccountData" element appears shifted downwards compared to ".divAccountPicker." Adding text to ".divPutTextToFixIssue" magically aligns the elements vertically.

This behavior arises from the fact that the baseline of an inline-block element is determined by the baseline of its last line box. By adding text to ".divPutTextToFixIssue," we effectively changed the baseline and forced the alignment to be top.

However, if both blocks contain a different number of lines, the alignment will break again. To ensure consistent vertical alignment, force the alignment using the vertical-align property:

.divAccountData {
  vertical-align: top;
}
Copy after login

This will align the baseline of ".divAccountData" with the top of ".divAccountPicker," regardless of their line count.

The above is the detailed content of Why Are My Inline-Block Elements Vertically Misaligned?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template