Home > Web Front-end > CSS Tutorial > Why Doesn't `vertical-align` Center Inline/Inline-Block Elements Within a Div?

Why Doesn't `vertical-align` Center Inline/Inline-Block Elements Within a Div?

Susan Sarandon
Release: 2024-12-23 14:17:17
Original
787 people have browsed it

Why Doesn't `vertical-align` Center Inline/Inline-Block Elements Within a Div?

Aligning Inline/Inline-Block Elements Vertically

In an attempt to align multiple inline and inline-block elements vertically within a div, a user encounters an issue where a span element remains positioned lower than expected within the container. Despite applying vertical-align:middle; and vertical-align:top; to the span, the alignment remains unchanged.

The CSS property vertical-align controls the vertical positioning of elements against the text baseline. However, this property applies to the elements being aligned, not their parent. To vertically align the children within the div, the user should instead target the child elements directly:

div > * {
    vertical-align:middle;  // Align children to middle of line
}
Copy after login

This revised code will ensure that all child elements within the div are vertically centered. Note that vertical-align aligns elements relative to the current text line, not the entire height of the parent div. If the intent is to create a taller div with centered elements, the line-height property can be used instead of height. For a working example, refer to the provided jsfiddle link:

[jsfiddle link]

The above is the detailed content of Why Doesn't `vertical-align` Center Inline/Inline-Block Elements Within a Div?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template