Bootstrap教程:學習構建第一個Bootstrap 4網站

快速教程,可幫助您快速掌握最新版本的Bootstrap。 (A quick tutorial to get you up to speed with the latest version of Bootstrap.)

In my opinion, the best way to learn a new technology is often to start building stuff from day one. This gives a sense of meaning to the learning process. Plus, it’s satisfying to see a product appear before you as you struggle your way through the material.

我認為,學習新技術的最好方法通常是從第一天開始就開始學習。 這給學習過程帶來了意義。 另外,當您在材料中苦苦掙扎時,很高興看到產品出現在您面前。

So in this article, I’ll walk you through building a simple website using Bootstrap 4.0 while highlighting the most important new features of the library.

因此,在本文中,我將引導您使用Bootstrap 4.0建立一個簡單的網站,同時重點介紹該庫的最重要的新功能。

If you want to learn Bootstrap 4.0 properly, check out this free course on Scrimba!

如果您想正確學習Bootstrap 4.0,請在Scrimba上查看此免費課程!

Now let’s get started.

現在開始吧。

我們將建立什么 (What we’ll build)

We’re going to build a basic portfolio website. Even though it’s simple, it contains several core concepts you’ll need to learn in order to use Bootstrap 4.0 properly.

我們將建立一個基本的投資組合網站。 即使很簡單,它也包含一些核心概念,您需要學習這些概念才能正確使用Bootstrap 4.0。

If you want to play around with the code, check out this Scrimba playground. Feel free to use it as a reference if you don’t understand something in the article and need to experiment for yourself.

如果您想使用該代碼,請訪問此Scrimba游樂場 。 如果您不了解本文中的內容并且需要自己進行實驗,請隨時將其用作參考。

導航欄 (The navbar)

Let’s start with the navbar. In Bootstrap 4.0 they’ve made navbars easier, as they require a bit less markup now. Here’s what we need to create the simplest navbar possible:

讓我們從導航欄開始。 在Bootstrap 4.0中,它們使導航欄更容易了,因為它們現在需要的標記要少一些。 這是我們創建盡可能簡單的導航欄所需的內容:

My portfolio

我的投資組合

Which results in the following:

結果如下:

The bg-light class makes the background light grey while the navbar-light class gives the text a dark colour. By default, the text colour in navbars is blue, however, I think it looks better with the navbar-light class.

bg-light類使背景navbar-light灰色,而navbar-light類使文本navbar-light深色。 默認情況下, navbar-light的文本顏色是藍色,但是,我認為使用navbar-light類看起來更好。

Let’s add some content to our navbar, at the same level as the brand anchor tag:

讓我們在導航欄上添加一些與品牌錨標簽相同的內容:

<ul class="navbar-nav">  <li class="navbar-item">  <a href="#" class="nav-link">Homepage</a>  </li>  <li class="navbar-item">  <a href="#" class="nav-link">Blog</a>  </li>  <li class="navbar-item">  <a href="#" class="nav-link">About</a>  </li>  <li class="navbar-item">  <a href="#" class="nav-link">Contact Us</a>  </li>  
</ul>

The three classes to take notice of here are the navbar-nav,navbar-link and navbar-item. Together they construct the navigation options the way you want them.

這里需要注意的三個類是navbar-navnavbar-linknavbar-item 。 它們一起以您想要的方式構造導航選項。

Here’s how that looks:

看起來是這樣的:

However, now we’ll need to make it responsive, as we want our navigation options to collapse into a hamburger icon on smaller screens. To achieve this, we need to do two things:

但是,現在我們需要使其具有響應性,因為我們希望導航選項在較小的屏幕上折疊成一個漢堡包圖標。 為此,我們需要做兩件事:

  1. Tell Bootstrap at which point the navigation options should break to collapse into a hamburger

    告訴Bootstrap,此時導航選項應該會破裂以折疊成漢堡包
  2. Create the markup for the hamburger

    為漢堡包創建標記

To make it collapse, we’ll add the navbar-expand-md class to the nav element itself:

為了使其折疊,我們將navbar-expand-md類添加到nav元素本身:

<nav class="navbar navbar-light bg-light `**navbar-expand-md**`">  
...  
</nav

This tells Bootstrap that we want the navbar options to toggle between expanded and collapsed states at the md breakpoint, which is at768px.

這告訴Bootstrap我們希望navbar選項在md斷點(即768px處在展開狀態和折疊狀態之間切換。

We also need to wrap our navigation options in a div (with the two classes collapse and navbar-collapse) which tells Bootstrap that this is the part we want to collapse.

我們還需要將導航選項包裝在div中(兩個類分別是collapsenavbar-collapse ),這將告訴Bootstrap這是我們要折疊的部分。

<div class="collapse navbar-collapse" id="navbarNav">  <ul class="navbar-nav">  ...   </ul>  
</div>

The id of navbarNav is to connect this item with the data-target attribute in the hamburger icon, which we’ll create like this:

navbarNav的ID是將此項與漢堡圖標中的data-target屬性相關聯,我們將如下創建:

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">  <span class="navbar-toggler-icon"></span>  
</button>
```html
We now have a great looking navbar, which collapses and expands at our chosen breakpoint:![](https://cdn-media-1.freecodecamp.org/images/1*1dn65y8seTpzTi1EV6EaVw.gif)### JumbotronThe next step is to create something that welcomes our users to the website below the navbar. To do that, we’ll use the [jumbotron](https://getbootstrap.com/docs/4.0/components/jumbotron/) component. It’s super simple:
```html
<div class="jumbotron jumbotron-fluid">  <div class="container">  <h1 class="display-3">Welcome to my website</h1>  <p class="lead">I'm a developer and designer. Check my portfolio below</p>  
</div>

Which results in the following:

結果如下:

The display-3 and lead classes are typography classes, which make the text a bit more opinionated and better looking in my view. You can read more about typography in Bootstrap 4.0 here.

display-3lead類是印刷類,在我看來,它們使文本更具自省感和外觀。 您可以在此處閱讀有關Bootstrap 4.0的排版的更多信息。

主要內容-網格和卡片 (The Main Content?—?Grid and Cards)

Below our jumbotron we’re going to add the main content of our website, which will consist of four cards. A card is a whole new component of Bootstrap 4.0, and it’s replacing panels, wells, and thumbnails from Bootstrap 3.0.

在巨無霸下面,我們將添加網站的主要內容,該內容包括四張卡片。 卡是Bootstrap 4.0的全新組件,它取代了Bootstrap 3.0中的面板,Kong和縮略圖。

Let’s first have a look at what we want to build:

首先讓我們看一下我們要構建的內容:

創建網格 (Creating the grid)

In order to make them appear nicely like this, and to also make sure they work well responsively, we’ll need to wrap the cards in a grid. The grid is one of the core pieces of Bootstrap, and many developers use the library solely because of the grid.

為了使它們看起來像這樣漂亮,并確保它們能很好地響應,我們需要將卡包裝成網格。 網格是Bootstrap的核心部分之一,許多開發人員僅由于網格而使用該庫。

We’ll start off by creating a very simple grid with no content. In Bootstrap, you always create rows first and then wrap columns inside the rows. By default, the grid can be divided into 12 columns in the width.

我們將從創建一個沒有內容的非常簡單的網格開始。 在引導,你總是創建行,然后換行內列。 默認情況下,網格的寬度可以分為12列。

Above the sm breakpoint, we want each of the cards to take up half the width, so we’ll give the columns a col-sm-6 class. When the screen reaches the lg breakpoint though, we want four cards in the width, so we’ll do col-lg-3.

sm斷點之上,我們希望每張卡占用一半的寬度,因此我們將為列提供col-sm-6類。 當屏幕到達lg斷點時,我們需要四張卡片的寬度,因此我們將執行col-lg-3

<div class="container">  <div class="row">  <div class="col-sm-6 col-lg-3">column</div>  <div class="col-sm-6 col-lg-3">column</div>  <div class="col-sm-6 col-lg-3">column</div>  <div class="col-sm-6 col-lg-3">column</div>  </div>  
</div>

This gives the following responsive layout:

這給出了以下響應式布局:

創建卡 (Creating the cards)

Now we simply need to replace the column text with a card component. Here’s the markup for our card:

現在,我們只需要用card組件替換列文本。 這是我們卡的標記:

<div class="card">  <img class="card-img-top" alt="Card header image" src="img1.png">  <div class="card-body">  <h5 class="card-title">Project 1</h5>  <p class="card-text">An awesome project</p>  <a href="#" class="btn btn-info">See project</a>  </div>  
</div>

To turn a div into a card we’ll simply add the class card. If we want an image to appear in the header of the card, we’ll add the card-img-top. As for the rest of the content, we’ll use the classes card-body, card-title , and card-text.

要將div變成卡片,我們只需添加類card 。 如果我們希望圖像出現在卡片的標題中,我們將添加card-img-top 。 至于其余的內容,我們將使用card-bodycard-titlecard-text

One problem, though, is that this layout won’t look good when the grid gets multiple rows. As you can see, we’ll need to add some spacing in-between the rows.

但是,一個問題是,當網格獲得多行時,這種布局看起來不會很好。 如您所見,我們需要在行之間添加一些間距。

This will introduce you to a new spacing concept in Bootstrap 4.0, where you can add classes to set the padding and margin. We’ll simply add the class mt-3 to the card divs.

這將為您介紹Bootstrap 4.0中的新間距概念,您可以在其中添加類來設置填充和邊距。 我們將簡單地將mt-3類添加到card div中。

<div class="card mt-3">  
...  
</div>

The mt stands for margin-top, and the 3 is a number on a scale from 1 to 5, where 5 is the most. You can also do for example pb-4, which would set thepadding-bootom to 4. You probably get the point by now. Once we’ve added this, we have a nice grid with cards on our website.

mt代表margin-top ,而3表示從1到5的數字,其中5表示最大。 例如,您也可以使用pb-4 ,它將padding-bootom設置為4。您現在可能已經明白了。 添加完之后,我們的網站上就會出現一個帶有卡片的漂亮網格。

聯系表 (Contact form)

Finally, let’s also add a contact form. It’ll simply be a new row in our grid. This time we’ll also use the offset class, as we don’t want it to be full-width, at least not above the md breakpoint.

最后,我們還要添加一個聯系表。 這將只是我們網格中的新行。 這次我們還將使用offset類,因為我們不希望它為全角,至少不超過md斷點。

So from md and upwards we’ll give it a width of six columns, and an offset of three:

因此,從md ,我們將為它提供六列的寬度,以及三列的偏移量:

<div class="row mt-5">  <div class="col-sm-12 **col-md-6 offset-md-3**">  <h3>Reach out!</h3>  _...form goes here..._  </div>  
</div>

Now let’s look at the code for the form itself:

現在讓我們看一下表單本身的代碼:

<form>  <div class="form-group">  <input type="text" class="form-control" id="email" placeholder="Your email..">  </div>  <div class="form-group">  <textarea class="form-control" placeholder="Your message..">              </textarea>  </div>  <button type="submit" class="btn btn-primary">Submit</button></form>

The controls?—?like the <input> and <textarea>—are styled with the form-control class. They make it look like a classical Boostrap form:

控件(如<input><textarea> )使用form-control類設置樣式。 它們使它看起來像經典的Boostrap形式:

And that’s it! You’ve now created your very first Bootstrap 4.0 website. If you want to learn the library properly, be sure to check out our free course on Scrimba.

就是這樣! 現在,您已經創建了第一個Bootstrap 4.0網站。 如果您想正確地學習圖書館,請務必查看有關Scrimba的免費課程。



Thanks for reading! My name is Per Borgen, I'm the co-founder of Scrimba – the easiest way to learn to code. You should check out our responsive web design bootcamp if want to learn to build modern website on a professional level.

謝謝閱讀! 我叫Per Borgen,我是Scrimba的共同創始人–學習編碼的最簡單方法。 如果要學習以專業水平構建現代網站,則應查看我們的響應式Web設計新手訓練營 。

翻譯自: https://www.freecodecamp.org/news/building-your-first-bootstrap-4-0-site-b54bbff6bc55/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394440.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394440.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394440.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用棧實現隊列 Implement Queue using Stacks

為什么80%的碼農都做不了架構師&#xff1f;>>> 問題&#xff1a; Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front…

Java利用POI生成Excel強制換行

前一段時間在做一個學校排課系統時&#xff0c;有一個地方需要利用把課程表生成excel匯出給客戶&#xff0c;由于之前用excel都只是簡單的應用&#xff0c;在單元格里都是用自動換行&#xff0c;而這次可能需要用到手動強制換行。 于是我在網上找了一下&#xff0c;網上找到的文…

550什么意思_研報翻譯官第二期:帶你了解什么是CPI

歡迎收看“第二期”研報翻譯官&#xff0c;臨近年末&#xff0c;各類金融研報接踵而至&#xff0c;我們也常會看到GDP、CPI、PPI這類字眼。過年回家跟親戚朋友嘮嗑的時候&#xff0c;如果不扯上幾句CPI或PPI&#xff0c;都顯自己得不夠專業。聽你們吹牛&#xff0c;我炒菜都有勁…

leetcode1314. 矩陣區域和(動態規劃)

給你一個 m * n 的矩陣 mat 和一個整數 K &#xff0c;請你返回一個矩陣 answer &#xff0c;其中每個 answer[i][j] 是所有滿足下述條件的元素 mat[r][c] 的和&#xff1a; i - K < r < i K, j - K < c < j K (r, c) 在矩陣內。 示例 1&#xff1a; 輸入&…

python讀取數據庫文件的擴展名_Python讀取sqlite數據庫文件的方法分析

本文實例講述了Python讀取sqlite數據庫文件的方法。分享給大家供大家參考&#xff0c;具體如下&#xff1a;這是Python內置的&#xff0c;不需要pip install 包數據庫里面有很多張表要操作數據庫首先要連接conect數據庫然后創建游標cursor來執行execute&#xff33;&#xff31…

C# 文件異步操作

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;//文件異步操作 namespace FileAsynchronousOperation {class Program{static void Main(string[] args){//實例化MyFile類MyFile myF…

軟考 中級職稱哪些最熱門_我如何利用有史以來最熱門的中級故事來建立排行榜。 以及它幾乎是怎么死的。...

軟考 中級職稱哪些最熱門by Michael Deng鄧小平 我如何利用有史以來最熱門的中級故事來建立排行榜。 以及它幾乎是怎么死的。 (How I built a leaderboard with the top Medium stories of all time. And how it almost died.) Last year I built Top Medium Stories — a web…

面試題一

1.html頁面由標簽組成&#xff0c;請寫出<head>中腳本定義標簽、下拉選擇框標簽  腳本定義標簽&#xff1a;<javascript></javascript>   下拉框選擇標簽&#xff1a;<select><option values""></option></select> 2…

leetcode712. 兩個字符串的最小ASCII刪除和(動態規劃)-Gogo

給定兩個字符串s1, s2&#xff0c;找到使兩個字符串相等所需刪除字符的ASCII值的最小和。 示例 1: 輸入: s1 “sea”, s2 “eat” 輸出: 231 解釋: 在 “sea” 中刪除 “s” 并將 “s” 的值(115)加入總和。 在 “eat” 中刪除 “t” 并將 116 加入總和。 結束時&#xff0…

python中封裝是什么意思_Python中數據封裝是什么?

封裝——“隱藏一切可以隱藏的實現細節&#xff0c;只向外界暴露(提供)簡單的編程接口”。在上節的 Student 類中&#xff0c;每個實例就擁有各自的 name 和 age 這些數據。我們可以通過函數來訪問這些數據&#xff0c;比如打印一個學生的年齡&#xff1a;>>> def pri…

jieba庫的使用

jieba庫的使用: jieba庫是一款優秀的 Python 第三方中文分詞庫&#xff0c;jieba 支持三種分詞模式&#xff1a;精確模式、全模式和搜索引擎模式&#xff0c;下面是三種模式的特點。 精確模式&#xff1a;試圖將語句最精確的切分&#xff0c;不存在冗余數據&#xff0c;適合做文…

Go語言實現HashSet

set.go // set project set.go package settype Set interface {Add(e interface{}) boolRemove(e interface{})Clear()Contains(e interface{}) boolLen() intSame(other Set) boolElements() []interface{}String() string }// 將集合other添加到集合one中 func AddSet(one S…

c#控件彈幕效果_C# Form 實現桌面彈幕

使用C# Form 簡單的實現了彈幕效果1.創建一個Form 設置2.添加一個計時器3. 代碼using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Drawing.Text;using System.Linq;using System.Text;using S…

Makefile中怎么使用Shell if判斷

/********************************************************************** Makefile中怎么使用Shell if判斷* 說明&#xff1a;* 譬如可能會在Makfile中需要判斷文件、文件夾的存在&#xff0c;使用shell語法* 輸出一些信息&#xff0c;等等。** …

我如何使用React和Typescript在freeCodeCamp中構建天氣應用

by Kelvin Mai通過凱文麥 我如何使用React和Typescript在freeCodeCamp中構建天氣應用 (How I built the weather app in freeCodeCamp using React and Typescript) So I finally decided to come back to freeCodeCamp and try to finish out my Front End Development Certi…

mysql結果集相減_MySQL_(Java)使用JDBC向數據庫發起查詢請求

課程相關鏈接&#xff1a;JDBC編程和MySQL數據庫課程源代碼在文章末尾~Java Database Connectivity簡單來說就是使用Java里面提供的一些類和方法&#xff0c;利用程序鏈接數據庫&#xff0c;進行增刪改查操作。這個過程就叫做JDBC編程接下來我們便分五步通過JDBC對MySQL中的數據…

在雙系統(Windows與Ubuntu)下刪除Ubuntu啟動項

問題概述&#xff1a;因為在自己學習Linux的時候&#xff0c;按照網上的教程錯誤的刪除了Ubuntu的一個內核驅動&#xff0c;導致Ubuntu不能啟動。我想到的辦法是重新安裝系統&#xff0c;重裝系統的第一步便是將Ubuntu從電腦中卸載。該筆記是有關如何刪除Ubuntu啟動項的。 使用…

iangularjs 模板_2018-web前端的自我介紹-優秀word范文 (5頁)

本文部分內容來自網絡整理&#xff0c;本司不為其真實性負責&#xff0c;如有異議或侵權請及時聯系&#xff0c;本司將立即刪除&#xff01;本文為word格式&#xff0c;下載后可方便編輯和修改&#xff01;web前端的自我介紹篇一&#xff1a;個人總結的web前端面試題1、自我介紹…

Teradata QueryGrid整合最佳分析技術 拓展客戶選擇空間

ZDNET至頂網CIO與應用頻道 05月11日 北京消息&#xff1a; 為持續幫助企業克服數據散布在不同分析系統的困難&#xff0c;全球領先的大數據分析和營銷應用服務供應商Teradata天睿公司宣布對Teradata QueryGrid 進行重要技術升級。此次升級新增并強化六項QueryGrid技術&#xf…

神舟筆記本bios_海爾雷神(藍天)神舟戰神游戲本風扇狂轉掉電大寫燈狂閃維修實例...

昨天收到一臺網友寄過來的海爾雷神游戲本。說到這個游戲本品牌&#xff0c;其實有幾個品牌的筆記本&#xff0c;它們的主板和模具是一模一樣的&#xff0c;也就是我們看到的品牌log不一樣而已。比如神舟的戰神 &#xff0c;機械師&#xff0c;機械革命&#xff0c;麥本本等等。…