การใช้งานมิกซินใน SASS
หลังจากที่เราประกาศมิกซินแล้ว
เราสามารถเรียกใช้มันได้ด้วยไดเรกทีฟ @include,
ซึ่งจะต้องระบุชื่อของมิกซินไว้เสมอ
นอกจากนี้ ยังสามารถเพิ่มพารามิเตอร์และสไตล์เพิ่มเติมได้ (ไม่จำเป็นต้องมีก็ได้)
มาดูตัวอย่าง:
@mixin mix{
width: 100px;
height: 100px;
}
div {
@include mix;
padding: 4px;
margin-top: 10px;
}
ผลลัพธ์หลังคอมไพล์:
div {
width: 100px;
height: 100px;
padding: 4px;
margin-top: 10px;
}
คุณสามารถเรียกใช้มิกซินที่ระดับรากของเอกสารได้เช่นกัน แต่เฉพาะในกรณีที่มิกซินเหล่านั้นไม่ได้กำหนดกฎ (rules) และไม่มีอ้างอิงถึง parent ดูตัวอย่างต่อไปนี้:
@mixin active {
p {
color: #161618;
background-color: red;
}
}
@include active;
หลังการคอมไพล์เราจะได้:
p {
color: #161618;
background-color: red;
}
บอกหน่อยว่าผลลัพธ์หลังคอมไพล์ของโค้ดต่อไปนี้จะเป็นอย่างไร:
@mixin font {
font-family:'Courier New', Courier, monospace;
font-size: 12px;
}
#product-card {
@include font;
color: #090957;
font-weight: 600px;
}
บอกหน่อยว่าผลลัพธ์หลังคอมไพล์ของโค้ดต่อไปนี้จะเป็นอย่างไร:
@mixin style {
color:#090957;
}
button, link {
@include style;
font-size: 10px;
padding: 4px;
}
บอกหน่อยว่าผลลัพธ์หลังคอมไพล์ของโค้ดต่อไปนี้จะเป็นอย่างไร:
@mixin active-text {
font-size: 14px;
font-weight: bold;
text-decoration: underline;
color: blue;
}
#product-card, .active, .content {
@include active-text;
padding: 4px;
}
บอกหน่อยว่าผลลัพธ์หลังคอมไพล์ของโค้ดต่อไปนี้จะเป็นอย่างไร:
@mixin size {
font-size: 14px;
padding: 10px;
margin: 4px;
}
@mixin color {
color: #580909;
background-color: #e9e5ab;
}
#product-card {
@include size;
@include color;
width: 600px;
}