การจัดรูปแบบด้วย CSS
แนะนำใหม่| English | ความหมาย |
|---|---|
| padding/ˈpædɪŋ/ | ระยะขอบด้านใน |
| border/ˈbɔːdə/ | เส้นขอบ |
| CSS/ˌsiː es ˈes/ | สไตลชีทแบบชั้นซ้อน |
| selector/sɪˈlektə/ | ตัวเลือก |
| declaration/ˌdekləˈreɪʃn/ | คำประกาศ |
| the box model/ðə bɒks ˈmɒdl/ | โมเดลกล่อง |
| margin/ˈmɑːdʒɪn/ | ระยะขอบด้านนอก |
| responsive design/rɪˈspɒnsɪv dɪˈzaɪn/ | การออกแบบที่ปรับขนาดได้ |
| media query/ˈmiːdiːə ˈkwɪərɪ/ | คำถามเกี่ยวกับสื่อ |
A 320-pixel card still overflows a 320-pixel screen
- The card's declared width is not always its total width. Padding and borders can add space outside the content width.
- CSS 层叠样式表 applies presentation rules. A selector 选择器 chooses elements; a declaration 声明 gives a property and value, such as
color: navy.
ให้เหตุผลทางปฏิบัติหนึ่งประการในการรักษากฎการนำเสนอที่นำไปใช้ซ้ำได้ในไฟล์สไตล์ชีท
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวอย่าง: “การใช้ไฟล์สไตล์ชีทร่วมกันช่วยให้เราอัปเดตการ์ดที่มีรูปแบบตรงกันได้อย่างสม่ำเสมอโดยไม่ต้องแก้ไข HTML element ทุกตัว”
Account for every part of the box
- The box model 盒模型 has content, padding 内边距, border 边框 and margin 外边距. Padding and border are inside the painted box; margin separates it from neighbours.
- With
content-box, width 320 px, padding 16 px on both sides and borders 2 px on both sides give 356 px before margins. Withborder-box, the declared 320 px includes that padding and border.

One width, two box-sizing models
$W_{content-box}=W+2P+2B$
Compare the content width and painted width with the lesson’s 320 px card.
เรียงลำดับพื้นที่ของกล่องโมเดลจากเนื้อหาออกไปด้านนอก
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Padding และ border ล้อมรอบเนื้อหา; margin แยกกล่องออกจากเพื่อนบ้าน
เมื่อใช้ content-box ความกว้างของเนื้อหาคือ 320 px, ขอบด้านซ้าย/ขวาคือ 16 px แต่ละด้าน และขอบด้านซ้าย/ขวาคือ 2 px แต่ละด้าน ความกว้างก่อนรวมค่า margins คือเท่าไหร่ในหน่วยพิกเซล?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
320 + 16 + 16 + 2 + 2 = 356 px.
Make the width flexible
- Responsive design 响应式设计 adapts content and layout to available space. This card fills its container up to 400 px, with padding and border included.
- Use the rule on an element with
class="card". Long unbroken content, parent margins and other rules still need checking; one declaration cannot guarantee the whole page fits.
.card {
box-sizing: border-box;
width: 100%;
max-width: 400px;
padding: 16px;
border: 2px solid currentColor;
overflow-wrap: anywhere;
}
Inside a 280 px container, this card is 280 px wide including its padding and border. Inside a 700 px container, it stops at 400 px. Its outer margins are still additional space.
A padded card should fill its container up to 400 px including its borders. Which rule set best fits?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
border-box รวม padding และ borders ไว้ในความกว้างที่กำหนด Parent layout และ content ยังต้องการการทดสอบ
Change layout only when it needs to change
- A media query 媒体查询 applies rules under a condition. For example, change two columns to one when they become too narrow to read.
- Choose the breakpoint from the content, then test several widths and larger text. Do not assume that all phones or all laptops have the same usable width.
Inspect the rule that actually wins
- The cascade considers factors including origin, importance and layers before selector specificity. For otherwise comparable rules, an ID is more specific than a class; a class is more specific than an element selector.
- When comparable rules have equal specificity, later source order can decide. Use browser developer tools to check matched rules, the loaded stylesheet and computed values.
!important participates in the cascade; it does not break CSS. Avoid using it as an unexplained patch. Check the cause first: a missing stylesheet, invalid declaration or overriding rule may all explain an unexpected result.
กฎปกติสองข้อที่มี origin และ layer เดียวกันใช้ selector .card เหมือนกันและกำหนดสีที่ขัดแย้งกัน ไม่มีปัจจัยตัดสินอื่นใดแตกต่างกัน สีใดจะชนะ?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ความจำเพาะเท่ากันไม่ได้หมายความว่าลำดับไม่สำคัญ สำหรับกฎที่เทียบเท่ากันและติดอันดับ ลำดับแหล่งที่มาทีหลังจะเป็นผู้ตัดสิน
!important เป็นส่วนหนึ่งของกฎการสลายตัวของ CSS แต่ไม่ควรใช้เป็นวิธีแก้ไขที่ไม่มีการอธิบายประกอบ
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ความสำคัญมีผลต่อการลำดับความสำคัญ ตรวจสอบความขัดแย้งและใช้โดยมีเจตนา แทนที่จะอ้างว่าทำให้ CSS บกพร่อง
จับคู่ข้อสังเกตกับการตรวจสอบที่เป็นประโยชน์ต่อไป
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตรวจสอบสาเหตุจริงแทนการสันนิษฐานว่าความล้มเหลวทั้งหมดมาจาก specificity
Test the box and the reading experience
- Try narrow and wide containers, keyboard focus and enlarged text. Check that text is readable in both colour themes and that content can wrap.
- Explain one overflow repair using the box calculation and one style conflict using the actual matching rules.
Calculate the whole box, prefer flexible sizes, then inspect the cascade. Specificity matters, but it is not the only deciding factor.
Source: W3C CSS cascade.
การ์ดพอดีกับขนาดข้อความปกติแต่ล้นออกเมื่อขยายข้อความ คุณควรทำอะไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
พฤติกรรมแบบตอบสนองรวมถึงการปรับข้อความและข้อจำกัดเนื้อหา ไม่ใช่แค่ภาพหน้าจอ viewport เดียว