Tree shaking is a technique to remove unused code from bundles to reduce file size. It analyzes import and export statements to determine unused code, which can then be excluded from the bundle. CSS frameworks like Bootstrap often include unused code by default that tree shaking tools can remove to minimize file size. Tools like UnCSS analyze CSS usage on a page to remove unused styles, but have limitations and an easier alternative is to only import needed components from frameworks during the build process.