อันนี้ผมอิงจากของ Bootstrap ตัวล่าสุดนะครับ เลือกเอาแล้วแต่ถนัดว่าจะเอาจากเล็กไปใหญ่ หรือใหญ่ลงมาเล็กตามความถนัดได้เลย
ความต่างของการเขียนจากเล็กไปใหญ่คือ mobile first กับ large screen size first ครับ อันแรกคือจอเล็กมาก่อน อย่างหลังคือจอใหญ่มาก่อน ก็คือแบบแรกเช็คจาก Min-width แบบหลังเช็คจาก Max-width หรือจะเช็คเป็นช่วงๆก็ได้ เช็คจาก min-with นี้ ถึง max-width นี้ อะไรประมาณนี้
Mobile First(จอเล็กก่อน หรือมือถือนั่นเอง)
แบบแรกสำหรับเขียนใน Sass css
// Source mixins
// No media query necessary for xs breakpoint as it's effectively `@media (min-width: 0) { ... }`
@include media-breakpoint-up(sm) { ... }
@include media-breakpoint-up(md) { ... }
@include media-breakpoint-up(lg) { ... }
@include media-breakpoint-up(xl) { ... }
@include media-breakpoint-up(xxl) { ... }
// Usage
// Example: Hide starting at `min-width: 0`, and then show at the `sm` breakpoint
.custom-class {
display: none;
}
@include media-breakpoint-up(sm) {
.custom-class {
display: block;
}
}
ส่วนอันนี้สำหรับเขียนด้วย Css ธรรมดา
// X-Small devices (portrait phones, less than 576px)
// No media query for `xs` 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) { ... }
// X-Large devices (large desktops, 1200px and up)
@media (min-width: 1200px) { ... }
// XX-Large devices (larger desktops, 1400px and up)
@media (min-width: 1400px) { ... }
ต่อไปเป็นการเอาจอใหญ่ก่อน แล้วตามด้วยจอเล็ก(Large screen first)
แบบแรกสำหรับเขียนใน Sass css
// No media query necessary for xs breakpoint as it's effectively `@media (max-width: 0) { ... }`
@include media-breakpoint-down(sm) { ... }
@include media-breakpoint-down(md) { ... }
@include media-breakpoint-down(lg) { ... }
@include media-breakpoint-down(xl) { ... }
@include media-breakpoint-down(xxl) { ... }
// Example: Style from medium breakpoint and down
@include media-breakpoint-down(md) {
.custom-class {
display: block;
}
}
ส่วนอันนี้สำหรับเขียนด้วย Css ธรรมดา
// X-Small devices (portrait phones, less than 576px)
@media (max-width: 575.98px) { ... }
// Small devices (landscape phones, less than 768px)
@media (max-width: 767.98px) { ... }
// Medium devices (tablets, less than 992px)
@media (max-width: 991.98px) { ... }
// Large devices (desktops, less than 1200px)
@media (max-width: 1199.98px) { ... }
// X-Large devices (large desktops, less than 1400px)
@media (max-width: 1399.98px) { ... }
// XX-Large devices (larger desktops)
// No media query since the xxl breakpoint has no upper bound on its width
โน้ตนิดนึง ก่อนจะเขียนด้วย Sass css เราต้องประกาศตัวแปรสำหรับแต่ละขนาดหน้าจอก่อนนะครับ ด้านล่างนี้เลย
$grid-breakpoints: (
xs: 0,
sm: 576px,
md: 768px,
lg: 992px,
xl: 1200px,
xxl: 1400px
);
อีกแบบนึงก็คือเป็นช่วงสำหรับแต่ละขนาดหน้าจอ ตัวอย่างเช่นเริ่มจากเล็กสุด 768px จนถึง 991.98px(ตัวอย่างคือจอแท็บเล็ตแนวตั้ง)
โดยเขียนด้วย Sass css แบบด้านล่างนี้
@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) { ... }
@include media-breakpoint-only(xxl) { ... }
และถ้าใน Css ธรรมดามันจะเขียนแบบด้านล่างนี้ครับ
@media (min-width: 768px) and (max-width: 991.98px) { ... }
แบบสุดท้ายคือเป็นช่วงยาวๆเลยครับ
ยกตัวอย่างเช่น กว้างน้อยสุด 768px มากสุด 1199.98px ซึ่งก็คือครอบคลุมตั้งแต่แท็บเล็ตแนวตั้งยันแนวนอนเลยนั่นเอง
โดยเขียนด้วย Sass css แบบด้านล่างนี้
@include media-breakpoint-between(md, xl) { ... }
และถ้าใน Css ธรรมดามันจะเขียนแบบด้านล่างนี้ครับ
// Example
// Apply styles starting from medium devices and up to extra large devices
@media (min-width: 768px) and (max-width: 1199.98px) { ... }
ย้ำอีกรอบว่าข้อมูลอ้างอิงผมเอามาจาก Bootstrap 5 นะครับ ตัวเต็มดูได้ที่เว็บหลักเลย