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">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="19" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="17" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="15" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="13" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="11" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="9" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="7" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="5" />
</div>
<div class="frame">
<img src="http://jsfiddle.net/img/logo.png" alt="Logo" height="3" />
</div>
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
Understanding vertical alignment helps you avoid trial-and-error CSS and choose a solution that matches the browser support you need.
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
- The helper
spanis as tall as the container. - The image and helper are both inline-level aligned with
vertical-align: middle. - This causes the image to appear vertically centered.
Simpler line-height technique
If the container has exactly one line and the image height is not larger than the container, this may work:
.frame {
height: 25px;
line-height: 25px;
text-align: center;
}
.frame img {
vertical-align: middle;
}
This is simple, but it can be unreliable for varying image sizes and edge cases.
Modern Flexbox technique
In modern browsers, this is the cleanest solution:
<div class="frame">
<img src="logo.png" alt="Logo">
</div>
.frame {
display: flex;
align-items: center;
justify-content: center;
height: 25px;
width: 160px;
border: 1px solid red;
}
.frame img {
max-height: 25px;
max-width: 160px;
}
This is easy to read and very reliable, but it does not support IE7.
display: table-cell technique
Another older approach is:
.frame {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 25px;
width: 160px;
border: 1px solid red;
}
This can work well in many cases, but support and behavior may vary in very old browsers.
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. - Inside it, there are two inline-level items:
- the
.helperspan - the
img
- the
- The helper is
inline-blockand hasheight: 100%, so it becomes25pxtall. - Both the helper and the image use
vertical-align: middle. - Because they are on the same inline line, the browser aligns their middles.
- Since the helper is the full height of the container, the image ends up visually centered within the container.
Why this succeeds
The image now has another inline element with the same container height to align against. Without that, vertical-align often does not produce the expected visual center.
Small trace example
- container height:
25px - helper height:
25px - image height:
15px - both align to the middle of the same line box
- image is centered vertically within the
25pxarea
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 {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.product-thumb img {
vertical-align: middle;
max-width: 100%;
max-height: 100%;
}
This keeps different product images visually centered inside matching boxes.
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.
.avatar-frame img {
max-width: 100%;
max-height: 100%;
}
4. Defensive styling
Developers often combine centering with rules that prevent layout bugs:
.frame {
overflow: hidden;
white-space: nowrap;
}
This helps keep the layout stable when images or content vary.
Why teams care
- consistent UI regardless of uploaded image dimensions
- fewer browser-specific bugs
- easier maintenance when styling reusable components
- better visual polish in lists, cards, and tables
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:
.frame {
display: flex;
align-items: center;
justify-content: center;
}
Fix
If IE7 is required, use the helper-element technique instead.
4. Not constraining image size
If the image is larger than the container, centering alone will not solve overflow.
.frame img {
vertical-align: middle;
}
Fix
Also limit the image dimensions:
.frame img {
max-width: 160px;
max-height: 25px;
}
5. Omitting alt text in examples
Broken HTML:
<img src="logo.png">
Fix
Always include alt text:
<img src="logo.png" alt="Logo">
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 | Yes | Good older support in many browsers | Structured box alignment | Can feel less intuitive |
| Flexbox | Yes | Modern browsers | Most modern layouts | Cleanest solution today |
vertical-align vs text-align
| Property | What it controls |
|---|---|
text-align | Horizontal alignment of inline content |
vertical-align | Vertical alignment of inline or table-cell elements |
Which one should you choose?
- Need IE7 support: use the helper element approach
- Need modern browser support only: use Flexbox
- Need a quick simple case:
line-heightmay be enough - Need table-like alignment behavior: try
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 not mean “center inside parent block” by itself
- For old browsers, use a helper element if needed
- For modern browsers, use Flexbox
- Add
max-widthandmax-heightto prevent overflow - Use
text-align: centerfor horizontal centering of inline images
Modern version
.frame {
display: flex;
align-items: center;
justify-content: center;
}
Common edge cases
- unknown image size
- fixed-height container
- very small images
- very large images that need scaling
- legacy browser support requirements
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 display: table-cell still useful?
Yes, especially in some older layouts, but Flexbox is usually preferred for modern projects.
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
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
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.