Lesson 6 of 60 – Bootstrap CDN
10%

Bootstrap CDN

A CDN, or Content Delivery Network, allows you to load Bootstrap files from a server on the internet instead of downloading and storing the Bootstrap files inside your project.

Using a CDN is one of the easiest ways to start working with Bootstrap. You simply add the Bootstrap CSS and JavaScript links to your HTML page.

Note: CDN is especially convenient for learning Bootstrap and for projects where using externally hosted Bootstrap resources is appropriate.

1. What is a CDN?

CDN stands for Content Delivery Network. It is a distributed network of servers used to deliver files to users over the internet.

Websites can use a CDN to load resources such as CSS, JavaScript, fonts, images, and libraries.

2. Bootstrap CDN

Bootstrap CDN allows your HTML page to load Bootstrap resources from an external CDN instead of storing those files locally.

This means you can start using Bootstrap without manually downloading the complete Bootstrap package.

3. Bootstrap CSS CDN

Bootstrap CSS can be added inside the <head> section using a <link> element.

<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet">

After adding this link, Bootstrap CSS classes become available on the page.

4. Bootstrap JavaScript CDN

Bootstrap's JavaScript bundle can be added before the closing </body> tag.

<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
</script>

The JavaScript bundle enables Bootstrap's interactive components.

5. Why Use Bootstrap CDN?

There are several reasons developers use a CDN when working with Bootstrap.

  • No manual Bootstrap download is required.
  • Easy to add to an HTML page.
  • Useful for learning and small projects.
  • Bootstrap files are hosted externally.
  • Different Bootstrap versions can be referenced explicitly.

6. Basic Bootstrap CDN Structure

<!DOCTYPE html>
<html>

<head>

    <title>
        Bootstrap CDN
    </title>

    <link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
    rel="stylesheet">

</head>

<body>

    ...

    <script
    src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
    </script>

</body>

</html>

7. CSS Link Goes Inside Head

The Bootstrap CSS CDN link is normally placed inside the <head> section.

<head>

    <link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
    rel="stylesheet">

</head>

This allows Bootstrap styles to be available when the page is rendered.

8. JavaScript Link Goes Near the End

Bootstrap's JavaScript bundle can be placed near the end of the <body> section.

<body>

    ...

    <script
    src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
    </script>

</body>

9. Using a Bootstrap Button

After loading Bootstrap CSS, you can immediately use Bootstrap classes.

<button class="btn btn-primary">
    Click Me
</button>

The btn and btn-primary classes come from Bootstrap.

10. Using Bootstrap Container

The Bootstrap container class can be used after adding the Bootstrap CSS CDN.

<div class="container">

    <h1>
        Bootstrap CDN
    </h1>

</div>

11. Using Bootstrap Grid

The grid system is also available when Bootstrap CSS is loaded.

<div class="container">

    <div class="row">

        <div class="col-md-6">
            Left
        </div>

        <div class="col-md-6">
            Right
        </div>

    </div>

</div>

12. jsDelivr CDN

One commonly used CDN for Bootstrap is jsDelivr. Bootstrap resources can be referenced using jsDelivr URLs.

https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/

The version number in the URL specifies which Bootstrap version is requested.

13. Version Number in CDN URL

A Bootstrap CDN URL can contain a version number.

bootstrap@5.3.3

Here, 5.3.3 identifies the Bootstrap version being requested.

14. Why Specify a Version?

Specifying a version makes it clear which Bootstrap release your project is using.

This can help keep the project consistent and avoid unintentionally loading a different release.

15. Minified CSS

The bootstrap.min.css file is a minified version of the Bootstrap CSS.

Minified files remove unnecessary whitespace and formatting to reduce file size.

bootstrap.min.css

16. Minified JavaScript

The bootstrap.bundle.min.js file is a minified Bootstrap JavaScript bundle.

It is commonly used when adding Bootstrap's JavaScript components to a web page.

17. Bootstrap Bundle

The Bootstrap bundle contains Bootstrap's JavaScript along with Popper.

This is convenient for components such as dropdowns, tooltips, and popovers that use Popper functionality.

bootstrap.bundle.min.js

18. CSS Only Bootstrap

If your page only needs Bootstrap's CSS features, you can include the CSS file without adding Bootstrap's JavaScript.

