visit
main > div.blog-section + article > * {
/* Code here */
}
.hero-image {
/* Code here */
}
Inlining that critical CSS into a <style>
tag in your <head>
<style>
/* critical styles */
body { font-family: sans-serif; color: #111; }
</style>
<!-- load remaining styles -->
<link rel="stylesheet" href="/css/main.css" media="print" onload="this.media='all'">
<noscript>
<link rel="stylesheet" href="/css/main.css">
</noscript>
@import
The @import
at-rule allows any CSS file to be included within another. This appears a reasonable way to load smaller components and fonts. But it is not.
@import
rules can be nested so the browser must load and parse each file in series.
Multiple <link>
tags within the HTML will load CSS files in parallel, which is considerably more efficient — especially when using HTTP/2.
/* main.css */
@import url("style1.css");
@import url("style2.css");
@import url("style3.css");
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="layout.css">
<link rel="stylesheet" href="carousel.css">
The <link>
tag provides an optional preload attribute that can start a download immediately rather than waiting for the real reference in the HTML.
<!-- preload styles -->
<link rel="preload" href="/css/main.css" as="style" />
<!-- some code -->
<!-- load preloaded styles -->
<link rel="stylesheet" href="/css/main.css" />
Rather than using a single site-wide CSS file, progressive rendering is a technique that defines individual stylesheets for separate components. Each is loaded immediately before a component is referenced in the HTML.
Each <link>
still blocks rendering, but for a shorter time, because the file is smaller. The page is usable sooner, because each component renders in sequence; the top of the page can be viewed while remaining content loads.
<head>
<!-- core styles used across components -->
<link rel='stylesheet' href='base.css' />
</head>
<body>
<!-- header component -->
<link rel='stylesheet' href='header.css' />
<header>...</header>
<!-- primary content -->
<link rel='stylesheet' href='content.css' />
<main>
<!-- form styling -->
<link rel='stylesheet' href='form.css' />
<form>...</form>
</main>
<!-- header component -->
<link rel='stylesheet' href='footer.css' />
<footer>...</footer>
</body>
<!-- core styles loaded on all devices -->
<link rel="stylesheet" href="core.css">
<!-- served to screens at least 40em wide -->
<link rel="stylesheet" media="(min-width: 40em)" href="40em.css">
<!-- served to screens at least 80em wide -->
<link rel="stylesheet" media="(min-width: 80em)" href="80em.css">
<svg xmlns="//www.w3.org/2000/svg" viewBox="0 0 800 600">
<circle cx="400" cy="300" r="50" stroke-width="5" stroke="#f00" fill="#ff0" />
<svg>
.mysvgbackground {
background: url('data:image/svg+xml;utf8,<svg xmlns="//www.w3.org/2000/svg" viewBox="0 0 800 600"><circle cx="400" cy="300" r="50" stroke-width="5" stroke="#f00" fill="#ff0" /></svg>') center center no-repeat;
}
<body>
<svg class="mysvg" xmlns="//www.w3.org/2000/svg" viewBox="0 0 800 600">
<circle cx="400" cy="300" r="50" />
<svg>
</body>
circle {
stroke-width: 1em;
}
.mysvg {
stroke-width: 5px;
stroke: #f00;
fill: #ff0;
}
Note that using an SVG within an <img>
tag or as a CSS background image means they’re separated from the DOM, and CSS styling will have no effect.
You can encode images to base64 strings, which you can use as data URIs in HTML <img>
tags and CSS backgrounds.
.background {
background-image: url('data:image/jpg;base64,ABC123...');
}
.svgbackground {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="//www.w3.org/2000/svg" viewBox="0 0 600 600"><circle cx="300" cy="300" r="150" stroke-width="3" stroke="#f00" fill="#ff0" /></svg>');
}
border-radius
box-shadow
text-shadow
opacity
transform
filter
position: fixed
backdrop-filter
background-blend-mode