Advertising remains a major revenue stream for many sites, and ad slots shape the interfaces we build. Laying out pages around them is a balancing act: the container must stay fluid while still presenting content and ads effectively. What follows covers common placements, styling tactics and responsive handling. Some of the CSS used is not fully supported in older browsers; wrapping it in @supports is a progressive-enhancement-friendly approach.
Where Ads Go: Standard Placements and Sizes
Standard sizes and placements exist so the industry can set pricing and compare metrics, even though the range of sizes in the wild is far larger.
Above the Navigation
Placing an ad above the main navigation works because navigation sits near the top of the page: it is prominent, suits a full-width layout and draws interaction. Alerts and notifications often occupy the same space. The simple approach is to place the ad element above the nav. To keep the navigation pinned once the ad scrolls away, use position: sticky. Per MDN, a sticky element is positioned according to the normal flow, then offset relative to its nearest scrolling ancestor and containing block. Fixed positioning is the tempting alternative, but it removes the nav from the document flow and pins it to the viewport, which is a harsher experience than sticky.

The reverse — making the ad sticky instead of the nav — needs weighing: hiding navigation can hurt UX, and persistent ads can interfere with content.
Header Units: Billboard and Leaderboard
- Billboard: a rectangular unit presented as a main call-to-action, typically 970⨉250. Its 970px width can set the size of the main content area. Despite its size it is rarely used (estimated at 2% of sites), though it commands higher rates.
- Leaderboard: a wide, short unit that often shares space with another element, typically 728⨉90. A 2019 SEMrush study cites 36% of publishers using leaderboards and 57% of advertisers buying them.
Even with a 970px container, a leaderboard leaves room for a logo alongside. Flexbox works well to separate logo from ad, and the container gets a fixed height equal to or greater than the leaderboard’s 90px.
.header .container {
/* Avoid content jumping */
height: 90px;
/* Flexibility */
display: flex;
align-items: center;
justify-content: space-between;
}
Sidebar: the Mid Page Unit
The mid page unit, also called the medium rectangle, is 300⨉250 and is the top-performing ad unit. It has shaped sidebar design for years.


With CSS grid, the same result comes from making the wrapper the grid container and defining two columns, the second exactly 300px wide:
<div class="wrapper">
<main>Main content</main>
<aside>Sidebar</aside>
</div>
.wrapper {
display: grid;
grid-template-columns: minmax(0, 1fr) 300px;
}
To draw more attention when a sidebar holds few ads, the same sticky technique used for navigation applies — with a catch: it affects reach when the sidebar is longer than the viewport or is dynamic.
<div class="wrapper">
<main>Main content</main>
<aside>
<div class="is-sticky">Sidebar</div>
</aside>
</div>
.is-sticky {
position: sticky;
top: 0;
}

Two fixes are common. The first keeps it simple and makes only important ads sticky; the CSS-Tricks sidebar follows this, with JavaScript toggling visibility.

The second uses a JavaScript library with scrolling behavior that fires sticky positioning only once the user reaches the end of the sidebar.

Missing or Failed Ads in the Sidebar
If a served ad is smaller than expected, or the ad script fails, the slot leaves whitespace that none of the above approaches handles. The markup:

<header class="header">
<div class="container">
<div class="header-content"> ... </div>
<aside class="aside">
<div class="aside-ad"> ... </div>
<div class="aside-content"> ... </div>
</aside>
</div>
</header>
The grid columns get the right measurements:
.header .container {
display: grid;
grid-template-columns: minmax(0, 1fr) 300px;
grid-gap: 24px;
min-height: 600px; /* Max height of the half-page ad */
}
The sidebar becomes a flexible container at the exact width and height the ad should be, hiding overflow:
.aside {
display: flex;
flex-direction: column;
overflow: hidden;
height: 600px;
width: 300px;
}
And .aside-content can scroll vertically if the widget needs to be shown:
.aside-content {
overflow-y: auto;
}

