📅  最后修改于: 2023-12-03 15:29:38.517000             🧑  作者: Mango
Bootstrap 4 is a popular front-end framework for developing responsive and mobile-first websites. It provides a set of ready-made UI components and styles that can help accelerate development.
CDN stands for Content Delivery Network, which is a network of servers distributed around the world that deliver web content to end-users faster by caching static assets such as CSS, JavaScript, and images.
Using Bootstrap 4 via a CDN can bring several benefits such as faster page load times, reduced server load, and improved user experience. In this guide, we will explain how to use Bootstrap 4 via CDN in your web projects.
To start using Bootstrap 4 via CDN, you need to add the following links to your HTML file:
<!-- CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-OgVRvuATP1z7JjHLkuOU3aWrkVwUrT2KbxZnTpExXy3gJqfgNQ/l8NaG4NpqE2MW" crossorigin="anonymous">
<!-- JavaScript -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.4.4/dist/umd/popper.min.js"
integrity="sha384-buG52r0/j7lN8Iqz1w133Hx2QAlbNOLX8WQKVYoLbxJODMXHSAMOdyLLG/LWZLoK" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
integrity="sha384-OgVRvuATP1z7JjHLkuOU3aWrkVwUrT2KbxZnTpExXy3gJqfgNQ/l8NaG4NpqE2MW" crossorigin="anonymous"></script>
The above code imports Bootstrap 4 CSS, jQuery, Popper.js, and Bootstrap 4 JavaScript. You can add these links to the head
section of your HTML file.
Once you have added the Bootstrap 4 links to your HTML file, you can start using its components and styles. Here is an example of how to create a responsive navbar using Bootstrap 4:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Brand</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
</ul>
</div>
</nav>
The above code creates a navbar using the nav
and navbar
classes. It includes a navbar-brand
element, a navbar-toggler
button, and a collapsible navbar-collapse
div that contains a list of navigation links. You can customize the navbar by adding more classes or properties.
Bootstrap 4 CDN is a convenient way to use the Bootstrap 4 framework without having to download and host the files yourself. It provides a fast and reliable way to add responsive UI components and styles to your web projects. By following the steps outlined in this guide, you can start using Bootstrap 4 via CDN in no time.