Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

defer-css

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

defer-css

Load css in your page without affecting load speed.

  • 0.0.4
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Defer Css

A simple function to defer css in your html tag.

Direct Browser Installation
<!--Using JsDeliver CDN-->
<script src="https://cdn.jsdelivr.net/npm/defer-css"></script>

<!-- Or Using UnPkg CDN-->
<script src="https://unpkg.com/defer-css"></script>

<!-- Or Using Bundle.run-->
<script src="https://bundle.run/defer-css"></script>
From Package Managers

You can include using require or import but defer-css does not export anything.

It sets window.deferCss && window.deferCssData

Usage

By default defined styles are loaded before the first <link> element in your page

<html>
    <head>
        <!-- Styles will be placed before this link element-->
        <link id="defer-css"/>
    </head>
</html>

You can change this to your custom id

<head>
    <link href="....">
    <link href="....">
    <link href="....">
    
    <!-- Styles will be placed before this add-css-here element-->
    <link id="add-css-here"/>
</head>

Load Css

deferCss([
    {href: 'style-1.min.css', crossorign: 'anonymous'},
    {href: 'style-2.min.css', crossorign: 'anonymous'}
], 'add-css-here')

Adds the following styles

    <link rel="stylesheet" href="style-1.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="style-2.min.css" crossorigin="anonymous">

If css object includes an onload function, it is executed when the css file is loaded.

deferCss([
    {
        href: 'style-1.min.css', 
        onload: function() {
            // do something
        }
    },
])
Multiple deferCss

Lets say you want to mount css in multiple places.

<head>
    <link id="main-css"></main-css>
    <link id="other-css"></other-css>
</head>
deferCss(['main-css-1.css', 'main-css-2.css'], 'main-css');
deferCss(['other-css-1.css', 'other-css-2.css'], 'other-css');

The deferCssData includes details you may need.

 deferCssData = {
    // Element mounted on, default = 'link'
    link: {
        total: 1, // total number of css loaded
        loaded: 3
    }
}
Example
<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Defer Css Test</title>
    <script src="https://cdn.jsdelivr.net/npm/defer-css"></script>
    <!-- Links are placed before this element -->
    <add-css-here/>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <div class="card shadow" style="margin-top: 20vh">
                <div class="card-body text-center">
                    <h1>Hello, world!</h1>
                    <h5 class="text-primary">
                        Bootstrap Loaded with defer-css
                    </h5>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    let scripts = [
        {href: 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'},
        // Style with onload
        {
            href: './app.css',
            onload: function () {
                console.log(this.href + ' Style Loaded!');
                // value of deferCssData
                console.log(deferCssData);
            }
        },
    ];

    // Defer scripts
    deferCss(scripts, 'add-css-here');
</script>
</body>
</html>

FAQs

Package last updated on 10 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc