---
url: "https://web.dev/articles/css-web-vitals"
titulek: "CSS for Web Vitals | Articles | web.dev"
h1: "CSS for Web Vitals Stay organized with collections Save and categorize content based on your preferences."
popis: "This article covers CSS-related techniques for optimizing Web Vitals."
jazyk: "en"
kanonicka: "https://web.dev/articles/css-web-vitals"
drobecky: ["Articles", "CSS for Web Vitals"]
zdroj: "live"
wayback_snapshot: ""
stazeno: "2026-09-13 22:29:18"
znaku: 10121
slov: 1438
kodovani: "iso-8859-2"
sha256: "11074b4c209baaf54a9a8490cb9872c3183ab1121e72848f46baf0d9f6465c4b"
---
CSS for Web Vitals
Stay organized with collections
Save and categorize content based on your preferences.
CSS-related techniques for optimizing Web Vitals
Katie Hempenius
Una Kravets
The way you write your styles and build layouts can have a major impact on [Core
Web Vitals](/explore/learn-core-web-vitals). This is particularly true for
[Cumulative Layout Shift (CLS)](/articles/cls) and [Largest Contentful
Paint (LCP)](/articles/lcp). This article covers CSS-related techniques for optimizing Web Vitals. These
optimizations are broken down by different aspects of a page: layout, images,
fonts, animations, and loading. Along the way, we'll explore improving an
[example page](https://codepen.io/una/pen/vYyLKvY): Layout Inserting content into the DOM Inserting content into a page after the surrounding content has already loaded
pushes everything else on the page down. This causes [layout
shifts](/articles/cls#layout_shifts_in_detail). [Cookie notices](/articles/cookie-notice-best-practices), particularly
those placed at the top of the page, are a common example of this problem. Other
page elements that often cause this type of layout shift when they load include
ads and embeds. Identify The Lighthouse "Avoid large layout shifts" audit identifies page elements that
have shifted. For this demo, the results look like this: The cookie notice is not listed in these findings because the cookie notice
itself isn't shifting when it loads. Rather, it causes the items below it on the
page (that is, `div.hero` and `article`) to shift. For more information on
identifying and fixing layout shifts, see [Debugging Layout
Shifts](/articles/debug-layout-shifts). Fix Place the cookie notice at the bottom of the page using absolute or fixed
positioning. Before: `.banner { position: sticky; top: 0; }` After: `.banner { position: fixed; bottom: 0; }` Another way to fix this layout shift would be to reserve space for the cookie
notice at the top of the screen. This approach is equally effective. For more
information, see [Cookie notice best
practices](/articles/cookie-notice-best-practices). Images Images and Largest Contentful Paint (LCP) Images are commonly the Largest Contentful Paint (LCP) element on a page. Other
[page elements that can be the LCP
element](/articles/lcp#what-elements-are-considered) include text blocks
and video poster images. The time at which the LCP element loads determines LCP. It's important to note that a page's LCP element can vary from page load to page
load depending on the content that is visible to the user when the page is first
displayed. For example, in this demo, the background of the cookie notice, the
hero image, and the article text are some of the potential LCP elements. In the example site, the background image of the cookie notice is actually a
large image. To improve LCP, you could instead paint the gradient in CSS, rather
than load an image to create the effect. Fix Change the `.banner` CSS to use a CSS gradient rather than an image: Before: `background: url("https://cdn.pixabay.com/photo/2015/07/15/06/14/gradient-845701\_960\_720.jpg")` After: `background: linear-gradient(135deg, #fbc6ff 20%, #bdfff9 90%);` Images and layout shifts Browsers can only determine the size of an image once the image loads. If the
image load occurs after the page has been rendered, but no space has been
reserved for the image, a layout shift occurs when the image appears. In the
demo, the hero image is causing a layout shift when it loads. Identify To identify images without explicit `width` and `height`, use Lighthouse's
"Image elements have explicit width and height" audit. In this example, both the hero image and article image are missing `width` and
`height` attributes. Fix Set the `width` and `height` attributes on these images to avoid layout shifts. Before: `
` After: `
`
The image now loads without causing a layout shift.
Fonts Fonts can delay text rendering and cause layout shifts. As a result, it is
important to deliver fonts quickly. Delayed text rendering By default, a browser will not immediately render a text element if its
associated web fonts have not loaded yet. This is done to prevent a ["flash of
unstyled text" (FOUT)](https://en.wikipedia.org/wiki/Flash_of_unstyled_content).
In many situations, this delays [First Contentful Paint
(FCP)](/articles/fcp). In some situations, this delays Largest Contentful
Paint (LCP). Layout shifts Font swapping, while excellent for displaying content to the user quickly, has
the potential to cause layout shifts. These layout shifts occur when a web font
and its fallback font take up different amounts of space on the page. Using
similarly proportioned fonts will minimize the size of these layout shifts.
In this example, font swapping caused page elements to shift upwards by five pixels.
Identify To see the fonts that are being loaded on a particular page, open the
**Network** tab in DevTools and filter by **Font**. Fonts can be large files, so
only using fewer fonts is generally better for performance. To see how long it takes for the font to be requested, click on the **Timing**
tab. The sooner that a font is requested, the sooner it can be loaded and used. To see the request chain for a font, click on the **Initiator** tab.
Generally speaking, the shorter the request chain, the sooner the font can be
requested. Fix This demo uses the Google Fonts API. Google Fonts provides the option to load
fonts via `` tags or an `@import` statement. The `` code snippet
includes a `preconnect` resource hint. This should result in faster
stylesheet delivery than using the `@import` version. At a very high-level, you can think of [resource
hints](https://www.w3.org/TR/resource-hints/#resource-hints) as a way to hint
to the browser that it will need to set up a particular connection or download a
particular resource. As a result, the browser will prioritize these actions.
When using resource hints, keep in mind that prioritizing a particular action
takes away browser resources from other actions. Thus, resource hints should be
used thoughtfully and not for everything. For more information, see [Establish
network connections early to improve perceived page
speed](/articles/preconnect-and-dns-prefetch). Remove the following `@import` statement from your stylesheet: `@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400&family=Roboto:wght@300&display=swap');` Add the following `` tags to the `