Home Web Front-end CSS Tutorial How to Vertically Center Text and Icons in CSS

How to Vertically Center Text and Icons in CSS

Feb 20, 2025 am 10:50 AM

How to Vertically Center Text and Icons in CSS

This article will introduce a variety of CSS horizontal and vertical centering methods that were once tricky but are now easy to implement. We will cover horizontal and vertical centering using Flexbox and positioning plus transformations. In another article, we will also cover how to use CSS Grid for horizontal and vertical centering.

Summary of key points

  • Use Flexbox vertically centered text and icons: Convert containers to Flex containers and use justify-content and align-items to centered child elements.
  • Combination of position and transform can achieve vertical centering, especially for highly variable elements. This is done by creating a positioning context and adjusting the position of the element relative to its container.
  • The
  • line-height attribute can be used to vertically center a single line of text or icons within a fixed height container, while the vertical-align can be used to center inline elements.

How to use Flexbox for horizontal and vertical centering

Flexbox can align elements horizontally and vertically, thereby centering text, icons, or any other element in CSS.

To use the Flexbox centering element, you can use the following code snippet:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}
Copy after login
Copy after login

This code snippet converts .container to a Flex container, automatically converting its child elements to a Flex project. You can then use justify-content to center these Flex items horizontally and use align-items to center vertically.

View example: CodePen link

Continue reading to learn about the vertical positioning and positioning techniques of the box model.

How to use position and transform to achieve flexible vertical centering

When you cannot use Flexbox or you need to center an item in the container, you can use position to place the element in the center of its container.

Instead, we can combine position and transform to vertically center highly variable elements. For example, to center an element vertically across the entire height of the viewport, you can use the following code snippet:

.container {
  position: relative;
  min-height: 100vh;
}
.vertical-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
Copy after login

We first create the positioning context by setting position: relative on the container element. This will allow us to locate the .vertical-center element within its boundaries.

Next, we place the upper left corner of the element to be centered at the center of its container by placing its top edge at 50% of the top of its parent element and its left edge at 50% of the left of its parent element place.

Finally, adjust the element back to the center by panning it upwards 50% of its height and 50% of its width to the left. Now our elements are centered vertically and horizontally within the container. Since placement is done using a percentage value relative to the size of the element, the element will remain centered if the width or height of the container or child element changes.

View example: CodePen link

How to use line-height to center vertically at fixed height

To center a single line of text or icon vertically within its container, use the line-height attribute. This property controls the height of a line in the text run and adds an equal amount of space above and below the wireframe of the inline element. If the height of the container is known, setting the line-height of the same value will neuter the child element vertically.

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}
Copy after login
Copy after login

How to use vertical-align to center inline elements

The

vertical-align attribute only affects the elements that are displayed as inline, inline-block or table-cell.

It takes length, percentage, or keyword value.

Length and percentage align the baseline of an element with a given distance above its parent element baseline.

Keyword value can be one of the following:

  • baseline
  • sub
  • super
  • text-top
  • text-bottom
  • middle
  • top
  • bottom

Most of these are very intuitive, but sub align the baseline with the subscript baseline of the parent element, while super align the baseline of the element with the superscript baseline of the parent element.

Let's take a look at vertical-align in a practical example.

Here is an image and text grid, both of which have different heights, meaning that the text is not all neatly aligned.

(The HTML code in the original text should be inserted here and modified accordingly to make it more concise and easy to understand, and avoid using invalid links)

We can set the grid container to display: inline-block and use vertical-align: bottom on the image to make everything line up neatly.

If there is no text here, and we want all images to be centered vertically, we can use vertical-align: middle and get pretty good results.

For more centering methods, be sure to check out how to use CSS Grid for horizontal and vertical centering.

FAQ on how to center text and icons vertically in CSS

(The original FAQ part has been streamlined here, retaining the core information, and expressing some answers more clearly.)

What is vertical alignment?

Vertical alignment in CSS is an attribute that controls the position of elements along the vertical axis. It is a powerful tool that helps you design your layout accurately and flexibly. It can be used to vertically center elements, align them with the top or bottom of the container, or evenly distribute them in the vertical space. The vertical-align attribute in CSS is usually used with inline or inline block elements, but can also be used with table cells.

How to use CSS to vertically center text?

can use display: flex and align-items: center to center text vertically.

Can you use vertical alignment to process block-level elements?

The block-level elements can be aligned vertically using position: relative; top: 50%; transform: translateY(-50%);.

Why does my vertical alignment not work?

Causes may include the parent container not specifying a height, or attempting to align block-level elements using vertical-align.

How to align images vertically?

The same method as text can be used.

Can vertical alignment be used to handle CSS Grid?

Attributes can be used. align-items

How to vertically align text in buttons?

can be used with the

attribute, whose value should be the same as the button height. line-height

How to align multiple elements vertically?

Flex or Grid methods can be used.

Can absolute positioning be handled using vertical alignment?

and

properties can be used. top transform

How to vertically align text in table cells?

Can be used

.

The above is the detailed content of How to Vertically Center Text and Icons in CSS. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

While You Weren't Looking, CSS Gradients Got Better While You Weren't Looking, CSS Gradients Got Better Apr 11, 2025 am 09:16 AM

One thing that caught my eye on the list of features for Lea Verou's conic-gradient() polyfill was the last item:

A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

See all articles