Styling
Styling ads is worthwhile for two reasons: it makes them feel native to the site, and it makes them more compelling. Guiding principles:
- Flexible layout. Use flexbox and grid so layouts reflow when an image fails to load or an element is removed, rather than refactoring markup.
- Consistency with site design. Ads that belong on the site are easier on the eye and borrow the trust established with readers; out-of-place ads look spammy and can compromise that trust.
- Clear call to action. Overbearing graphics or excess text muddy the action and can hurt performance.
- Accurate language. The ad should deliver on its promise and set correct expectations.
- High-resolution images. For image-led and particularly small units, double the pixel density by sizing images at twice the space they occupy.
Custom ads under your control are easy to fit to a layout. Dynamically injected script ads often cannot be wrapped in a styling div; ::before/::after pseudo-elements and [attribute^=value] selectors help there. Platforms commonly generate a unique ID per ad unit starting with a shared prefix that can target styles:
[id^="prefix-"] {
/* your style */
}
Responsive Ads
Platforms that inject ads by script usually bundle their own responsive styles. When they don't — or when you fully control the ads — handling screen sizes is crucial so the call to action stays clear at any width.
Reordering with Flexbox, Grid and nth-child
order in flexbox and grid changes visual placement without touching source order. That lets an ad be pulled “above the fold” for smaller viewports.

The markup:
<div class="items">
<div class="ad">...</div>
<div class="item">...</div>
<div class="item">...</div>
<!-- and so on... -->
</div>
:nth-child selects items by source order using a formula:
.items {
display: grid;
/* ... */
}
.item:nth-child(-n+2) {
order: -1;
}
@media only screen and (min-width: 768px) {
.article:nth-child(-n+3) {
order: -1;
}
}
The selector targets the first n elements and gives them a negative order, letting the ad slide between items depending on viewport size.
Static Ads
Some ads cannot flex. A flexible container that hides overflow still adds responsive behavior; the design needs to be intentional, with important content flush to the left and the right side cut off.

The markup for the flexible container:
<div class="ad">
<img src="https://i.imgur.com/udEua3H.png" alt="728x90" />
</div>
Depending on where the important content sits, justify the container to flex-start, flex-end or center while hiding the overflow:
.ad {
display: flex;
justify-content: flex-end; /* depending on the side your important content live */
overflow: hidden;
}
Responsive Images
The <picture> element lets the browser pick specific images at specific viewport sizes to fill the space well:
<picture>
<!-- Use the ad_728x90.jpg file at 768px and above -->
<source media="(min-width: 768px)">
<!-- The default file -->
<img src="ad_300x250">
</picture>
Beyond sharper rendering on high-DPI screens, <picture> also allows serving a more optimized asset for low-resolution screens that often sit on slower connections. srcset lets the browser choose the right resolution for the display; the two can be combined.
<imghttps://css-tricks.com/cdn-cgi/l/email-protection" class="__cf_email__">[email protected], [email protected] 2x" src="ad_300x250_fallback.jpg" />
The combination, starting from the correct ad width:
<picture>
<!-- ... -->
<source media="(min-width: 768px)"https://css-tricks.com/cdn-cgi/l/email-protection" class="__cf_email__">[email protected] 2x" />
<!-- ... -->
<imghttps://css-tricks.com/cdn-cgi/l/email-protection" class="__cf_email__">[email protected], [email protected] 2x" src="ad_300x250_fallback.jpg" />
</picture>
.selector {
width: 250px;
}
Media queries handle the different assets and sizes:
.selector {
width: 300px;
height: 250px;
}
@media (min-width: 768px) {
.responsive-ad {
width: 728px;
height: 90px;
}
}
For more flexibility, the image can scale relative to its parent container:
.selector img {
display: block;
width: 250px;
height: auto;
}
With srcset, performance is not a major worry: the browser downloads only the asset needed for a given resolution.



