Categories: css-tricks.com

Resources aren’t requested by CSS, but by the DOM

This is a good tweet from Harry:

I like it because, as he says, it’s the correct way to think about it. It helps form a mental model of how websites work.

Just to spell it out a bit more…

/*
  Just because I'm in the CSS, doesn't mean I'll load!
  In order for `myfont.woff2` to load, a selector needs to 
  set `font-family: 'MyWebFont';` AND something in the DOM
  needs to match that selector for that file to be requested.
*/@font-face {
  font-family: 'MyWebFont';
  src: url('myfont.woff2') format('woff2');
}

/*
  Just because I'm in the CSS, doesn't mean I'll load!
  In order for `whatever.jpg` to load, the selector
  `.some-element` needs to be in the DOM. 
*/.some-element {
  background-image: url(whatever.jpg);
}

The post Resources aren’t requested by CSS, but by the DOM appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.

hnikoloski

Share
Published by
hnikoloski

Recent Posts

Demystifying Screen Readers: Accessible Forms & Best Practices

This is the 3rd post in a small series we are doing on form accessibility.…

3 weeks ago

Managing User Focus with :focus-visible

This is going to be the 2nd post in a small series we are doing…

1 month ago

The Power of :has() in CSS

Hey all you wonderful developers out there! In this post we are going to explore…

1 month ago

Accessible Forms with Pseudo Classes

Hey all you wonderful developers out there! In this post, I am going to take…

2 months ago

Passkeys: What the Heck and Why?

These things called passkeys sure are making the rounds these days. They were a main attraction at W3C…

1 year ago

Some Cross-Browser DevTools Features You Might Not Know

I spend a lot of time in DevTools, and I’m sure you do too. Sometimes…

1 year ago