DEV Community

Cover image for Generate OG Images with HTML and CSS in CI
WebsiteGeek
WebsiteGeek

Posted on Edited on

Generate OG Images with HTML and CSS in CI

Every blog post I ship needs a social preview image, and for years I made them by hand. I would open a design tool, copy the title, adjust the layout, export a PNG, and upload it. It worked for five posts. It fell apart at fifty.

The breaking point came during a docs sprint. We merged twelve pages in one afternoon, and nobody updated the preview images. Shared links on X and Slack showed a generic gray box. Click-through dropped, and I spent the next morning fixing images one by one.

Why hand-made social cards do not scale

Open Graph images are simple in theory. They are 1200 by 630 PNGs that platforms show when someone shares your link. In practice, they multiply fast. Every post, every docs page, every changelog entry wants its own card.

Manual design creates drift. Titles get out of date, author names change, and branding shifts. Nobody wants to open a graphics editor to fix a typo in a card. So the cards rot, and your links look broken even when the content is solid.

The template problem

Most teams try a template next. That helps, but someone still has to fill in the template and export the file. It is a chore that sits outside the normal code review flow. It gets skipped under deadline pressure.

The fix is to treat preview images like any other build artifact. The title already lives in your front matter. The author already lives in your CMS. The image should be generated from that same data, on every build.

Rendering cards from HTML and CSS

HTML and CSS are a natural fit for social cards. You already know how to build a card layout with flexbox, padding, and web fonts. You can preview it in a browser, adjust spacing, and reuse your brand tokens.

The missing step is turning that markup into a PNG at build time. A headless browser or a dedicated renderer loads your template, applies your styles, waits for fonts, and takes a clean screenshot at exactly 1200 by 630. No manual exports.

What a CI pipeline looks like

My current setup keeps a cards folder next to the content. Each content type has one template: post, docs page, and release note. A build script reads the title, date, and tag from front matter and injects them into the template.

The renderer runs after the site builds but before deploy. Changed pages get fresh PNGs. Unchanged pages keep cached files. The output goes to the same static folder as the rest of the site, so the meta tags just point at a local path.

Fonts, layout, and sharp text

Fonts cause most rendering bugs. A card that looks crisp on your laptop can ship with fallback fonts in CI because the build machine never installed your brand font. Pin your fonts with local WOFF2 files and use font-display rules that wait for load.

Keep layouts simple. Large title text, one accent bar, and a small logo lockup beat a busy design at small sizes. Test your template at 600 pixels wide, because that is close to how it appears in a timeline. If it reads there, it reads everywhere.

Caching and invalidation

Do not re-render every card on every commit. Hash the inputs: title, template version, and theme values. Only render when the hash changes. This keeps builds fast and makes diffs easy to review.

I store the hash in the filename or in a small manifest. When a post title changes, the pipeline picks it up and regenerates that single image. Everything else stays cached.

Putting it into practice

Start with one template and ten pages. Hard-code the data first and get the pixels right. Then wire in front matter, then add the CI step. Review the PNG diff in your pull request like any other snapshot.

If you want to skip the browser setup, I now render my cards with an HTML-to-image renderer with monthly subscription plans that turns markup into clean PNGs without managing servers, and you can see the approach here: HTML to Image API for automated social cards. It fits the same template-in, PNG-out flow I described above.

Once the pipeline runs, hand-made cards feel strange. Titles stay in sync, branding stays consistent, and every shared link looks finished. That polish compounds across hundreds of posts.

Top comments (0)