For example, grid, typography, spacing, colors, and basic button styling do not require Bootstrap JavaScript.

19. When JavaScript is Needed

Bootstrap JavaScript is required for interactive components such as:

  • Modal
  • Dropdown
  • Carousel
  • Collapse
  • Offcanvas
  • Tooltip
  • Popover
  • Toast

20. Bootstrap CDN and Internet Connection

When Bootstrap resources are loaded directly from a CDN, the browser needs access to that external resource.

If the CDN resource cannot be reached, Bootstrap may not load correctly on the page.

21. CDN vs Local Bootstrap

Feature CDN Local Files
Download Required No Yes
Internet Resource Yes Not for Bootstrap files
Setup Very Easy Requires file management
Customization Can use custom CSS Can modify project resources as needed

22. Bootstrap CDN with Integrity

CDN resources can be used with Subresource Integrity, commonly called SRI. An integrity attribute allows the browser to verify that the downloaded resource matches an expected cryptographic hash.

When copying CDN links from official documentation, use the complete integrity and cross-origin attributes supplied with that resource.

23. Cross-Origin Attribute

When an SRI-enabled external resource is used, the crossorigin attribute is commonly included as part of the CDN resource tag.

<link
rel="stylesheet"
href="..."
integrity="..."
crossorigin="anonymous">

The exact integrity value depends on the specific resource.

24. Complete Bootstrap CDN Page

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
    name="viewport"
    content="width=device-width,
    initial-scale=1.0">

    <title>
        Bootstrap CDN Example
    </title>

    <link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
    rel="stylesheet">

</head>

<body>

    <div class="container mt-5">

        <h1 class="text-primary">
            Welcome to Bootstrap
        </h1>

        <p class="lead">
            Bootstrap CDN Example
        </p>

        <button class="btn btn-success">
            Start Learning
        </button>

    </div>

    <script
    src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">
    </script>

</body>

</html>

25. Testing Bootstrap CDN

After adding the CDN links, test Bootstrap by using a few Bootstrap classes.

<h1 class="text-primary">
    Bootstrap Test
</h1>

<button class="btn btn-danger">
    Test Button
</button>

If the elements display with Bootstrap styling, the CSS CDN is working.

26. Common CDN Mistake

A common mistake is copying an incomplete or incorrect CDN URL.

Always check that the CSS or JavaScript URL points to the intended Bootstrap resource and version.

27. Another Common Mistake

Another mistake is including Bootstrap JavaScript when the project needs a Bootstrap interactive component but forgetting the correct JavaScript bundle.

For Bootstrap 5, the bundle is a convenient option because it includes Popper.

28. CDN Version Consistency

Try to use matching Bootstrap versions for the CSS and JavaScript resources in the same project.

bootstrap@5.3.3
bootstrap@5.3.3

Using the same version helps avoid unexpected compatibility problems.

29. Advantages of Bootstrap CDN

  • Easy to set up.
  • No manual Bootstrap download is required.
  • Simple HTML integration.
  • Specific versions can be referenced.
  • Useful for learning Bootstrap.
  • Useful for many simple web projects.
  • Bootstrap CSS and JavaScript can be loaded separately.

30. Bootstrap CDN Best Practice

When using Bootstrap through a CDN, choose the Bootstrap version intentionally, use the appropriate CSS and JavaScript resources, and keep the resource versions consistent.

For production websites, also consider the project's requirements, security practices, caching strategy, availability requirements, and whether locally hosted assets or a package-based build process would be more appropriate.

📌 Key Points

  • CDN stands for Content Delivery Network.
  • Bootstrap can be loaded from a CDN.
  • Bootstrap CSS is normally added inside the head section.
  • Bootstrap JavaScript can be added near the end of the body.
  • jsDelivr is one CDN that can host Bootstrap resources.
  • A version number can be specified in the CDN URL.
  • Bootstrap's bundle includes Popper.
  • Not every Bootstrap feature requires JavaScript.
  • Interactive Bootstrap components require Bootstrap JavaScript.
  • SRI can be used to verify external resources.
  • Keep Bootstrap CSS and JavaScript versions consistent.

🧠 Quick Quiz

Question: What does CDN stand for?