Vertical alignment can be a bit tricky when trying to position text next to an image using HTML and CSS. Whether you are creating a responsive design or simply looking for a neat layout, understanding how to vertically align elements is crucial. This tutorial will guide you through different methods of achieving vertical alignment between text and images.
Introduction
In web development, laying out content in a visually appealing manner often requires precise control over the positioning of elements. One common scenario is placing text next to an image while ensuring both are aligned vertically. Depending on your needs, various CSS techniques can be employed: from basic properties like vertical-align
and line-height
, to more advanced methods using flexbox or CSS tables.
Basic Techniques
Using vertical-align
The simplest way to align text with an image is by applying the vertical-align
property. This property affects inline elements, such as images and spans within a line box.
<div>
<img src="https://via.placeholder.com/30" alt="small img" style="vertical-align: middle;">
<span>Aligned Text</span>
</div>
In the above example, setting vertical-align: middle
on the image will align its baseline to the middle of the line box containing both the image and text. Note that if you need perfect alignment for both elements, apply vertical-align: middle
to each inline element.
Advanced Techniques
Flexbox Layout
Flexbox is a powerful CSS layout module designed for building complex layouts with ease. It offers excellent control over alignment, direction, order, and size of items within a container.
<div class="flex-container">
<img src="https://via.placeholder.com/60x60" alt="Sample Image">
<span>Aligned Text</span>
</div>
<style>
.flex-container {
display: flex;
align-items: center;
}
</style>
Here, the display: flex
and align-items: center
properties ensure that both the image and text are vertically centered within their container.
CSS Tables
CSS tables offer another method for vertical alignment by leveraging table-related properties on block-level elements. This technique mimics traditional HTML tables but without using actual <table>
tags, which allows for more flexibility in layout design.
<div class="css-table">
<img src="https://via.placeholder.com/60x60" alt="Sample Image">
<span>Aligned Text</span>
</div>
<style>
.css-table {
display: table;
}
.css-table img, .css-table span {
display: table-cell;
vertical-align: middle;
}
</style>
In this method, both the image and text are treated as cells in a CSS table, making it easy to vertically align them using vertical-align
.
Pseudo-element Technique
Using pseudo-elements is an innovative way to achieve center alignment. This approach involves adding an invisible element to assist with vertical centering.
<div class="pseudo-container">
<img src="https://via.placeholder.com/60x60" alt="Sample Image">
<span>Aligned Text</span>
</div>
<style>
.pseudo-container {
position: relative;
}
.pseudo-container:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.pseudo-container img, .pseudo-container span {
display: inline-block;
vertical-align: middle;
}
</style>
Here, a pseudo-element is used to create an invisible helper that allows for the centering of elements.
Conclusion
Vertical alignment between text and images can be achieved using various CSS techniques. Each method has its advantages depending on the specific layout requirements and browser support needed. Flexbox offers modern flexibility, while CSS tables provide a solid alternative with broad compatibility. By understanding these methods, you can create beautifully aligned content in your web designs.
Whether using basic inline properties or exploring more advanced layout modules like flexbox, mastering vertical alignment enhances both the functionality and aesthetics of your projects.