之前有寫過一篇文章 使用sass mixin來開發responsive網站,利用 SASS 提供的 Mixin 來加速 Responsive 開發的方法,在 Boostrap 4 Beta 的版本中也被整合了相同的使用,而且功能上也相當的完善:


首先要先介紹一下 Boostrap Media queries 的基本佈局,如下:

CSS:

// Extra small devices (portrait phones, less than 576px)
// No media query since this is the default in Bootstrap

// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) { ... }

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) { ... }

// Large devices (desktops, 992px and up)
@media (min-width: 992px) { ... }

// Extra large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }

如上面代碼所示,分成五個部分 xssmmdlgxl,為主要的範圍


接著當我們在用 Sass 來編寫我們的樣式表時,則可以直接使用下面的 Mixin 來對不同的 media queries 以上的範圍進行操作:

Sass:

@include media-breakpoint-up(xs) { ... }
@include media-breakpoint-up(sm) { ... }
@include media-breakpoint-up(md) { ... }
@include media-breakpoint-up(lg) { ... }
@include media-breakpoint-up(xl) { ... }

// Example usage:
@include media-breakpoint-up(sm) {
  .some-class {
    display: block;
  }
}

Media queries 以下 的範圍:

CSS:

// Extra small devices (portrait phones, less than 576px)
@media (max-width: 575px) { ... }

// Small devices (landscape phones, less than 768px)
@media (max-width: 767px) { ... }

// Medium devices (tablets, less than 992px)
@media (max-width: 991px) { ... }

// Large devices (desktops, less than 1200px)
@media (max-width: 1199px) { ... }

// Extra large devices (large desktops)
// No media query since the extra-large breakpoint has no upper bound on its width

Sass:

@include media-breakpoint-down(xs) { ... }
@include media-breakpoint-down(sm) { ... }
@include media-breakpoint-down(md) { ... }
@include media-breakpoint-down(lg) { ... }

Media queries 當前 的範圍:

CSS:

// Extra small devices (portrait phones, less than 576px)
@media (max-width: 575px) { ... }

// Small devices (landscape phones, 576px and up)
@media (min-width: 576px) and (max-width: 767px) { ... }

// Medium devices (tablets, 768px and up)
@media (min-width: 768px) and (max-width: 991px) { ... }

// Large devices (desktops, 992px and up)
@media (min-width: 992px) and (max-width: 1199px) { ... }

// Extra large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }

Mixin 來對不同的 當前 media queries 的範圍進行操作:

Sass:

@include media-breakpoint-only(xs) { ... }
@include media-breakpoint-only(sm) { ... }
@include media-breakpoint-only(md) { ... }
@include media-breakpoint-only(lg) { ... }
@include media-breakpoint-only(xl) { ... }

兩個 Media queries 之間 的範圍:

CSS:

// Example
// Apply styles starting from medium devices and up to extra large devices
@media (min-width: 768px) and (max-width: 1199px) { ... }

Sass:

@include media-breakpoint-between(md, xl) { ... }

官方文件 : Responsive breakpoints

Summary
[CSS]Booststrap 4 breakpoint mixin
Article Name
[CSS]Booststrap 4 breakpoint mixin
Description
之前有寫過一篇文章 [使用sass mixin來開發responsive網站],利用 SASS 提供的 Mixin 來加速 Responsive 開發的方法,在 Boostrap 4 Beta 的版本中也被整合了相同的使用,而且功能上也相當的完善:
Author

Facebook 功能: