แนวคิดส่วนใหญ่ของ CSS ตรงกับแนวคิดการจัดรูปแบบของ Roblox ตัวอย่างต่อไปนี้แสดงให้เห็นว่าคอนเซ็ปต์ CSS และ HTML สอดคล้องกับ Luau และคลาส/คุณสมบัติของ Roblox อย่างไร
ในการทดสอบแต่ละตัวอย่างสคริปต์ Luau ต่อไปนี้:
ใน Explorer ให้สร้างสิ่งต่อไปนี้:

- StyleSheet อินสแตนซ์ภายใน ReplicatedStorage.
- ตัวจัดเก็บ ScreenGui ใน StarterGui.
- อินสแตนซ์ LocalScript ภายใน ScreenGui.
ใน LocalScript ให้วางโค้ดสนับสนุนต่อไปนี้:
LocalScriptlocal CollectionService = game:GetService("CollectionService")local ReplicatedStorage = game:GetService("ReplicatedStorage")local coreSheet = ReplicatedStorage:FindFirstChildWhichIsA("StyleSheet")local screenGui = script.Parentlocal styleLink = screenGui:FindFirstChildWhichIsA("StyleLink")styleLink.StyleSheet = coreSheetสำหรับแต่ละตัวอย่างด้านล่าง ให้วางบรรทัดโค้ด Luau ตามที่สนับสนุนบรรทัด 1–7.
ตัวเลือก
คุณสมบัติ Selector ของ StyleRule ระบุว่ากฎไหนควรมีผลต่ออินสแตนซ์ใด ตัวเลือกเฉพาะหลายประเภทเชื่อมโยงจาก CSS ไปยัง Luau และสามารถใช้ร่วมกับตัวประกอบได้
องค์ประกอบ
องค์ประกอบที่เทียบเท่ากับตัวเลือกองค์ประกอบ CSS คือ ตัวเลือกคลาส ของ Roblox ที่เลือกอินสแตนซ์ทั้งหมดของคลาส GuiObject ที่กำหนด เช่น Frame, ImageLabel, TextButton ฯลฯ
CSSbutton {background-color: #335FFF;color: #E1E1E1;width: 15%;height: 40px;border: none;}
HTML<button>เมนูหลัก</button>
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = "TextButton" -- ตัวเลือกคลาส Robloxrule:SetProperties({BackgroundColor3 = Color3.fromHex("335FFF"),TextColor3 = Color3.fromHex("E1E1E1"),Size = UDim2.new(0.15, 0, 0, 40),BorderSizePixel = 0})local button = Instance.new("TextButton")button.Text = "เมนูหลัก"button.Parent = screenGui
คลาส
ภาพรวมที่ตรงกับ CSS class selectors ของ Roblox คือ tag selectors ที่ใช้ แท็ก ที่ใช้ในการจัดกลุ่มผ่าน CollectionService.
CSS.button-primary {background-color: #335FFF;color: #E1E1E1;}
HTML<button class="button-primary">เมนูหลัก</button>
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = ".ButtonPrimary" -- ตัวเลือกแท็ก Robloxrule:SetProperties({BackgroundColor3 = Color3.fromHex("335FFF"),TextColor3 = Color3.fromHex("E1E1E1"),AutomaticSize = Enum.AutomaticSize.XY})local button = Instance.new("TextButton")button.Text = "เมนูหลัก"button.Parent = screenGui-- เพิ่มแท็กให้กับปุ่มCollectionService:AddTag(button, "ButtonPrimary")
ตัวระบุ
การเปรียบเทียบที่ใกล้เคียงที่สุดของ Roblox กับ CSS id คือ ตัวเลือก #[name] ที่เลือกระหว่างค่าว่าอะไรคือ Instance.Name แตกต่างจาก W3C specification ของคุณสมบัติ id ชื่อไม่จำเป็นต้องไม่ซ้ำกัน
CSS#modal-frame {background-color: #000022;opacity: 0.5;width: 50%;min-height: 100px;}
HTML<div id="modal-frame"></div>
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = "#ModalFrame" -- ตัวเลือกชื่ออินสแตนซ์rule:SetProperties({BackgroundColor3 = Color3.fromHex("000022"),BackgroundTransparency = 0.5,Size = UDim2.new(0.5, 0, 0, 100),AutomaticSize = Enum.AutomaticSize.Y})local frame = Instance.new("Frame")frame.Parent = screenGui-- เปลี่ยนชื่อเฟรมให้ตรงกับตัวเลือกframe.Name = "ModalFrame"
ตัวประกอบ
ตัวประกอบช่วยให้คุณผสมผสาน ตัวเลือก พื้นฐานเพื่อจับคู่ความสัมพันธ์ลำดับชั้นที่ลึกซึ้งยิ่งขึ้น
เด็ก
ตัวเลือกเด็ก > เป็นเหมือนกันระหว่าง CSS และ Roblox
CSS.menu-container {width: 25%;}.menu-container > button {background-color: #335FFF;color: #E1E1E1;width: 80%;height: 40px;border: none;}
HTML<div class="menu-container"><button>ตัวเลือก</button></div>
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = ".MenuContainer > TextButton" -- ตัวเลือกเด็กrule:SetProperties({BackgroundColor3 = Color3.fromHex("335FFF"),TextColor3 = Color3.fromHex("E1E1E1"),Size = UDim2.new(0.8, 0, 0, 40),BorderSizePixel = 0})-- สร้างเมนูคอนเทนเนอร์local menuContainer = Instance.new("Frame")menuContainer.Size = UDim2.new(0.25, 0, 0, 0)menuContainer.AutomaticSize = Enum.AutomaticSize.YmenuContainer.Parent = screenGui-- เพิ่มแท็กCollectionService:AddTag(menuContainer, "MenuContainer")-- สร้างปุ่มlocal button = Instance.new("TextButton")button.Text = "ตัวเลือก"-- ตั้งค่าเมนูคอนเทนเนอร์เป็นพ่อของปุ่มbutton.Parent = menuContainer
ลูกหลาน
แตกต่างจากไวยากรณ์ของช่องว่างใน CSS เช่น .menu-container button ที่เห็นที่นี่ Roblox ใช้ตัวประกอบ >> เพื่อระบุความสัมพันธ์ของลูกหลาน
CSS.menu-container {width: 25%;}.sub-container {width: 75%;}.menu-container button {background-color: #335FFF;color: #E1E1E1;width: 80%;height: 40px;border: none;}
HTML<div class="menu-container"><div class="sub-container"><button>ตัวเลือก</button></div></div>
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = ".MenuContainer >> TextButton" -- ตัวเลือกลูกหลานrule:SetProperties({BackgroundColor3 = Color3.fromHex("335FFF"),TextColor3 = Color3.fromHex("E1E1E1"),Size = UDim2.new(0.8, 0, 0, 40),BorderSizePixel = 0})-- สร้างเมนูคอนเทนเนอร์local menuContainer = Instance.new("Frame")menuContainer.Size = UDim2.new(0.25, 0, 0, 0)menuContainer.AutomaticSize = Enum.AutomaticSize.YmenuContainer.Parent = screenGui-- เพิ่มแท็กCollectionService:AddTag(menuContainer, "MenuContainer")-- สร้างซับคอนเทนเนอร์local subContainer = Instance.new("Frame")subContainer.Size = UDim2.new(0.75, 0, 0, 0)subContainer.AutomaticSize = Enum.AutomaticSize.Y-- ตั้งค่าเมนูคอนเทนเนอร์เป็นพ่อของซับคอนเทนเนอร์subContainer.Parent = menuContainer-- สร้างปุ่มlocal button = Instance.new("TextButton")button.Text = "ตัวเลือก"-- ตั้งค่าซับคอนเทนเนอร์เป็นพ่อของปุ่มbutton.Parent = subContainer
รายการตัวเลือก
ตัวเลือกหลายตัวเลือก (รวมถึงตัวเลือกที่มีตัวประกอบ) สามารถประกาศได้ด้วยบล็อกคุณสมบัติเดียวกัน แยกด้วยเครื่องหมายจุลภาค เพื่อเพื่อลดความซ้ำซ้อน
CSSimg, p {background-color: #FF0033;}
HTML<img src="gear.png" width="100" height="100"><p>เมนูหลัก</p>
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = "ImageLabel, TextLabel" -- ตัวเลือกสำหรับป้ายกำกับภาพและป้ายกำกับข้อความrule:SetProperty("BackgroundColor3", Color3.fromHex("ff0033"))-- สร้างป้ายกำกับภาพlocal imageLabel = Instance.new("ImageLabel")imageLabel.Image = "rbxassetid://104919049969988"imageLabel.Size = UDim2.new(0, 100, 0, 100)imageLabel.Parent = screenGui-- สร้างป้ายกำกับข้อความlocal textLabel = Instance.new("TextLabel")textLabel.Size = UDim2.new(1, 0, 0, 0)textLabel.AutomaticSize = Enum.AutomaticSize.YtextLabel.TextXAlignment = Enum.TextXAlignment.LefttextLabel.TextYAlignment = Enum.TextYAlignment.ToptextLabel.Text = "เมนูหลัก"textLabel.Parent = screenGui
คลาสพิเศษ
การเปรียบเทียบ Roblox กับ CSS pseudo-class selectors คือ state selectors ซึ่งตรงกับหนึ่งในสี่ Enum.GuiState ค่า เช่น Hover หรือ Press.
CSSimg:hover {opacity: 0.5;}
HTML<img src="gear.png" width="100" height="100">
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = "ImageLabel:Hover" -- ตัวเลือกสถานะrule:SetProperty("ImageTransparency", 0.5)-- สร้างป้ายกำกับภาพlocal imageLabel = Instance.new("ImageLabel")imageLabel.Image = "rbxassetid://104919049969988"imageLabel.Size = UDim2.new(0, 100, 0, 100)imageLabel.BackgroundTransparency = 1imageLabel.Parent = screenGui
พูดนอก
คล้ายกับ CSS pseudo-elements ที่สามารถปรับแต่งส่วนเฉพาะขององค์ประกอบได้ Roblox สามารถสร้าง UIComponents ที่เป็นภาพลวงตาผ่านคุณสมบัติ Selector ของกฎสไตล์ ตัวอย่างเช่น กฎต่อไปนี้จะสร้างตัวปรับ UICorner ภายใต้ Frame ทุกตัวที่มีแถบ RoundedCorner20 และตั้งค่ารัศมีของโมดิฟายเออร์แต่ละตัวเป็น 20 พิกเซล
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheetrule.Selector = "Frame.RoundedCorner20::UICorner" -- ตัวเลือกคอมโพเนนต์ UIrule:SetProperty("CornerRadius", UDim.new(0, 20))-- สร้างเฟรมlocal frame = Instance.new("Frame")frame.Size = UDim2.new(0.4, 0, 0.2, 0)frame.Parent = screenGui-- เพิ่มแท็กให้กับเฟรมCollectionService:AddTag(frame, "RoundedCorner20")
การสืบค้น
Roblox style queries เป็นเส้นทางเชื่อมโยงระหว่าง CSS media queries และ container queries โดยใช้พรีฟิก @ คุณสามารถสลับสไตล์ตามขนาดของพ่อแม่ ประเภทอุปกรณ์อินพุต หรือการตั้งค่าการเข้าถึงของผู้ใช้
ใน CSS @container ใช้เพื่อใช้สไตล์ตามขนาดของส่วนประกอบพ่อ ใน Roblox คุณจะกำหนดพรีโอตนซ์ pseudo-instance query โดยใช้ ::StyleQuery ตามด้วย ตัวระบุ สำหรับคุณสมบัติ Selector ของเงื่อนไข ตัวอย่างเช่น "::StyleQuery #WideContainer" ควบคู่กับชื่อเงื่อนไข StyleQuery เช่น "MinSize" เพื่อประเมิน GuiObject.AbsoluteSize ของผู้ปกครอง จากนั้นจึงสร้าง StyleRule โดยมี @[identifier] prefix เป็นคุณสมบัติ Selector เช่น @WideContainer ที่ใช้ในการจัดสไตล์ให้กับอินสแตนซ์หรือบุตรของมันเมื่อคำถามใช้งานอยู่.
CSS.container {container-type: inline-size;background-color: rgba(0, 0, 34, 0.5);}button {background-color: #335FFF;color: #E1E1E1;width: 75%;height: 40px;border: none;}@container (min-width: 400px) {button {background-color: #cc0033;}}
HTML<div class="container" style="width: 80%; height: 200px;"><button>เมนูหลัก</button></div>
Luau-- กฎของคอนเทนเนอร์local containerRule = Instance.new("StyleRule")containerRule.Selector = "Frame" -- ตัวเลือคลาส RobloxcontainerRule:SetProperties({BackgroundColor3 = Color3.fromRGB(0, 0, 34),BackgroundTransparency = 0.5,BorderSizePixel = 0})containerRule.Parent = coreSheet-- กฎปุ่มlocal buttonRule = Instance.new("StyleRule")buttonRule.Selector = "TextButton" -- ตัวเลือคลาส RobloxbuttonRule:SetProperties({BackgroundColor3 = Color3.fromHex("335FFF"),TextColor3 = Color3.fromHex("E1E1E1"),Size = UDim2.new(0.75, 0, 0, 40),BorderSizePixel = 0})buttonRule.Parent = containerRule -- เป็นลูกของกฎคอนเทนเนอร์-- เงื่อนไขการค้นหา (#WideContainer)local queryCondition = Instance.new("StyleRule")queryCondition.Selector = "::StyleQuery #WideContainer"queryCondition:SetProperty("MinSize", Vector2.new(400, 0))queryCondition.Parent = containerRule -- เป็นลูกของกฎคอนเทนเนอร์-- กฎที่ใช้เมื่อเงื่อนไขทำงาน (@WideContainer)local queryStyle = Instance.new("StyleRule")queryStyle.Selector = "@WideContainer Frame > TextButton" -- ตัวเลือกเด็กqueryStyle:SetProperty("BackgroundColor3", Color3.fromHex("CC0033"))queryStyle.Parent = containerRule -- เป็นลูกของกฎคอนเทนเนอร์-- สร้างอินสแตนซ์local container = Instance.new("Frame")container.Size = UDim2.new(0.8, 0, 0, 200)container.Parent = screenGuilocal button = Instance.new("TextButton")button.Text = "เมนูหลัก"button.Parent = container
Roblox ยังให้บริการ built-in queries ที่เชื่อมโยงโดยตรงกับสถานะของสภาพแวดล้อมทั่วโลก เช่น ViewportDisplaySize หรือ ReducedMotionEnabled. สิ่งเหล่านี้ไม่ต้องการการกำหนดค่า ::StyleQuery และสามารถใช้ได้โดยตรงด้วยพรีฟิก @.
| คุณสมบัติของ CSS Media | ตัวเลือกการค้นหาสไตล์ Roblox |
|---|---|
| (max-width: 600px) | @ViewportDisplaySizeSmall |
| (min-width: 601px) and (max-width: 1200px) | @ViewportDisplaySizeMedium |
| (min-width: 1201px) | @ViewportDisplaySizeLarge |
| (pointer: fine) | @PreferredInputKeyboardAndMouse |
| (pointer: coarse) | @PreferredInputTouch |
| (any-pointer: coarse) | @PreferredInputGamepad |
| (prefers-reduced-motion: reduce) | @ReducedMotionEnabledTrue |
| (prefers-reduced-motion: no-preference) | @ReducedMotionEnabledFalse |
CSS.container {container-type: inline-size;background-color: rgba(0, 0, 34, 0.5);}@media (prefers-reduced-motion: reduce) {.container {background-color: rgba(0, 0, 34, 1);}}
HTML<div class="container" style="width: 80%; height: 200px;"></div>
Luau-- กฎคอนเทนเนอร์local containerRule = Instance.new("StyleRule")containerRule.Selector = "Frame" -- ตัวเลือคลาส RobloxcontainerRule:SetProperties({BackgroundColor3 = Color3.fromRGB(0, 0, 34),BackgroundTransparency = 0.5,BorderSizePixel = 0})containerRule.Parent = coreSheet-- การค้นหาที่รวมอยู่local motionRule = Instance.new("StyleRule")motionRule.Selector = "@ReducedMotionEnabledTrue Frame"motionRule:SetProperty("BackgroundTransparency", 0)motionRule.Parent = containerRule -- เป็นลูกของกฎคอนเทนเนอร์-- สร้างอินสแตนซ์local container = Instance.new("Frame")container.Size = UDim2.new(0.8, 0, 0, 200)container.Parent = screenGui
ตัวแปร
CSS อนุญาตให้คุณประกาศและอ้างอิง ตัวแปร ทั่วทั้งระบบกานจัดรูปแบบ โดย Roblox ทำได้ผ่านระบบโทเคนและ คุณสมบัติของอินสแตนซ์. โดยใช้ $ เป็นพรีฟิก คุณสามารถอ้างอิงคุณสมบัติที่ประกาศใน StyleRule หรือ StyleSheet chain การสืบทอดเมื่อกำหนดค่าคุณสมบัติของสไตล์
CSS:root {--button-bg-color: #335FFF;--button-text-color: #E1E1E1;}button {background-color: var(--button-bg-color);color: var(--button-text-color);}
HTML<button>เมนูหลัก</button>
Luaulocal rule = Instance.new("StyleRule")rule.Parent = coreSheet-- ตั้งค่าโทเคนชีตสไตล์โดยใช้คุณสมบัติcoreSheet:SetAttribute("ButtonBgColor", Color3.fromHex("335FFF"))coreSheet:SetAttribute("ButtonTextColor", Color3.fromHex("E1E1E1"))rule.Selector = "TextButton" -- ตัวเลือกคลาสrule:SetProperties({BackgroundColor3 = "$ButtonBgColor",TextColor3 = "$ButtonTextColor"})-- สร้างปุ่มlocal button = Instance.new("TextButton")button.AutomaticSize = Enum.AutomaticSize.XYbutton.Text = "เมนูหลัก"button.Parent = screenGui
การเปลี่ยนผ่าน
CSS transitions ช่วยให้คุณทำการทวีคูณค่าคุณสมบัติตามระยะเวลาที่กำหนด คุณสามารถทำสิ่งนี้ใน Roblox โดยการตั้งค่าการเปลี่ยนคุณสมบัติใน StyleRule ผ่าน SetPropertyTransition() (หนึ่งเดียว) หรือ SetPropertyTransitions() (หลายรายการ).
CSSbutton {background-color: #335FFF;color: #E1E1E1;width: 15%;height: 40px;border: none;transition:background-color 1s ease-out,transform 1.25s ease-out}button:hover {background-color: #33AAFF;transform: rotate(-5deg);}
HTML<button>เมนูหลัก</button>
Luau-- กฎปุ่มlocal buttonRule = Instance.new("StyleRule")buttonRule.Selector = "TextButton" -- ตัวเลือคลาส RobloxbuttonRule:SetProperties({BackgroundColor3 = Color3.fromHex("335FFF"),TextColor3 = Color3.fromHex("E1E1E1"),Size = UDim2.new(0.15, 0, 0, 40),BorderSizePixel = 0})-- ตั้งค่าพฤติกรรมการเปลี่ยนคุณสมบัติbuttonRule:SetPropertyTransitions({BackgroundColor3 = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out),Rotation = TweenInfo.new(1.25, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)})buttonRule.Parent = coreSheet-- กฎปุ่ม Hoverlocal hoverRule = Instance.new("StyleRule")hoverRule.Selector = "TextButton:Hover" -- ตัวเลือกสถานะhoverRule:SetProperties({AutoButtonColor = false,BackgroundColor3 = Color3.fromHex("33AAFF"),Rotation = -5})hoverRule.Parent = coreSheet-- สร้างปุ่มข้อความlocal button = Instance.new("TextButton")button.Text = "เมนูหลัก"button.Parent = screenGui
การซ้อนและการรวม
การนำแนวคิดจาก SCSS มาใช้ StyleRules สามารถซ้อนกันและตัวเลือกของมันจะ รวมกัน.
SCSS#menu-frame {background-color: #000022;width: 25%;min-height: 200px;display: flex;flex-direction: column;justify-content: space-evenly;align-items: center;> button {background-color: #335FFF;color: #E1E1E1;width: 80%;height: 40px;border: none;&:hover {opacity: 0.5;}}}
HTML<div id="menu-frame"><button>Charms</button><button>Mana</button><button>Scrolls</button></div>
Luau-- กฎเฟรมเมนูlocal menuFrameRule = Instance.new("StyleRule")menuFrameRule.Selector = "#MenuFrame"menuFrameRule:SetProperties({BackgroundColor3 = Color3.fromHex("000022"),Size = UDim2.new(0.25, 0, 0, 200),AutomaticSize = Enum.AutomaticSize.Y})menuFrameRule.Parent = coreSheet-- กฎเลย์เอาต์เมนูlocal menuLayoutRule = Instance.new("StyleRule")menuLayoutRule.Selector = "::UIListLayout"menuLayoutRule:SetProperties({FillDirection = Enum.FillDirection.Vertical,VerticalFlex = Enum.UIFlexAlignment.SpaceEvenly,HorizontalAlignment = Enum.HorizontalAlignment.Center})menuLayoutRule.Parent = menuFrameRule -- ตั้งกฎของเฟรมเมนูเป็นพ่อ-- กฎปุ่มlocal buttonRule = Instance.new("StyleRule")buttonRule.Selector = "> TextButton"buttonRule:SetProperties({BackgroundColor3 = Color3.fromHex("335FFF"),TextColor3 = Color3.fromHex("E1E1E1"),Size = UDim2.new(0.8, 0, 0, 40),BorderSizePixel = 0})buttonRule.Parent = menuFrameRule -- ตั้งกฎของเฟรมเมนูเป็นพ่อ-- กฎปุ่ม Hoverlocal buttonHoverRule = Instance.new("StyleRule")buttonHoverRule.Selector = ":Hover"buttonHoverRule:SetProperties({AutoButtonColor = false,BackgroundTransparency = 0.5,TextTransparency = 0.5})buttonHoverRule.Parent = buttonRule -- ตั้งกฎปุ่มเป็นพ่อ-- สร้างเฟรมพ่อlocal menuFrame = Instance.new("Frame")menuFrame.Name = "MenuFrame"menuFrame.Parent = screenGui-- สร้างปุ่มภายในเฟรมlocal button1 = Instance.new("TextButton")button1.Text = "Charms"button1.Parent = menuFramelocal button2 = Instance.new("TextButton")button2.Text = "Mana"button2.Parent = menuFramelocal button3 = Instance.new("TextButton")button3.Text = "Scrolls"button3.Parent = menuFrame