Question
How can an image be vertically aligned inside a containing div?
For example, the goal is to vertically center the <img> inside a <div> with the class frame:
<div class="frame" style="height: 25px;">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" />
</div>
The .frame element has a fixed height, but the image height is unknown. Additional elements may be added inside .frame if necessary. The solution needs to work in Internet Explorer 7 and later, as well as WebKit and Gecko browsers.
Example CSS and HTML:
.frame {
height: 25px; /* Equals maximum image height */
line-height: 25px;
width: 160px;
border: 1px solid red;
text-align: center;
margin: 1em 0;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="250" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="25" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="23" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="21" />
</div>
<div class="frame">
< = = = />
Short Answer
By the end of this page, you will understand how vertical alignment works for images in CSS, why vertical-align: middle is often misunderstood, and which techniques work best for centering an image inside a fixed-height container. You will also learn which solutions are suitable for older browsers like IE7 and which modern approaches are preferred today.
Concept
Vertical centering in CSS can be confusing because it depends on how the element is displayed.
An <img> is an inline-level replaced element, so properties like vertical-align affect how it aligns relative to surrounding inline content, not how it centers itself inside a block container by default.
That means this often surprises beginners:
img {
vertical-align: middle;
}
This does not automatically mean “center the image vertically inside its parent div.” It only affects the image's alignment within a line box.
To vertically center an image inside a fixed-height container, you usually need one of these approaches:
- use the container's
line-heightwhen the layout is simple - use a helper element with
vertical-align: middle - use
display: table-cell - use modern layout systems like Flexbox
In the original problem, browser support includes IE7, so modern Flexbox is not an option. A classic inline-alignment trick is more appropriate.
Why this matters in real programming:
- image thumbnails often need to sit centered in cards or boxes
- logos need alignment inside navigation bars
- icons need to line up with text or controls
- reusable UI components often contain unknown image sizes
Mental Model
Think of a line of inline content like people standing on the same floor.
- The
divis the room. - The line box is the floor line inside the room.
- The image is one person standing on that floor.
vertical-align: middlechanges how that person lines up relative to the others on the same line.
The key idea is: the image is not centering itself against the whole room unless the room is being treated like a line of inline content.
So if you want true vertical centering, you often need to make the container behave in a way that gives the image something meaningful to align against.
A practical way to remember it:
text-alignhandles horizontal alignment for inline contentvertical-alignhandles vertical alignment for inline or table-cell content- block elements usually need a different layout technique
Syntax and Examples
Classic inline alignment technique
A common older-browser-friendly technique is to use a helper element so the image can align vertically against it.
<div class="frame">
<span class="helper"></span>
<img src="logo.png" alt="Logo">
</div>
.frame {
height: 25px;
width: 160px;
border: 1px solid red;
text-align: center;
white-space: nowrap;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.frame img {
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
How it works
Step by Step Execution
Consider this example:
<div class="frame">
<span class="helper"></span>
<img src="logo.png" alt="Logo" height="15">
</div>
.frame {
height: 25px;
width: 160px;
text-align: center;
border: 1px solid red;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.frame img {
vertical-align: middle;
}
What happens step by step
- The
.framecontainer is created with a fixed height of25px.
Real World Use Cases
Vertical image centering appears in many common interfaces:
- Navigation bars: center a logo inside a fixed-height header
- Thumbnail grids: center product images inside equal-size boxes
- Profile cards: keep avatars centered even when source images differ in size
- Buttons with icons: align an icon properly within a button area
- Media galleries: place uploaded images inside preview frames
- Admin dashboards: center status icons and logos inside table cells or panels
Example: product thumbnail box
<div class="product-thumb">
<span class="helper"></span>
<img src="shoe.jpg" alt="Shoe">
</div>
.product-thumb {
width: 120px;
height: 120px;
text-align: center;
border: 1px solid #ccc;
}
.product-thumb .helper {
: inline-block;
: ;
: middle;
}
{
: middle;
: ;
: ;
}
Real Codebase Usage
In real projects, developers usually choose the centering technique based on browser support and component design.
Common patterns
1. Modern component layout with Flexbox
Used when only modern browsers matter.
.avatar-frame {
display: flex;
align-items: center;
justify-content: center;
}
This is common in React, Vue, and general app UIs.
2. Legacy support fallback
Used in older enterprise systems or legacy apps that still support old IE versions.
<div class="avatar-frame">
<span class="helper"></span>
<img src="avatar.png" alt="Avatar">
</div>
This helper-based pattern is typical when maintaining older codebases.
3. Size constraints with max-width and max-height
Real code usually prevents overflow.
Common Mistakes
1. Assuming vertical-align: middle centers inside any div
Broken expectation:
.frame img {
vertical-align: middle;
}
This alone usually does not center the image relative to the full height of a block container.
Fix
Use a helper element, table-cell, or Flexbox depending on browser support.
2. Using line-height as a universal solution
This can seem to work in simple cases:
.frame {
height: 25px;
line-height: 25px;
}
But it may fail or look inconsistent with varying image sizes, mixed content, or more complex layouts.
Fix
Use line-height only for very simple cases. Prefer a more explicit centering technique when possible.
3. Forgetting that old browser support changes your options
Broken approach for IE7 support:
{
: flex;
: center;
: center;
}
Comparisons
| Technique | Works for vertical centering | Browser support | Best for | Notes |
|---|---|---|---|---|
vertical-align: middle alone | Sometimes | Very broad | Inline alignment | Not enough by itself for most block-container centering |
line-height + vertical-align | Simple cases | Very broad | Single-line, fixed-height containers | Can be fragile |
Helper element + vertical-align | Yes | Good for old browsers, including IE7-era needs | Legacy-compatible layouts | Common classic workaround |
display: table-cell |
Cheat Sheet
/* Legacy-friendly vertical centering with helper element */
.frame {
height: 25px;
width: 160px;
text-align: center;
white-space: nowrap;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.frame img {
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
<div class="frame">
<span class="helper"></span>
<img src="logo.png" alt="Logo">
</div>
Quick rules
vertical-alignworks on inline, inline-block, and table-cell elements- It does mean “center inside parent block” by itself
FAQ
Why does vertical-align: middle not center my image inside a div?
Because it aligns inline or table-cell elements relative to a line box, not automatically relative to the full height of a block container.
What is the best modern way to vertically center an image in CSS?
Use Flexbox with display: flex and align-items: center.
What should I use if I need IE7 support?
Use a helper element with display: inline-block and vertical-align: middle.
Can I use line-height to vertically center an image?
Yes, in simple fixed-height cases, but it is less reliable than other approaches.
Should I use position: absolute for vertical centering?
Only if the layout specifically needs positioned elements. It is usually not the simplest or most maintainable option.
How do I keep large images from overflowing the container?
Use max-width and max-height on the image.
Does text-align: center vertically center an image?
No. It only centers inline content horizontally.
Is still useful?
Mini Project
Description
Build a small logo frame component that displays logos of different heights inside fixed-size boxes. The purpose is to practice vertically centering images when the image dimensions are unknown, while keeping the layout consistent across multiple items.
Goal
Create a reusable image frame that horizontally and vertically centers differently sized images inside a fixed-height container.
Requirements
- Create a container with a fixed width and height.
- Center the image horizontally and vertically inside the container.
- Ensure large images do not overflow the container.
- Use a solution that works with older browsers rather than Flexbox.
- Display at least three images with different heights to confirm the centering works.
Keep learning
Related questions
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.
CSS Parent Selector Explained: Selecting a Parent <li> from an <a>
Learn whether CSS has a parent selector, how :has() works, and practical alternatives for styling a parent li from a child anchor.
CSS Previous Sibling Selector: What Exists and How to Work Around It
Learn whether CSS has a previous sibling selector, why it does not, and practical ways to style earlier elements using CSS alternatives.