Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Vertically Align Content with CSS

There are ways to vertically align content within a div so it publishes in the middle.

Note: This article discussed vertical centering with the CSS display and vertical-align properties. Another article, Vertical Content Centering With CSS, discusses vertical centering with the position property.

A little bit of history:

In the old days, a decade and a half ago, before I learned CSS, I used to vertically align content with a table td tag, using attribute valign="middle"

It still works, of course, like virtually all early HTML does. Although using CSS where possible is generally considered more acceptable by those who develop websites.

When CSS started to become accepted by the site-development community, way back when, I resisted it. "Why change something that already works?" was my argument. But the more I learned about it, the more I liked it. Now, I absolutely enjoy working with CSS.

Two years ago, we published Vertical Content Centering With CSS at Willmaster.com with techniques for vertical alignment of content. It uses the CSS position:absolute; declaration and utilizes the top and margin-top properties for aligning the content.

It has two techniques. One requires you know the height of the content at the time the HTML is coded. The other uses JavaScript to do the vertical alignment at the time the page is loaded.

This technique:

In this article, you'll find out how to use the CSS display:table-cell; and vertical-align:middle; declarations to vertically align content — even when the height of the content isn't known at the time the HTML is coded.

Here's an example:

An
example.

And here's the source code of the example. Notes follow.

<div 
   style="
      display:table-cell; 
      height:100px; 
      width:100px; 
      text-align:center; 
      vertical-align:middle; 
      background-color:beige;
   ">
An<br>example.
</div>

Notes:

The two required CSS declarations are printed in blue. The rest are used to format presentation of the example.

The display:table-cell; declaration makes the div behave as though it was a table td container. The vertical-align:middle; declaration vertically aligns the content within the div. (Here's information about the display property.)

Unlike the text-align:center; declaration, which works in divs with any display property, the vertical-align:middle; declaration only works reliably in a div also containing the display:table-cell; declaration. (Here's information about the vertical-align property.)

Using this technique:

This technique can be useful and quick to implement when the display:table-cell; declaration can be used. If other display property values need to be used, like block, then a technique mentioned in Vertical Content Centering With CSS might be used.

There are a number of ways to vertically align content. Probably there's a way for any situation you encounter.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC