Bootstrap 4 บทช่วยสอน

BS4 HOME BS4 เริ่มต้น BS4 คอนเทนเนอร์ BS4 Grid Basic BS4 วิชาการพิมพ์ BS4 สี ตาราง BS4 BS4 รูปภาพ BS4 Jumbotron การแจ้งเตือน BS4 ปุ่ม BS4 กลุ่มปุ่ม BS4 ป้าย BS4 แถบความคืบหน้า BS4 BS4 Spinners BS4 การแบ่งหน้า BS4 รายการกลุ่ม การ์ด BS4 BS4 ดรอปดาวน์ BS4 ยุบ BS4 Navs BS4 Navbar แบบฟอร์ม BS4 อินพุต BS4 กลุ่มอินพุต BS4 BS4 แบบฟอร์มที่กำหนดเอง BS4 ม้าหมุน BS4 โมดอล เคล็ดลับเครื่องมือ BS4 BS4 ป๊อปโอเวอร์ BS4 ขนมปังปิ้ง BS4 Scrollspy BS4 ยูทิลิตี้ BS4 Flex ไอคอน BS4 วัตถุสื่อ BS4 ตัวกรอง BS4

Bootstrap 4 Grid

ระบบกริด BS4 BS4 ซ้อน/แนวนอน BS4 Grid XSmall BS4 กริดขนาดเล็ก BS4 กริดขนาดกลาง BS4 กริดขนาดใหญ่ BS4 Grid XLarge ตัวอย่างกริด BS4

Bootstrap 4 อื่นๆ

เทมเพลตพื้นฐาน BS4 แบบฝึกหัด BS4 แบบทดสอบ BS4

Bootstrap 4 อ้างอิง

ทุกชั้นเรียน JS Alert ปุ่ม JS JS Carousel JS ยุบ JS Dropdown JS Modal JS Popover JS Scrollspy JS แท็บ JS Toasts JS Tooltip


Bootstrap 4 ยูทิลิตี้


Bootstrap 4 ยูทิลิตี้

Bootstrap 4 มีคลาสยูทิลิตี้/ตัวช่วยมากมายในการจัดรูปแบบองค์ประกอบอย่างรวดเร็วโดยไม่ต้องใช้โค้ด CSS


พรมแดน

ใช้borderคลาสเพื่อเพิ่มหรือลบเส้นขอบออกจากองค์ประกอบ:

ตัวอย่าง

ตัวอย่าง

<span class="border"></span>
<span class="border border-0"></span>
<span class="border border-top-0"></span>
<span class="border border-right-0"></span>
<span class="border border-bottom-0"></span>
<span class="border border-left-0"></span>

เส้นขอบสี

เพิ่มสีให้กับเส้นขอบด้วยคลาสสีขอบตามบริบท:

ตัวอย่าง

ตัวอย่าง

<span class="border border-primary"></span>
<span class="border border-secondary"></span>
<span class="border border-success"></span>
<span class="border border-danger"></span>
<span class="border border-warning"></span>
<span class="border border-info"></span>
<span class="border border-light"></span>
<span class="border border-dark"></span>
<span class="border border-white"></span>

รัศมีชายแดน

เพิ่มมุมโค้งมนให้กับองค์ประกอบที่มีroundedคลาส:

ตัวอย่าง

ตัวอย่าง

<span class="rounded-sm"></span>
<span class="rounded"></span>
<span class="rounded-lg"></span>
<span class="rounded-top"></span>
<span class="rounded-right"></span>
<span class="rounded-bottom"></span>
<span class="rounded-left"></span>
<span class="rounded-circle"></span>
<span class="rounded-0"></span>


ลอยตัวและเคลียร์ฟิกซ์

ลอยองค์ประกอบไปทางขวาด้วย.float-rightคลาสหรือทางซ้ายด้วย.float-leftและล้าง float ด้วย .clearfixคลาส:

ตัวอย่าง

Float left Float right

ตัวอย่าง

<div class="clearfix">
  <span class="float-left">Float left</span>
  <span class="float-right">Float right</span>
</div>

ลอยตัวตอบสนอง

ลอยองค์ประกอบไปทางซ้ายหรือทางขวาขึ้นอยู่กับความกว้างของหน้าจอ ด้วยคลาส float ที่ตอบสนอง ( .float-*-left|right- โดยที่ * คือsm(>=576px), md(>=768px), lg(>=992px) หรือxl(>=1200px)):

ตัวอย่าง

Float right on small screens or wider

Float right on medium screens or wider

Float right on large screens or wider

Float right on extra large screens or wider

Float none

ตัวอย่าง

<div class="float-sm-right">Float right on small screens or wider</div><br>
<div class="float-md-right">Float right on medium screens or wider</div><br>
<div class="float-lg-right">Float right on large screens or wider</div><br>
<div class="float-xl-right">Float right on extra large screens or wider</div><br>
<div class="float-none">Float none</div>

จัดกึ่งกลาง

จัดองค์ประกอบให้อยู่ตรงกลางด้วย.mx-autoคลาส (เพิ่มระยะขอบซ้ายและระยะขอบขวา: อัตโนมัติ):

ตัวอย่าง

Centered

ตัวอย่าง

<div class="mx-auto bg-warning" style="width:150px">Centered</div>

ความกว้าง

กำหนดความกว้างขององค์ประกอบด้วยคลาส w-* ( .w-25, .w-50, .w-75, .w-100, .mw-100):

ตัวอย่าง

Width 25%
Width 50%
Width 75%
Width 100%
Max Width 100%

ตัวอย่าง

<div class="w-25 bg-warning">Width 25%</div>
<div class="w-50 bg-warning">Width 50%</div>
<div class="w-75 bg-warning">Width 75%</div>
<div class="w-100 bg-warning">Width 100%</div>
<div class="mw-100 bg-warning">Max Width 100%</div>

ส่วนสูง

กำหนดความสูงขององค์ประกอบด้วยคลาส h-* ( .h-25, .h-50, .h-75, .h-100, .mh-100):

ตัวอย่าง

Height 25%
Height 50%
Height 75%
Height 100%
Max Height 100%

ตัวอย่าง

<div style="height:200px;background-color:#ddd">
  <div class="h-25 bg-warning">Height 25%</div>
  <div class="h-50 bg-warning">Height 50%</div>
  <div class="h-75 bg-warning">Height 75%</div>
  <div class="h-100 bg-warning">Height 100%</div>
  <div class="mh-100 bg-warning" style="height:500px">Max Height 100%</div>
</div>

ระยะห่าง

Bootstrap 4 มีคลาสยูทิลิตี้ที่ตอบสนองและช่องว่างภายในที่หลากหลาย ใช้ได้กับเบรกพอยต์ทั้งหมด: xs(<=576px), sm(>=576px), md(>=768px), lg(>=992px) หรือxl(>=1200px)):

คลาสใช้ในรูปแบบ: {property}{sides}-{size}for xsและ{property}{sides}-{breakpoint}-{size}for sm, md, lg, และxl.

โดยที่ทรัพย์สินเป็นหนึ่งใน:

  • m- ชุดmargin
  • p- ชุดpadding

โดยที่ด้านเป็นหนึ่งใน:

  • t- ชุดmargin-topหรือpadding-top
  • b- ชุดmargin-bottomหรือpadding-bottom
  • l- ชุดmargin-leftหรือpadding-left
  • r- ชุดmargin-rightหรือpadding-right
  • x- ตั้งค่าทั้งpadding-leftและpadding-rightหรือmargin-leftและmargin-right
  • y- ตั้งค่าทั้งpadding-topและpadding-bottomหรือmargin-topและmargin-bottom
  • ว่าง - ตั้งค่า a marginหรือpaddingทั้ง 4 ด้านขององค์ประกอบ

โดยที่ขนาดเป็นหนึ่งใน:

  • 0- ชุดmarginหรือpaddingถึง0
  • 1- ตั้งค่าmarginหรือpaddingเป็น.25rem(4px หากขนาดตัวอักษรคือ 16px)
  • 2- ตั้งค่าmarginหรือpaddingเป็น.5rem(8px หากขนาดตัวอักษรคือ 16px)
  • 3- ตั้งค่าmarginหรือpaddingเป็น1rem(16px หากขนาดตัวอักษรคือ 16px)
  • 4- ตั้งค่าmarginหรือpaddingเป็น1.5rem(24px หากขนาดตัวอักษรคือ 16px)
  • 5- ตั้งค่าmarginหรือpaddingเป็น3rem(48px หากขนาดตัวอักษรคือ 16px)
  • auto- ตั้งค่าmarginเป็นอัตโนมัติ

หมายเหตุ:ระยะขอบสามารถเป็นค่าลบได้โดยการเพิ่ม "n" ข้างหน้าsize :

  • n1- ตั้งค่าmarginเป็น-.25rem(-4px หากขนาดตัวอักษรคือ 16px)
  • n2- ตั้งค่าmarginเป็น-.5rem(-8px ถ้า font-size คือ 16px)
  • n3- ตั้งค่าmarginเป็น-1rem(-16px ถ้าขนาดตัวอักษรคือ 16px)
  • n4- ตั้งค่าmarginเป็น-1.5rem(-24px หากขนาดตัวอักษรคือ 16px)
  • n5- ตั้งค่าmarginเป็น-3rem(-48px หากขนาดตัวอักษรคือ 16px)

ตัวอย่าง

I only have a top padding (1.5rem = 24px)
I have a padding on all sides (3rem = 48px)
I have a margin on all sides (3rem = 48px) and a bottom padding (3rem = 48px)

ตัวอย่าง

<div class="pt-4 bg-warning">I only have a top padding (1.5rem = 24px)</div>
<div class="p-5 bg-success">I have a padding on all sides (3rem = 48px)</div>
<div class="m-5 pb-5 bg-info">I have a margin on all sides (3rem = 48px) and a bottom padding (3rem = 48px)</div>

ตัวอย่างระยะห่างเพิ่มเติม

.m-# / m-*-# ขอบทุกด้าน
.mt-# / mt-*-# ขอบด้านบน
.mb-# / mb-*-# ขอบล่าง
.ml-# / ml-*-# ระยะขอบซ้าย
.mr-# / mr-*-# ขอบขวา
.mx-# / mx-*-# ระยะขอบซ้ายและขวา
.my-# / my-*-# ขอบบนและล่าง
.p-# / p-*-# บุนวมทุกด้าน
.pt-# / pt-*-# แผ่นรองซับ
.pb-# / pb-*-# แผ่นรองด้านล่าง
.pl-# / pl-*-# เบาะซ้าย
.pr-# / pr-*-# ช่องว่างภายในด้านขวา
.py-# / py-*-# บุนวมด้านบนและด้านล่าง
.px-# / px-*-# บุนวมซ้ายและขวา

เงา

ใช้shadow-คลาสเพื่อเพิ่มเงาให้กับองค์ประกอบ:

ตัวอย่าง

No shadow
Small shadow
Default shadow
Large shadow

ตัวอย่าง

<div class="shadow-none p-4 mb-4 bg-light">No shadow</div>
<div class="shadow-sm p-4 mb-4 bg-white">Small shadow</div>
<div class="shadow p-4 mb-4 bg-white">Default shadow</div>
<div class="shadow-lg p-4 mb-4 bg-white">Large shadow</div>

จัดแนวแนวตั้ง

ใช้align-คลาสเพื่อเปลี่ยนการจัดตำแหน่งขององค์ประกอบ (ใช้ได้เฉพาะในองค์ประกอบแบบอินไลน์ อินไลน์บล็อก อินไลน์ตาราง และเซลล์ตาราง):

ตัวอย่าง

baseline top middle bottom text-top text-bottom

ตัวอย่าง

<span class="align-baseline">baseline</span>
<span class="align-top">top</span>
<span class="align-middle">middle</span>
<span class="align-bottom">bottom</span>
<span class="align-text-top">text-top</span>
<span class="align-text-bottom">text-bottom</span>

การฝังที่ตอบสนอง

สร้างวิดีโอที่ตอบสนองหรือฝังสไลด์โชว์ตามความกว้างของพาเรนต์

เพิ่ม.embed-responsive-itemองค์ประกอบที่ฝัง (เช่น <iframe> หรือ <video>) ในองค์ประกอบหลักด้วย.embed-responsiveและอัตราส่วนภาพที่คุณเลือก:

ตัวอย่าง

ตัวอย่าง

<!-- 21:9 aspect ratio -->
<div class="embed-responsive embed-responsive-21by9">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>

<!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>

<!-- 4:3 aspect ratio -->
<div class="embed-responsive embed-responsive-4by3">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>

<!-- 1:1 aspect ratio -->
<div class="embed-responsive embed-responsive-1by1">
  <iframe class="embed-responsive-item" src="..."></iframe>
</div>

ทัศนวิสัย

ใช้ คลาส .visibleหรือ.invisibleเพื่อควบคุมการมองเห็นองค์ประกอบ หมายเหตุ:คลาสเหล่านี้จะไม่เปลี่ยนค่าที่แสดง CSS พวกเขาเพียงเพิ่มvisibility:visibleหรือvisibility:hidden:

ตัวอย่าง

I am visible

ตัวอย่าง

<div class="visible">I am visible</div>
<div class="invisible">I am invisible</div>

ตำแหน่ง

ใช้.fixed-topคลาสเพื่อทำให้องค์ประกอบคงที่/อยู่ที่ด้านบนสุดของหน้า:

ตัวอย่าง

<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
  ...
</nav>

Use the .fixed-bottom class to make any element fixed/stay at the bottom of the page:

Example

<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-bottom">
  ...
</nav>

Use the .sticky-top class to make any element fixed/stay at the top of the page when you scroll past it. Note: This class does not work in IE11 and earlier (will treat it as position:relative).

Example

<nav class="navbar navbar-expand-sm bg-dark navbar-dark sticky-top">
  ...
</nav>

Close icon

Use the .close class to style a close icon. This is often used for alerts and modals. Note that we use the &times; symbol to create the actual icon (a better looking "x"). Also note that it floats right by default:

Example

Example

<button type="button" class="close">&times;</button>

Screenreaders

Use the .sr-only class to hide an element on all devices, except screen readers:

Example

<span class="sr-only">I will be hidden on all screens except for screen readers.</span>

Colors

As described in the Colors chapter, here is a list of all text and background color classes:

The classes for text colors are: .text-muted, .text-primary, .text-success, .text-info, .text-warning, .text-danger, .text-secondary, .text-white, .text-dark, .text-body (default body color/often black) and .text-light:

Example

This text is muted.

This text is important.

This text indicates success.

This text represents some information.

This text represents a warning.

This text represents danger.

Secondary text.

Dark grey text.

Body text.

Light grey text.

Contextual text classes can also be used on links, which will add a darker hover color:

You can also add 50% opacity for black or white text with the .text-black-50 or .text-white-50 classes:

Example

Black text with 50% opacity on white background

White text with 50% opacity on black background


Background Colors

The classes for background colors are: .bg-primary, .bg-success, .bg-info, .bg-warning, .bg-danger, .bg-secondary, .bg-dark and .bg-light.

Note that background colors do not set the text color, so in some cases you'll want to use them together with a .text-* class.

Example

This text is important.

This text indicates success.

This text represents some information.

This text represents a warning.

This text represents danger.

Secondary background color.

Dark grey background color.

Light grey background color.


Typography/Text Classes

As described in the Typography chapter, here is a list of all typography/text classes:

Class Description Example
.display-* Display headings are used to stand out more than normal headings (larger font-size and lighter font-weight), and there are four classes to choose from: .display-1, .display-2, .display-3, .display-4
.font-weight-bold Bold text
.font-weight-bolder Bolder bold text
.font-weight-normal Normal text
.font-weight-light Light weight text
.font-weight-lighter Lighter weight text
.font-italic Italic text
.lead Makes a paragraph stand out
.small Indicates smaller text (set to 85% of the size of the parent)
.text-break Prevents long text from breaking layout
.text-center Indicates center-aligned text
.text-decoration-none Removes the underline from a link
.text-left Indicates left-aligned text
.text-justify Indicates justified text
.text-monospace Monospaced text
.text-nowrap Indicates no wrap text
.text-lowercase Indicates lowercased text
.text-reset Resets the color of a text or a link (inherits the color from its parent)
.text-right Indicates right-aligned text
.text-uppercase Indicates uppercased text
.text-capitalize Indicates capitalized text
.initialism Displays the text inside an <abbr> element in a slightly smaller font size
.list-unstyled Removes the default list-style and left margin on list items (works on both <ul> and <ol>). This class only applies to immediate children list items (to remove the default list-style from any nested lists, apply this class to any nested lists as well)
.list-inline Places all list items on a single line (used together with .list-inline-item on each <li> elements)
.pre-scrollable Makes a <pre> element scrollable

Block Elements

To make an element into a block element, add the .d-block class. Use any of the d-*-block classes to control WHEN the element should be a block element on a specific screen width:

Example

d-block d-sm-block d-md-block d-lg-block d-xl-block

Example

<span class="d-block bg-success">d-block</span>
<span class="d-sm-block bg-success">d-sm-block</span>
<span class="d-md-block bg-success">d-md-block</span>
<span class="d-lg-block bg-success">d-lg-block</span>
<span class="d-xl-block bg-success">d-xl-block</span>

Other Display Classes

Other display classes are also available:

Class Description Example
.d-none Hides an element
.d-*-none Hides an element on a specific screen size
.d-inline Makes an element inline
.d-*-inline Makes an element inline on a specific screen size
.d-inline-block Makes an element inline block
.d-*-inline-block Makes an element inline block on a specific screen size
.d-table Makes an element display as a table
.d-*-table Makes an element display as a table on a specific screen size
.d-table-cell Makes an element display as a table cell
.d-*-table-cell Makes an element display as a table cell on a specific screen size
.d-table-row Makes an element display as a table row
.d-*-table-row Makes an element display as a table row on a specific screen size
.d-flex Creates a flexbox container and transforms direct children into flex items
.d-*-flex Creates a flexbox container on a specific screen size
.d-inline-flex Creates an inline flexbox container
.d-*-inline-flex Creates an inline flexbox container on a specific screen size

Flex

Use .flex-* classes to control the layout with flexbox.

Read more about Bootstrap 4 Flex, in our next chapter.

Example

Horizontal:

Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3

Vertical:

Flex item 1
Flex item 2
Flex item 3
Flex item 1
Flex item 2
Flex item 3