How to use Bootstrap CDN?

前端 未结 6 958
庸人自扰
庸人自扰 2020-12-23 10:04

I\'m trying to use a CDN on Bootstrap to increase performance to my website. However when referencing the CSS directly it works whereas using the CDN doesn\'t.

How

相关标签:
6条回答
  • 2020-12-23 10:28

    As others have mentioned, using a CDN is usually as easy as adding:

    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"
          rel="stylesheet">
    

    into your HTML. But this doesn't work when you are loading your html from a local file.

    The reason is the missing protocol. When using a CDN, it's usually a good idea not to specify the protocol, so that your browser will use either http or https depending on the protocol used to get your html in the first place.

    This is important because if your server is using https, it is better to have all references using https to avoid browsers (specially IExplorer) complaining about mixing content. On the other hand, using a protocol-less URL for CDN is more cache friendly (http://encosia.com/cripple-the-google-cdns-caching-with-a-single-character/).

    Unfortunately, a protocol-less URL is a bad idea if the protocol is file://. So if you are creating a private HTML that will be loaded from disk, then you should add the protocol and use the CDN like this:

    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"
          rel="stylesheet">
    

    Hope this will be userful to someone...

    0 讨论(0)
  • 2020-12-23 10:31

    Make your HTML look like this:

    <!DOCTYPE html>
    <html>
      <head>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
      </head>
      <body>
        <div class="navbar">
          <div class="navbar-inner">
            <a class="brand" href="#">Matt's Homepage</a>
            <ul class="nav">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="http://www.akkatracker.com">Blog</a></li>
              <li><a href="http://www.twitter.com">Twitter</a></li>
            </ul>
          </div>
        </div>
        <div class="btn"><a href="http://www.akkatracker.com">Click Here to Visit my Blog</a>  </div>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-23 10:34

    Hi Akkatracker I appreciate your question; it helped me. You can use a CDN for Bootstrap. Here is a simple complete html example where you do not have to download bootstrap since you are linking to a public content distributed network of the css & js files.

    Simple Bootstrap CDN HTML Example

    <html>
        <head>
            <title>Bootstrap CDN Simple Example</title>
            <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
        </head>
        <body>
            <h1> Bootstrap CDN Simple Complete HTML Example</h1>    
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
            <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        </body>
    </html>
    

    Please note JQuery needs to come before bootstrap.js. The order of our JavaScript libraries is significant. Hope this helps

    0 讨论(0)
  • 2020-12-23 10:42

    I am new to using Twitter Bootstrap as well as BootstrapCDN. I did some brief experimenting tonight and I made my site look correct, well almost, using the following 'Head'.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>My Project Site</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    
    <!--link rel="stylesheet/less" href="less/bootstrap.less" type="text/css" /-->
    <!--link rel="stylesheet/less" href="less/responsive.less" type="text/css" /-->
    <!--script src="js/less-1.3.3.min.js"></script--> 
    <!--append ‘#!watch’ to the browser URL, then refresh the page. -->
    
    
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-   
    combined.min.css" rel="stylesheet">
    <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js">    
    </script>
    
    
    <!-- my custom stylesheet -->
         <link href="/word/css/style.css" rel="stylesheet">
    
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <![endif]-->
    
    <!-- Fav and touch icons -->
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="img/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="img/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="img/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-57-precomposed.png">
    <link rel="shortcut icon" href="img/favicon.png">
    
    
    </head>
    

    So I have replaced the old style sheets with the ones provided at BootstrapCDN.com. I am not sure if it is proper to replace the existing stylesheets, but here goes to experimentation and my result is so so...

    This gets everything styled almost right; well maybe its styled totally right. I am still stepping through the DOM tools to see why my modals are non-functional. I suspect my issue with the modals is the JavaScript and the fact the site was just migrated from a local folder.

    0 讨论(0)
  • 2020-12-23 10:45

    follow this fiddle cdn usage http://jsfiddle.net/MgcDU

    css

    @import url('http://twitter.github.com/bootstrap/assets/css/bootstrap.css');
    .container {
       margin-top: 10px;
    }
    

    html

     <div class="container">
    <div class="hero-unit">
        <h1>Bootstrap jsFiddle Skeleton</h1>
        <p>Fork this fiddle to test your Bootstrap stuff.</p>
        <p>
            <a class="btn btn-primary btn-large" href="http://twitter.github.com/bootstrap/index.html" target="_blank">
                Learn more about Bootstrap
            </a>
        </p>
    </div>
    

    0 讨论(0)
  • 2020-12-23 10:54

    Using Microsoft Ajax Content Delivery Network as Bootstrap CDN:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Bootstrap Demo</title>
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.min.css">
            <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
            <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/bootstrap.min.js"></script>
        </head>
        <body>
            <div class="container">
                <h1>Bootstrap Demo</h1>
            </div>
        </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题