1679053020
ทุกสิ่งที่คุณต้องรู้เพื่อใช้ CSS Grid อย่างมืออาชีพ เครื่องมือ CSS Grid ทั้งหมดที่คุณต้องการเพื่อสร้างเลย์เอาต์เว็บไซต์ขั้นพื้นฐานและขั้นสูงในรูปแบบที่ตอบสนองซึ่งดูดีในทุกอุปกรณ์ CSS Grid Handbook - คู่มือฉบับสมบูรณ์เกี่ยวกับ Grid Containers และ Grid Item
CSS Grid มอบเครื่องมือในการสร้างเลย์เอาต์เว็บไซต์ขั้นพื้นฐานและขั้นสูงในรูปแบบที่ตอบสนองซึ่งดูดีบนอุปกรณ์มือถือ แท็บเล็ต และเดสก์ท็อป
บทช่วยสอนนี้กล่าวถึงทุกสิ่งที่คุณจำเป็นต้องรู้เพื่อใช้ CSS Grid อย่างมืออาชีพ
ดังนั้นโดยไม่ต้องกังวลใจอีกต่อไป มาทำความเข้าใจว่า CSS Grid คืออะไร
CSS Grid Layout Module ทำให้เบราว์เซอร์แสดงองค์ประกอบ HTML ที่เลือกเป็นโมเดลกล่องตาราง
ตารางช่วยให้คุณปรับขนาดและเปลี่ยนตำแหน่งคอนเทนเนอร์กริดและรายการในสองมิติได้อย่างง่ายดาย
บันทึก:
คอนเทนเนอร์กริดเป็นองค์ประกอบ HTMLที่มีdisplayค่าของคุณสมบัติเป็นgridหรือinline-grid
รายการกริดคือรายการลูกโดยตรงของคอนเทนเนอร์กริด
คอนเทนเนอร์กริด (พื้นที่สีเหลืองขนาดใหญ่ในรูปภาพ) เป็นองค์ประกอบ HTML ที่มีค่าของคุณสมบัติการแสดงเป็นกริดหรืออินไลน์กริด รายการกริด (กล่องเล็กภายในคอนเทนเนอร์สีเหลือง) เป็นรายการย่อยโดยตรงของคอนเทนเนอร์กริด
gridบอกให้เบราว์เซอร์แสดงองค์ประกอบ HTML ที่เลือกเป็นโมเดลกริดบ็อกซ์ระดับบล็อก
displayกล่าวอีกนัยหนึ่งคือ การตั้ง ค่าคุณสมบัติขององค์ประกอบ เพื่อ gridเปลี่ยนโมเดลกล่องให้เป็นโมดูลโครงร่างกริดระดับบล็อก
นี่คือตัวอย่าง:
section {
display: grid;
background-color: orange;
margin: 10px;
padding: 7px;
}
ข้อมูลโค้ดด้านบนใช้gridค่าเพื่อแปลงองค์ประกอบของเอกสาร HTML <section>จาก<section>โหนดปกติเป็นโมเดลกล่องกริดระดับบล็อก
บันทึก:
ตอนนี้เรามาพูดถึงinline-gridคุณค่า
inline-gridบอกให้เบราว์เซอร์แสดงองค์ประกอบ HTML ที่เลือกในรูปแบบกล่องกริดระดับอินไลน์
displayกล่าวอีกนัยหนึ่งคือ การตั้ง ค่าคุณสมบัติขององค์ประกอบ เพื่อ inline-gridเปลี่ยนโมเดลกล่องให้เป็นโมดูลเค้าโครงกริดระดับอินไลน์
นี่คือตัวอย่าง:
section {
display: inline-grid;
background-color: orange;
margin: 10px;
padding: 7px;
}
ข้อมูลโค้ดด้านบนใช้inline-gridค่าเพื่อแปลงองค์ประกอบของเอกสาร HTML <section>จาก<section>โหนดปกติเป็นโมเดลกล่องกริดระดับอินไลน์
บันทึก:
ในการแปลงองค์ประกอบ HTML ปกติเป็นโมเดลกล่องกริด (หรืออินไลน์กริด) โมดูลโครงร่างกริดจะจัดเตรียมคุณสมบัติสองประเภทสำหรับการวางตำแหน่งกล่องกริดและชายน์โดยตรง:
A grid container's properties specify how browsers should layout items within the grid box model.
Note: We define a grid container's property on the container, not its items.
The eight (8) types of grid container properties are:
Let's discuss the eight types now.
grid-template-columns specifies the number and widths of columns browsers should display in the selected grid container.
section {
display: grid;
grid-template-columns: 95px 1fr;
background-color: orange;
margin: 10px;
padding: 7px;
}
The snippet above used the grid-template-columns property to display two columns of different widths in the selected <section> grid container.
Note: We used the fr (fraction) unit to scale the second column relative to the fraction of available space in the grid container.
section {
display: grid;
grid-template-columns: 15% 60% 25%;
background-color: orange;
margin: 10px;
padding: 7px;
}
The snippet above used the grid-template-columns property to display three columns of different widths in the selected <section> grid container.
Note:
Tip:
grid-template-rows specifies the number and heights of rows browsers should display in the selected grid container.
section {
display: grid;
grid-template-rows: 95px 1fr 70px;
background-color: orange;
margin: 10px;
padding: 7px;
}
The snippet above used the grid-template-rows property to display three rows of different heights in the selected <section> grid container.
หมายเหตุ:เราใช้frหน่วย (เศษส่วน)เพื่อปรับขนาดแถวที่สองเทียบกับเศษส่วนของพื้นที่ว่างในคอนเทนเนอร์กริด
section {
display: grid;
grid-template-rows: 90px 300px 1fr;
grid-template-columns: auto auto auto auto;
background-color: orange;
margin: 10px;
padding: 7px;
}
ตัวอย่างด้านบนใช้grid-template-rowsคุณสมบัติเพื่อแสดงสามคอลัมน์ที่มีความสูงต่างกันใน<section>คอนเทนเนอร์กริด ที่เลือก
บันทึก:
เคล็ดลับ:
ปรับเนื้อหาระบุว่าเบราว์เซอร์ควรวางตำแหน่งคอลัมน์ของคอนเทนเนอร์กริดตามแกนแถวอย่างไร
บันทึก:
คุณสมบัติjustify-contentยอมรับค่าต่อไปนี้:
เรามาพูดถึงค่าเหล่านี้กัน
startวางตำแหน่งคอลัมน์ของคอนเทนเนอร์กริดด้วยขอบเริ่มต้นของแถว
ปรับตำแหน่งค่าเริ่มต้นของเนื้อหาของคอลัมน์ไปยังขอบเริ่มต้นแถวของคอนเทนเนอร์กริด
นี่คือตัวอย่าง:
section {
display: grid;
justify-content: start;
grid-template-columns: repeat(4, 40px);
background-color: orange;
margin: 10px;
}
ข้อมูลโค้ดด้านบนใช้startค่าเพื่อวางตำแหน่ง<section>คอลัมน์ของขอบแถวเริ่มต้นของคอนเทนเนอร์กริด
centerวางตำแหน่งคอลัมน์ของคอนเทนเนอร์กริดให้อยู่กึ่งกลางของแกนแถวของกริด
ปรับตำแหน่งค่ากึ่งกลางของเนื้อหาให้อยู่ในตำแหน่งกึ่งกลางของคอนเทนเนอร์กริด
นี่คือตัวอย่าง:
section {
display: grid;
justify-content: center;
grid-template-columns: repeat(4, 40px);
background-color: orange;
margin: 10px;
}
ข้อมูลโค้ดด้านบนใช้centerค่าเพื่อวางตำแหน่ง<section>คอลัมน์ของ "ให้อยู่กึ่งกลางของคอนเทนเนอร์กริด"
endวางตำแหน่งคอลัมน์ของคอนเทนเนอร์กริดด้วยขอบท้ายแถว
ปรับตำแหน่งค่าสิ้นสุดของเนื้อหาของคอลัมน์ไปยังขอบท้ายแถวของคอนเทนเนอร์กริด
นี่คือตัวอย่าง:
section {
display: grid;
justify-content: end;
grid-template-columns: repeat(4, 40px);
background-color: orange;
margin: 10px;
}
ข้อมูลโค้ดด้านบนใช้endค่าเพื่อวางตำแหน่ง<section>คอลัมน์ของขอบแถวท้ายแถวของคอนเทนเนอร์กริด
space-betweenทำสิ่งต่อไปนี้:
justify-content's space-between value creates even spacing between each pair of columns between the first and last grid column
Here's an example:
section {
display: grid;
justify-content: space-between;
grid-template-columns: repeat(4, 40px);
background-color: orange;
margin: 10px;
}
The snippet above used the space-between value to create even spacing between each pair of columns between the first and last grid column.
space-around assigns equal spacing to each side of a grid container's columns.
Therefore, the space before the first column and after the last one is half the width of the space between each pair of columns.
justify-content's space-around value assigns equal spacing to each side of the grid container's columns
Here's an example:
section {
display: grid;
justify-content: space-around;
grid-template-columns: repeat(4, 40px);
background-color: orange;
margin: 10px;
}
The snippet above used the space-around value to assign equal spacing to each side of the grid container's columns.
space-evenly assigns even spacing to both ends of a grid container and between its columns.
justify-content's space-evenly value assigns even spacing to both ends of the grid container and between its columns
Here's an example:
section {
display: grid;
justify-content: space-evenly;
grid-template-columns: repeat(4, 40px);
background-color: orange;
margin: 10px;
}
We used the space-evenly value to assign even spacing to both ends of the grid container and between its columns.
justify-items specifies the default justify-self value for all the grid items.
The justify-items property accepts the following values:
Let's discuss the four values.
stretch is justify-items' default value. It stretches the grid container's items to fill their individual cells' row (inline) axis.
justify-items' stretch value stretches grid items to fill their individual cells' row axis
Here's an example:
section {
display: grid;
justify-items: stretch;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
}
The snippet above used the stretch value to stretch the grid items to fill their individual cells' row axis.
start positions a grid container's items with the row-start edge of their individual cells' row axis.
justify-items' start value positions grid items to their individual cells' row-start edge
Here's an example:
section {
display: grid;
justify-items: start;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
}
The snippet above used the start value to position the grid items to their individual cells' row-start edge.
center positions a grid container's items to the center of their individual cells' row axis.
justify-items' center value positions grid items to their individual cells' center
Here's an example:
section {
display: grid;
justify-items: center;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
}
ข้อมูลโค้ดด้านบนใช้centerค่าเพื่อวางตำแหน่งรายการกริดให้อยู่กึ่งกลางของแกนแถวของแต่ละเซลล์
endวางตำแหน่งรายการของคอนเทนเนอร์กริดด้วยขอบท้ายแถวของแกนแถวของแต่ละเซลล์
ค่าสิ้นสุดของรายการที่จัดชิดขอบจะจัดตำแหน่งรายการกริดไปยังขอบท้ายแถวของแต่ละเซลล์
นี่คือตัวอย่าง:
section {
display: grid;
justify-items: end;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
}
ข้อมูลโค้ดด้านบนใช้endค่าเพื่อวางตำแหน่งรายการกริดไปยังขอบท้ายแถวของเซลล์แต่ละเซลล์
align-contentระบุว่าเบราว์เซอร์ควรจัดแนวแถวของคอนเทนเนอร์กริดตามแกนคอลัมน์ของคอนเทนเนอร์อย่างไร
บันทึก:
คุณสมบัติalign-contentยอมรับค่าต่อไปนี้:
เรามาพูดถึงค่าเหล่านี้กัน
startจัดแนวแถวของคอนเทนเนอร์กริดให้ตรงกับขอบเริ่มต้นของคอลัมน์ของแกนคอลัมน์ของกริด
ค่าเริ่มต้นของการจัดแนวเนื้อหาจะจัดแนวแถวให้ชิดขอบเริ่มต้นคอลัมน์ของคอนเทนเนอร์กริด
นี่คือตัวอย่าง:
section {
display: grid;
align-content: start;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 300px;
}
ข้อมูลโค้ดด้านบนใช้startค่าเพื่อจัด<section>แถวของแถวให้ตรงกับขอบเริ่มต้นคอลัมน์ของคอนเทนเนอร์กริด
centerจัดแนวแถวของคอนเทนเนอร์กริดให้อยู่กึ่งกลางของแกนคอลัมน์ของกริด
align-content's center value aligns rows to the center of the grid container
section {
display: grid;
align-content: center;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 300px;
}
The snippet above used the center value to align the <section>'s rows to the center of the grid container.
end aligns a grid container's rows with the column-end edge of the grid's column axis.
align-content's end value aligns rows to the grid container's column-end edge
Here's an example:
section {
display: grid;
align-content: end;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 300px;
}
The snippet above used the end value to align the <section>'s rows to the grid container's column-end edge.
space-between does the following:
การจัดตำแหน่งเนื้อหาช่องว่างระหว่างค่าสร้างระยะห่างระหว่างแถวแต่ละคู่ระหว่างแถวกริดแรกและแถวสุดท้าย
นี่คือตัวอย่าง:
section {
display: grid;
align-content: space-between;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 300px;
}
ข้อมูลโค้ดด้านบนใช้space-betweenค่าเพื่อสร้างระยะห่างระหว่างแถวแต่ละคู่ระหว่างแถวกริดแรกและแถวสุดท้าย
space-aroundกำหนดระยะห่างเท่ากันให้กับแต่ละด้านของแถวของคอนเทนเนอร์กริด
ดังนั้นช่องว่างก่อนแถวแรกและหลังแถวสุดท้ายคือครึ่งหนึ่งของความกว้างของช่องว่างระหว่างแถวแต่ละคู่
ค่าช่องว่างรอบ ๆ ของเนื้อหาจะกำหนดระยะห่างที่เท่ากันให้กับแต่ละด้านของแถวของคอนเทนเนอร์กริด
นี่คือตัวอย่าง:
section {
display: grid;
align-content: space-around;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 300px;
}
ข้อมูลโค้ดด้านบนใช้space-aroundค่าเพื่อกำหนดระยะห่างที่เท่ากันให้กับแต่ละด้านของแถวของคอนเทนเนอร์กริด
space-evenlyกำหนดระยะห่างเท่ากันให้กับปลายทั้งสองของคอนเทนเนอร์กริดและระหว่างแถว
ค่าช่องว่างเท่า ๆ กันของเนื้อหาจะกำหนดระยะห่างที่เท่ากันให้กับปลายทั้งสองของคอนเทนเนอร์กริดและระหว่างแถว
นี่คือตัวอย่าง:
section {
display: grid;
align-content: space-evenly;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 300px;
}
เราใช้space-evenlyค่านี้เพื่อกำหนดระยะห่างเท่ากันให้กับปลายทั้งสองด้านของคอนเทนเนอร์กริดและระหว่างแถว
จัดรายการระบุalign-selfค่าเริ่มต้นสำหรับรายการกริดทั้งหมด
คุณสมบัติalign-itemsยอมรับค่าต่อไปนี้:
เรามาพูดถึงค่าสี่ค่าด้านล่างกัน
stretchเป็นค่าเริ่มต้นalign-itemsสำหรับ มันยืดรายการของคอนเทนเนอร์กริดเพื่อเติมแกนคอลัมน์ (บล็อก) ของแต่ละเซลล์
ค่าการยืดของรายการจัดแนวจะยืดรายการกริดเพื่อเติมแกนคอลัมน์ของแต่ละเซลล์
นี่คือตัวอย่าง:
section {
display: grid;
align-items: stretch;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 400px;
}
ตัวอย่างด้านบนใช้stretchค่าเพื่อยืดรายการกริดเพื่อเติมแกนคอลัมน์ของแต่ละเซลล์
startจัดแนวรายการของคอนเทนเนอร์กริดให้ตรงกับขอบเริ่มต้นของคอลัมน์ของแกนคอลัมน์ของแต่ละเซลล์
ค่าเริ่มต้นของ align-ites จะจัดแนวรายการกริดให้ตรงกับขอบเริ่มต้นของคอลัมน์ของแต่ละเซลล์
นี่คือตัวอย่าง:
section {
display: grid;
align-items: start;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 400px;
}
ข้อมูลโค้ดด้านบนใช้startค่าเพื่อจัดแนวรายการกริดให้ตรงกับขอบเริ่มต้นของคอลัมน์ของแต่ละเซลล์
centerจัดแนวรายการของคอนเทนเนอร์กริดให้อยู่กึ่งกลางของแกนคอลัมน์ของแต่ละเซลล์
ค่ากึ่งกลางของรายการที่จัดตำแหน่งจะจัดรายการกริดให้อยู่กึ่งกลางของเซลล์แต่ละเซลล์
นี่คือตัวอย่าง:
section {
display: grid;
align-items: center;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 400px;
}
ข้อมูลโค้ดด้านบนใช้centerค่าเพื่อจัดแนวรายการกริดให้อยู่กึ่งกลางของแกนคอลัมน์ของแต่ละเซลล์
endจัดแนวรายการของคอนเทนเนอร์กริดให้ตรงกับขอบท้ายคอลัมน์ของแกนคอลัมน์ของแต่ละเซลล์
ค่าสิ้นสุดของรายการที่จัดตำแหน่งจะจัดรายการกริดให้ตรงกับขอบท้ายคอลัมน์ของแต่ละเซลล์
นี่คือตัวอย่าง:
section {
display: grid;
align-items: end;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin: 10px;
height: 400px;
}
ข้อมูลโค้ดด้านบนใช้endค่าเพื่อจัดแนวรายการกริดให้ตรงกับขอบท้ายคอลัมน์ของแต่ละเซลล์
ตอนนี้เราทราบประเภทของคุณสมบัติของคอนเทนเนอร์กริด CSS แล้ว เราสามารถพูดคุยเกี่ยวกับคุณสมบัติของรายการกริดได้
คุณสมบัติของรายการกริดจะระบุวิธีที่เบราว์เซอร์ควรจัดวางรายการที่ระบุภายในโมเดลกริดบ็อกซ์
Note: We define a grid item's property on the item, not its container.
The ten (10) types of grid item properties are:
Let's discuss the ten types now.
justify-self specifies how browsers should position the selected grid item along its cell's row (inline) axis.
The justify-self property accepts the following values:
Let's discuss the four values.
stretch is justify-self's default value. It stretches the selected grid item to fill its cell's row (inline) axis.
justify-self's stretch value stretches the selected grid item to fill its cell's row axis
Here's an example:
.grid-item1 {
justify-self: stretch;
}
ตัวอย่างด้านบนใช้stretchค่าเพื่อยืดgrid-item1เพื่อเติมแกนแถวของเซลล์
startวางตำแหน่งรายการกริดที่เลือกด้วยขอบเริ่มต้นแถวของแกนแถวของเซลล์
ค่าเริ่มต้นของ justify-self จะวางตำแหน่งรายการกริดที่เลือกไว้ที่ขอบเริ่มต้นแถวของเซลล์
นี่คือตัวอย่าง:
.grid-item1 {
justify-self: start;
}
ข้อมูลโค้ดด้านบนใช้startค่าเพื่อวางตำแหน่งgrid-item1ที่ขอบเริ่มต้นแถวของเซลล์
centerวางตำแหน่งรายการกริดที่เลือกไว้ตรงกลางแกนแถวของเซลล์
ค่ากึ่งกลางของ justify-self จะวางตำแหน่งรายการกริดที่เลือกไว้ที่กึ่งกลางเซลล์
นี่คือตัวอย่าง:
.grid-item1 {
justify-self: center;
}
ตัวอย่างด้านบนใช้centerค่าเพื่อจัดตำแหน่งgrid-item1ให้อยู่กึ่งกลางเซลล์
endวางตำแหน่งรายการกริดที่เลือกด้วยขอบท้ายแถวของแกนแถวของเซลล์
ค่าสิ้นสุดของ justify-self จะวางตำแหน่งรายการกริดที่เลือกไว้ที่ขอบท้ายแถวของเซลล์
นี่คือตัวอย่าง:
.grid-item1 {
justify-self: end;
}
ตัวอย่างด้านบนใช้endค่าเพื่อวางตำแหน่งgrid-item1ที่ขอบท้ายแถวของเซลล์
align-selfระบุว่าเบราว์เซอร์ควรจัดตำแหน่งรายการกริดที่เลือกตามแกนคอลัมน์ (บล็อก) ของเซลล์อย่างไร
คุณสมบัติalign-selfยอมรับค่าต่อไปนี้:
เรามาพูดถึงค่าสี่ค่าด้านล่างกัน
stretchเป็นalign-selfค่าเริ่มต้นของ ขยายรายการกริดที่เลือกให้เต็มแกนคอลัมน์ (บล็อก) ของเซลล์
ค่ายืดของ align-self จะยืดรายการกริดที่เลือกเพื่อเติมแกนคอลัมน์ของเซลล์
นี่คือตัวอย่าง:
.grid-item1 {
align-self: stretch;
}
ตัวอย่างด้านบนใช้stretchค่าที่จะยืดgrid-item1เพื่อเติมแกนคอลัมน์ของเซลล์
startจัดแนวรายการกริดที่เลือกให้ตรงกับขอบเริ่มต้นของคอลัมน์ของแกนคอลัมน์ของเซลล์
ค่าเริ่มต้นของ align-self จะจัดแนวรายการกริดที่เลือกให้ตรงกับขอบเริ่มต้นของคอลัมน์ของเซลล์
นี่คือตัวอย่าง:
.grid-item1 {
align-self: start;
}
ข้อมูลโค้ดด้านบนใช้startค่าเพื่อจัดแนวให้ชิดgrid-item1กับขอบเริ่มต้นของคอลัมน์ของเซลล์
centerจัดแนวรายการกริดที่เลือกให้อยู่กึ่งกลางของแกนคอลัมน์ของเซลล์
ค่ากึ่งกลางของ align-self จะจัดรายการกริดที่เลือกให้อยู่กึ่งกลางของเซลล์
นี่คือตัวอย่าง:
.grid-item1 {
align-self: center;
}
ตัวอย่างด้านบนใช้centerค่าเพื่อจัดตำแหน่งgrid-item1ให้อยู่กึ่งกลางเซลล์
endจัดรายการกริดที่เลือกให้ตรงกับขอบท้ายคอลัมน์ของแกนคอลัมน์ของเซลล์
ค่าสิ้นสุดของ align-self จะจัดรายการกริดที่เลือกให้ตรงกับขอบปลายคอลัมน์ของเซลล์
นี่คือตัวอย่าง:
.grid-item1 {
align-self: end;
}
ข้อมูลโค้ดด้านบนใช้endค่าเพื่อจัดแนว grid-item1 ให้ตรงกับขอบท้ายคอลัมน์ของเซลล์
grid-column-startระบุว่ารายการกริดที่เลือกควรเริ่มต้น (หรือขยาย) ตามแกนแถว (อินไลน์) ของคอนเทนเนอร์กริด
คุณสมบัติgrid-column-startยอมรับค่าต่อไปนี้:
.grid-item1 {
grid-column-start: auto;
}
ข้อมูลโค้ดด้านบนใช้autoค่าเพื่อเริ่มอัตโนมัติgrid-item1ตามโฟลว์เค้าโครงคอลัมน์ปกติ
.grid-item1 {
grid-column-start: 3;
}
The snippet above used the grid-column-start property to start grid-item1 at column line 3.
.grid-item1 {
grid-column-start: span 2;
}
The snippet above used the span 2 value to span grid-item1 across two columns.
grid-column-end specifies where the selected grid item should end (or span) along the grid container's row (inline) axis.
The grid-column-end property accepts the following values:
.grid-item1 {
grid-column-end: auto;
}
The snippet above used the auto value to auto-end grid-item1 according to the normal layout flow.
.grid-item1 {
grid-column-start: 1;
grid-column-end: 3;
}
ตัวอย่างด้านบนใช้grid-column-endพร็อพเพอร์ตี้เพื่อสิ้นสุดgrid-item1ที่คอลัมน์บรรทัดที่ 3
.grid-item1 {
grid-column-start: 2;
grid-column-end: span 2;
}
ตัวอย่างด้านบนใช้span 2ค่าเพื่อขยายgrid-item1ระหว่างสองคอลัมน์
ตารางคอลัมน์เป็นชวเลขสำหรับคุณสมบัติgrid-column-startและgrid-column-end
กล่าวอีกนัยหนึ่งแทนที่จะเขียนว่า:
.grid-item1 {
grid-column-start: 1;
grid-column-end: 3;
}
คุณสามารถใช้grid-columnคุณสมบัติเพื่อย่อรหัสของคุณดังนี้:
.grid-item1 {
grid-column: 1 / 3;
}
นี่คือไวยากรณ์ของคอลัมน์กริด:
grid-column: grid-column-start / grid-column-end;
grid-row-startระบุว่ารายการกริดที่เลือกควรเริ่มต้น (หรือขยาย) ตามแกนคอลัมน์ (บล็อก) ของคอนเทนเนอร์กริด
คุณสมบัติgrid-row-startยอมรับค่าต่อไปนี้:
.grid-item1 {
grid-row-start: auto;
}
ข้อมูลโค้ดด้านบนใช้autoค่าเพื่อเริ่มอัตโนมัติgrid-item1ตามโฟลว์เค้าโครงแถวปกติ
.grid-item1 {
grid-row-start: 3;
}
ตัวอย่างด้านบนใช้grid-row-startพร็อพเพอร์ตี้เพื่อเริ่มต้นgrid-item1ที่บรรทัดที่ 3
.grid-item1 {
grid-row-start: span 2;
}
ตัวอย่างด้านบนใช้span 2ค่าเพื่อครอบคลุมgrid-item1สองแถว
จุดสิ้นสุดของแถวกริดระบุว่ารายการกริดที่เลือกควรสิ้นสุด (หรือขยาย) ตามแกนคอลัมน์ (บล็อก) ของคอนเทนเนอร์กริด
คุณสมบัติgrid-row-endยอมรับค่าต่อไปนี้:
.grid-item1 {
grid-row-end: auto;
}
ข้อมูลโค้ดด้านบนใช้autoค่าเพื่อสิ้นสุดอัตโนมัติgrid-item1ตามโฟลว์เค้าโครงแถวปกติ
.grid-item1 {
grid-row-start: 1;
grid-row-end: 5;
}
ตัวอย่างด้านบนใช้grid-row-endคุณสมบัติเพื่อสิ้นสุดgrid-item1ที่แถวบรรทัดที่ 5
.grid-item1 {
grid-row-end: span 3;
}
ตัวอย่างด้านบนใช้span 3ค่าเพื่อครอบคลุมgrid-item1สามแถว
แถวตารางเป็นชวเลขสำหรับคุณสมบัติgrid-row-startและgrid-row-end
กล่าวอีกนัยหนึ่งแทนที่จะเขียนว่า:
.grid-item1 {
grid-row-start: 1;
grid-row-end: 5;
}
คุณสามารถใช้grid-rowคุณสมบัติเพื่อย่อรหัสของคุณดังนี้:
.grid-item1 {
grid-row: 1 / 5;
}
นี่คือไวยากรณ์ของแถวกริด:
grid-row: grid-row-start / grid-row-end;
คุณสามารถใช้ คุณสมบัติ พื้นที่กริดเพื่อวัตถุประสงค์ต่อไปนี้:
เรามาพูดถึงจุดประสงค์สองประการด้านล่างกัน
นี่คือไวยากรณ์สำหรับการใช้grid-areaคุณสมบัติเป็นชวเลขสำหรับ คุณสมบัติ grid-column-start, grid-column-end, grid-row-startและgrid-row-end:
.your-grid-item {
grid-area: grid-row-start / grid-column-start / grid-row-end / grid-column-end;
}
ดังนั้น แทนที่จะเขียนว่า
.grid-item1 {
grid-row-start: 3;
grid-row-end: 5;
grid-column-start: 1;
grid-column-end: span 2;
}
คุณสามารถใช้grid-areaคุณสมบัติเพื่อย่อรหัสของคุณดังนี้:
.grid-item1 {
grid-area: 3 / 1 / 5 / span 2;
}
นี่คือไวยากรณ์สำหรับการใช้grid-areaคุณสมบัติเพื่อระบุชื่อของรายการกริด:
.your-grid-item {
grid-area: item-name;
}
นี่คือตัวอย่าง:
.grid-item1 {
grid-area: firstDiv;
}
.grid-item2 {
grid-area: middleDiv;
}
.grid-item2 {
grid-area: lastDiv;
}
<section>
<div class="grid-item1">1</div>
<div class="grid-item2">2</div>
<div class="grid-item3">3</div>
</section>
การใช้grid-areaเพื่อกำหนดรายการกริดที่มีชื่อทำให้คุณสมบัติคอนเทนเนอร์กริดของคุณgrid-template-areasใช้ชื่อเพื่อกำหนดขนาดและตำแหน่งของรายการ
พื้นที่เทมเพลตกริดระบุพื้นที่ที่คุณต้องการวางรายการกริดที่มีชื่อภายในคอนเทนเนอร์กริด
ข้อควรจำ:เราใช้คุณสมบัติ CSS grid-areaเพื่อตั้งชื่อรายการกริด
.grid-item1 {
grid-area: firstDiv;
}
section {
display: grid;
grid-template-areas: "firstDiv firstDiv firstDiv . .";
background-color: orange;
margin: 50px;
}
ข้อมูลโค้ดด้านบนใช้grid-template-areasพร็อพเพอร์ตี้เพื่อวางgrid-item1ในพื้นที่สามคอลัมน์แรก
หมายเหตุต่อไปนี้:
.grid-item1 {
grid-area: header;
}
.grid-item2 {
grid-area: article;
}
.grid-item3 {
grid-area: footer;
}
.grid-item4 {
grid-area: sidebar;
}
.grid-item5 {
grid-area: ads1;
}
.grid-item6 {
grid-area: ads2;
}
.grid-item7 {
grid-area: ads3;
}
section {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(7, 1fr);
grid-template-areas:
"header header header header header"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads2"
"sidebar article article article ads3"
"sidebar footer footer footer footer";
background-color: orange;
margin: 30px;
}
ข้อมูลโค้ดด้านบนใช้grid-template-areasคุณสมบัติเพื่อระบุตำแหน่งที่เบราว์เซอร์ควรวางรายการกริดในแถวและคอลัมน์ของคอนเทนเนอร์กริด
ต่อไปนี้เป็นข้อมูลสำคัญ 4 ประการที่ต้องจำไว้เมื่อใช้grid-template-areasพร็อพเพอร์ตี้:
คุณสมบัติgrid-template-areasต้องการให้คุณระบุรายการสำหรับเซลล์กริดทั้งหมด
ตัวอย่างเช่น พิจารณาส่วนย่อยนี้:
grid-template-areas:
"header header"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads2"
"sidebar article article article ads3"
"sidebar footer footer footer footer";
ด้านบนเป็นgrid-template-areasค่าที่ไม่ถูกต้องเนื่องจากแถวแรกไม่สมบูรณ์
กล่าวอีกนัยหนึ่ง แถวแรกเป็นแถวเดียวที่มีสองคอลัมน์ อย่างไรก็ตามgrid-template-areasคาดว่าแถวทั้งหมดในคอนเทนเนอร์กริดจะมีจำนวนคอลัมน์เท่ากัน
สมมติว่าคุณต้องการเว้นว่างบางเซลล์ไว้ ในกรณีนั้น ให้ใช้จุด ( .) หรือจุดที่ไม่เว้นวรรคหลายจุด ( ....)
นี่คือตัวอย่าง:
grid-template-areas:
"header header . . ."
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads2"
"sidebar article article article ads3"
"sidebar footer footer footer footer";
ตัวอย่างด้านบนใช้ สัญลักษณ์จุด เว้นระยะ สาม จุด ( .) เพื่อระบุเซลล์ว่างสามเซลล์
คุณสมบัติgrid-template-areasไม่สามารถวางรายการสองครั้งภายในคอนเทนเนอร์กริด
ตัวอย่างเช่น พิจารณาส่วนย่อยนี้:
grid-template-areas:
"header header header header header"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads2"
"sidebar article article article ads3"
"sidebar footer header header header";
ด้านบนเป็นgrid-template-areasค่าที่ไม่ถูกต้องเนื่องจากheaderรายการใช้พื้นที่กริดสองพื้นที่
ที่พักgrid-template-areasไม่สามารถสร้างพื้นที่ที่ไม่ใช่รูปสี่เหลี่ยมผืนผ้า (เช่น รูปตัว T หรือรูปตัว L)
ตัวอย่างเช่น พิจารณาส่วนย่อยนี้:
grid-template-areas:
"header header header header header"
"sidebar ads1 ads1 ads1 ads1"
"sidebar article article article ads1"
"sidebar article article article ads1"
"sidebar article article article ads2"
"sidebar article article article ads3"
"sidebar footer footer footer footer";
ด้านบนเป็นgrid-template-areasค่าที่ไม่ถูกต้องเนื่องจากads1รายการสร้างพื้นที่กริดที่ไม่ใช่สี่เหลี่ยม
ตอนนี้เราทราบประเภทของคุณสมบัติของรายการกริด CSS แล้ว เราสามารถพูดคุยเกี่ยวกับวิธีกำหนดขนาดกริดต่ำสุดและสูงสุดได้
minmax()เป็นฟังก์ชัน CSS Grid สำหรับกำหนดขนาดกริดต่ำสุดและสูงสุด
minmax()ยอมรับสองข้อโต้แย้ง นี่คือไวยากรณ์:
minmax(minimum-size, maximum-size)
หมายเหตุต่อไปนี้:
คุณสามารถใช้minmax()ฟังก์ชันเป็นค่าสำหรับคุณสมบัติ CSS ต่อไปนี้:
ด้านล่างนี้คือตัวอย่างวิธีminmax()การทำงานของฟังก์ชัน CSS
section {
display: grid;
grid-template-rows: 50px 100px minmax(70px, 250px);
grid-template-columns: auto auto auto;
background-color: orange;
margin: 10px;
padding: 7px;
}
เราใช้ฟังก์ชัน CSS minmax()เพื่อตั้ง<section>ค่าความสูงของแถวที่สามเป็นค่าต่ำสุด70pxและสูงสุด250pxเป็น
section {
display: grid;
grid-template-rows: auto auto auto;
grid-template-columns: 1fr minmax(30%, 70%) 1fr;
background-color: orange;
margin: 10px;
padding: 7px;
}
เราใช้minmax()ฟังก์ชัน CSS เพื่อตั้ง<section>ค่าความกว้างของคอลัมน์ที่สองเป็นค่าต่ำสุด30%และค่าสูงสุด70%เป็น
หมายเหตุ:คุณสามารถใช้ฟังก์ชัน CSS repeat()เพื่อระบุgrid-template-rowsหรือgrid-template-columnsค่าที่มีรูปแบบซ้ำๆ ได้ เรามาพูดถึงrepeat()ฟังก์ชั่นกัน
ฟังก์ชันrepeat() CSS ช่วยให้คุณเขียนค่าที่กระชับและอ่านง่ายขึ้นเมื่อระบุแทร็กตาราง หลายรายการ ที่มีรูปแบบซ้ำๆ
บันทึก:
repeat()ยอมรับสองข้อโต้แย้ง นี่คือไวยากรณ์:
repeat(number-of-repetition, track-list-to-repeat)
อาร์กิวเมนต์number-of-repetitionระบุจำนวนครั้งที่เบราว์เซอร์ควรทำซ้ำรายการแทร็กที่ระบุ (อาร์กิวเมนต์ที่สอง)
อาร์กิวเมนต์number-of-repetitionสามารถเป็นค่าใดก็ได้ต่อไปนี้:
ค่าauto-fillและauto-fitจะสร้างแทร็กโดยอัตโนมัติตามต้องการเพื่อเติมคอนเทนเนอร์กริดโดยไม่ทำให้เกิดโอเวอร์โฟลว์
ความแตกต่างระหว่างค่าทั้งสองคือการauto-fitยุบแทร็กเปล่าเป็นศูนย์พิกเซล ( 0px) แต่auto-fillแสดงทั้งแทร็กที่ว่างเปล่าและเต็ม
หมายเหตุ:แทร็กว่างคือคอลัมน์หรือแถวที่ไม่มีรายการตาราง
อาร์กิวเมนต์track-list-to-repeatระบุรูปแบบแทร็กที่คุณต้องการทำซ้ำในแกนแนวนอนหรือแนวตั้งของคอนเทนเนอร์กริด
กล่าวอีกนัยหนึ่งtrack-list-to-repeatประกอบด้วยค่าหนึ่งค่าหรือมากกว่าที่ระบุขนาดของแทร็กที่เบราว์เซอร์ควรทำซ้ำภายในคอนเทนเนอร์กริด
หมายเหตุ: สมมติ ว่าคุณnumber-of-repetitionเป็นauto-fillหรือ auto-fitในกรณีนั้น คุณสามารถใช้เฉพาะขนาดคงที่เป็นtrack-list-to-repeatอาร์กิวเมนต์
ด้านล่างนี้คือตัวอย่างวิธีrepeat()การทำงานของฟังก์ชัน CSS
section {
display: grid;
grid-template-columns: repeat(3, 70px);
background-color: orange;
margin: 10px;
padding: 7px;
}
ตัวอย่างด้านบนใช้repeat()ฟังก์ชัน CSS เพื่อสร้าง70pxคอลัมน์กว้าง สามคอลัมน์
ด้านล่างเป็นคุณสมบัติที่ไม่repeat()เทียบเท่ากับด้านบนgrid-template-columns:
grid-template-columns: 70px 70px 70px;
section {
display: grid;
grid-template-columns: 50px repeat(3, 90px);
background-color: orange;
margin: 10px;
padding: 7px;
}
ตัวอย่างด้านบนใช้repeat()ฟังก์ชัน CSS เพื่อสร้าง90pxคอลัมน์กว้าง สามคอลัมน์
ด้านล่างเป็นคุณสมบัติที่ไม่repeat()เทียบเท่ากับด้านบนgrid-template-columns:
grid-template-columns: 50px 90px 90px 90px;
section {
display: grid;
grid-template-columns: 40px repeat(2, 60px 1fr);
background-color: orange;
margin: 10px;
padding: 7px;
}
ข้อมูลโค้ดด้านบนใช้repeat()ฟังก์ชัน CSS เพื่อสร้าง60px 1frคอลัมน์กว้าง สองคอลัมน์
ด้านล่างเป็นคุณสมบัติที่ไม่repeat()เทียบเท่ากับด้านบนgrid-template-columns:
grid-template-columns: 40px 60px 1fr 60px 1fr;
หมายเหตุ:เราใช้frหน่วย (เศษส่วน)เพื่อปรับขนาดคอลัมน์ที่สามและห้าโดยเทียบกับเศษส่วนของพื้นที่ว่างในคอนเทนเนอร์กริด
section {
display: grid;
grid-template-columns: repeat(auto-fill, 70px);
background-color: orange;
margin: 10px;
padding: 7px;
}
ข้อมูลโค้ดด้านบนใช้ฟังก์ชัน CSS repeat()เพื่อเติมคอนเทนเนอร์กริดโดยอัตโนมัติด้วย70pxคอลัมน์กว้าง
section {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(50px, 1fr));
background-color: orange;
margin: 10px;
padding: 7px;
}
ข้อมูลโค้ดด้านบนใช้ CSS repeat()และminmax()ฟังก์ชันเพื่อเติมคอนเทนเนอร์กริดโดยอัตโนมัติด้วย50pxคอลัมน์ที่มีความกว้างขั้นต่ำและสูงสุด1frที่
หมายเหตุ หมายถึง หน่วยเศษส่วน1frหนึ่ง หน่วย
section {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(50px, 1fr));
background-color: orange;
margin: 10px;
padding: 7px;
}
ข้อมูลโค้ดด้านบนใช้ ฟังก์ชัน CSS repeat()และminmax()เพื่อให้พอดีกับคอนเทนเนอร์กริดโดยอัตโนมัติโดยมีคอลัมน์กว้างขั้นต่ำและ50pxสูงสุด1fr
ในบทความนี้ เราได้กล่าวถึงเครื่องมือ CSS Grid ทั้งหมดที่คุณต้องสร้างเลย์เอาต์เว็บไซต์ขั้นพื้นฐานและขั้นสูงในรูปแบบตอบสนองที่ดูดีบนอุปกรณ์ทั้งหมด
ฉันหวังว่าคุณจะพบว่าบทความนี้มีประโยชน์
หากคุณชอบบทช่วยสอนนี้ คุณสามารถรับเวอร์ชันคู่มือได้ที่ Amazon เป็นคู่มืออ้างอิงฉบับย่อที่มีประโยชน์สำหรับ CSS Grid
ที่มา: https://www.freecodecamp.org
#css
1625061420
Collection of free hand-picked simple CSS grid examples. Also, it includes a bunch of front-end techniques, tips, and tricks for your future reference. Hope you will like these freebies and find them useful. Happy coding!
#layouts #css grid #grid #layouts #css #css grid layout
1625050140
This is a collection of free CSS Masonry grids. I have found around the Codepen! This Masonry grid allows you to easily create grid layouts in HTML and CSS without having to program the whole thing in JavaScript. CSS Masonry grids are a great way to help layout elements in a grid-like format. If you need some inspiration for your next design layout, see the free CSS Masonry grids below.
#layouts #css grid #grid #css masonry #css #free
1603188000
The other day one of our students asked about possibility of having a CSS cheatsheet to help to decide on the best suited approach when doing this or that layout.
This evolved into the idea of making a visual CSS cheatsheet with all (most) of the common patterns we see everyday and one of the best possible conceptual implementation for them.
In the end any layout could and should be split into parts/blocks and we see every block separately.
Here is our first take on that and we would be happy to keep extending it to help us all.
Please, send you suggestions in the comments in community or via gitlab for the repeated CSS patterns with your favourite implementation for that so that we will all together make this as useful as it can be.
#css #css3 #cascading-style-sheets #web-development #html-css #css-grids #learning-css #html-css-basics
1617932400
This effect is so cool and just fun to see. What it comes down to is having a background image show through the text.
How it works is that we will have a div that will have the image as a background. On that, we put our text element, using blend-mode it will show through the image.
The result you can see and touch on this Codepen.
#css #css3 #html-css #css-grids #learning-css #html-css-basics
1617785759
Let’s refresh Our CSS Grid Memory. Here’s a Cheat Sheet of everything you can do with Grid to get started in 2021!🎖️
#css #css3 #css-grid #learning-css #html-css #web-development #webdev