Put the images in the same container, then let CSS control their layout. Use Flexbox for a simple row that can wrap or stack, and use CSS Grid when you need explicit columns and rows. Keep max-width: 100% and height: auto on the images so they stay inside their cells without distortion.
The basic Flexbox pattern
HTML provides the images and their source order; CSS determines whether they appear beside one another. This is a complete two-image example:
<div class='image-row'>
<img src='first.jpg' alt='Description of the first image'>
<img src='second.jpg' alt='Description of the second image'>
</div>
.image-row {
display: flex;
gap: 1rem;
}
.image-row img {
display: block;
max-width: 100%;
height: auto;
}
display: flex places the direct children in a row by default, while gap adds consistent space between them. The image rules prevent an intrinsic image width from spilling out of its available area and preserve each image’s proportions.
Choose Flexbox or Grid
| Need | Better starting point | Why |
|---|---|---|
| One row, flexible widths, spacing or wrapping | Flexbox | It is designed for one-dimensional arrangement and lets items share or divide the available width. |
| Defined rows and columns | CSS Grid | You can declare the number of tracks and change those tracks at a breakpoint. |
| Images that must stack on a narrow screen | Either | Flexbox can wrap or switch to a column; Grid can change from two tracks to one. |
Neither method is universally superior. Base the choice on the structure of the gallery, then check the narrow-screen layout, image fit and source order.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Make equal-width images
Give each flex item the same flexible size when the images should occupy equal shares of the row:
.image-row {
display: flex;
gap: 1rem;
}
.image-row img {
flex: 1;
min-width: 0;
max-width: 100%;
height: auto;
}
In practice, the flex sizing belongs on the items being laid out. If you need more predictable control over the image box itself, wrap each image:
.image-row {
display: flex;
gap: 1rem;
}
.image-card {
flex: 1;
min-width: 0;
}
.image-card img {
display: block;
width: 100%;
height: auto;
}
Use a different flex value when one image should be larger than another. For example, .image-card.featured { flex: 2; } gives that item twice the flexible share of an item with flex: 1. Do not set a fixed width larger than the container; let the available space determine the final size.
Stack or wrap images on small screens
A desktop row can become cramped when the viewport is narrow or zoomed. You can allow the items to move to another line:
.image-row {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.image-card {
flex: 1 1 18rem;
min-width: 0;
}
.image-card img {
display: block;
width: 100%;
height: auto;
}
The basis gives the browser a useful preferred width; when the available space is insufficient, the cards wrap instead of forcing horizontal overflow. For a guaranteed one-column mobile layout and a two-column wide layout, use a media query:
Rank #2
.image-row {
display: grid;
grid-template-columns: 1fr;
gap: 1rem;
}
.image-row img {
display: block;
width: 100%;
max-width: 100%;
height: auto;
}
@media (min-width: 40rem) {
.image-row {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
Choose the breakpoint where the actual images and their surrounding content become crowded, not a breakpoint tied to a particular phone model. A mobile-first single column is often the simplest default. Flexible layouts can also reflow without a breakpoint when the content has enough room to decide naturally.
When evaluating the result, check a narrow viewport and a zoomed page. W3C reflow techniques use a viewport equivalent to 320 CSS pixels as an objective for avoiding an unwanted horizontal scrollbar in ordinary vertically read content; this is a test condition for the technique, not a population statistic.
Build a multi-column gallery with CSS Grid
Grid is appropriate when the gallery has a defined two-dimensional structure. This example displays three columns on wider screens and one column on narrow screens:
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitches<div class='gallery'>
<img src='one.jpg' alt='Description of image one'>
<img src='two.jpg' alt='Description of image two'>
<img src='three.jpg' alt='Description of image three'>
<img src='four.jpg' alt='Description of image four'>
</div>
.gallery {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}
.gallery img {
display: block;
width: 100%;
max-width: 100%;
height: auto;
}
@media (max-width: 45rem) {
.gallery {
grid-template-columns: 1fr;
}
}
For a fixed two-column gallery, replace the first declaration with grid-template-columns: repeat(2, minmax(0, 1fr)). The minmax(0, 1fr) tracks allow a column to shrink to the available width instead of being forced wider by an oversized item.
Keep image proportions and choose the right file
Fit the image to its cell
Use max-width: 100% when an image should never exceed its layout region. Use height: auto to retain its natural aspect ratio. Setting both width and height to unrelated fixed values can stretch the image; if a deliberate crop is required, treat that as a separate design decision rather than a side-by-side alignment rule.
Rank #3
Serve responsive resources
CSS controls placement, but it does not decide which source file the browser downloads. Use srcset and sizes when the same image is available at several resolutions, or use <picture> for art direction or a different crop at a smaller viewport. These HTML features complement the Flexbox or Grid layout; they do not replace it.
Account for intrinsic dimensions
Images with very different proportions can have different heights even when their columns are equal. That is normally correct because automatic height preserves each image. If equal-height tiles are a visual requirement, define that requirement explicitly and test the crop at every viewport rather than distorting the source image.
Recommended Free Tools
Accessibility and document order
Write the HTML in the order people should read or navigate the images. A visual row does not justify changing the logical sequence. Avoid using Flexbox order, reverse directions or Grid placement to express meaningful content order: keyboard focus and assistive technology may follow the source order instead of the visual arrangement.
Give informative images useful alternative text that communicates their purpose. For a purely decorative image, an empty alt attribute can prevent redundant announcements when that matches the context. If an image is a link, make the alternative text describe the link’s destination or action.
W3C’s C31, C32 and C37 pages describe reflow, ordering and image-fitting techniques as examples rather than the only ways to meet accessibility requirements. Use them as practical checks: no unexpected horizontal scrolling at a narrow width, no misleading visual-versus-source sequence and no image escaping its layout area.
Rank #4
- Brand: Wiley
- Set of 2 Volumes
- A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
Common implementation problems
The images remain one above the other
Check that the container, not an unrelated ancestor, has display: flex or display: grid. Also check that the selector matches the actual class name and that another rule is not setting display: block on the container later in the cascade.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →The second image is pushed off-screen
The items may have fixed widths larger than the available container, or the flex container may not be allowed to wrap. Remove oversized fixed widths, keep max-width: 100% on the images and choose either flex-wrap: wrap or a narrow-screen one-column rule.
There is no space between images
Add gap to the Flexbox or Grid container. This is more predictable than relying on whitespace in the HTML and applies consistently to rows and columns.
Images overflow even though the layout is flexible
Inspect the image’s computed width and any inline style, then apply max-width: 100%. If the image is inside a flex item, ensure that the item can shrink by using a zero minimum width on the item when necessary.
The layout works on desktop but scrolls horizontally on mobile
Resize the viewport until the problem appears and identify the rule that keeps a track or item wider than the container. Allow Flexbox wrapping, switch Grid to one track, and test again at a zoom level as well as at the narrow viewport.
Best Value
The visual order is confusing for keyboard users
Remove CSS reordering and put the elements in the intended reading sequence in the HTML. Then use styling only to create the visual arrangement.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Or skip the browser setup
If your goal is to obtain screenshots of pages rather than build a gallery component, ScreenshotNeo provides a website screenshot API. It accepts a URL and returns a PNG, JPEG, WebP or PDF. Before capture, it accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups and chat widgets; each cleanup step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads and cache hits are not billed, and the response identifies the page verdict and billing status in X-Page-Verdict and X-Billed headers.
Use the API documentation at https://screenshotneo.com/docs/ for authentication and options. A one-call capture looks like this:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
Python:
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://example.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
Node.js:
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
ScreenshotNeo also offers an MCP server with take_screenshot, get_page_info and capture_pdf tools for Claude, Cursor and other MCP clients. Other available controls include full-page capture with lazy images loaded, CSS-selector element capture, dark mode, device presets and custom viewports, retina scale, PDF paper settings and page ranges, custom CSS or JavaScript, pre-capture clicks, selector waits, delay or network-idle waits, request blocking, custom headers and cookies, user agent, authorization, timezone, geolocation, transparent backgrounds, resizing, TTL-based caching, signed image links, asynchronous webhooks, bulk capture for up to 100 URLs per call, a usage API and an OpenAPI specification.
Free tools Windows power users keep installed
One-click scans. No signup required.
The free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 screenshots, and every feature is included on every plan. Create a free ScreenshotNeo account to get started.
Final checks before publishing
- Confirm the images share one Flexbox or Grid container.
- Use a gap instead of manual spacer elements.
- Keep images constrained with
max-width: 100%and automatic height. - Test wrapping or a one-column layout at the width where the row becomes crowded.
- Verify that HTML source order matches the intended reading and keyboard order.
- Provide meaningful alternative text, or an empty
altfor decorative images when appropriate. - Test with zoom and a narrow viewport for unintended horizontal scrolling.
Frequently Asked Questions
Do I need a special HTML tag to align images side by side?
No. A normal container with adjacent <img> elements is sufficient; Flexbox or Grid on that container supplies the alignment.
Why do equal columns sometimes have unequal heights?
Equal column widths do not change the source aspect ratios. With automatic height, a taller or narrower original image can occupy more vertical space while remaining undistorted.
Can I use the same layout for linked images?
Yes. Put each image inside its own link or card, keep the links in the intended HTML order, and apply the sizing rule to the image or its immediate wrapper.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteQuick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

