StyleRule

Show Deprecated

Defines style properties which override properties on the instances affected by the Selector property.

Summary

Properties

  • Read Parallel

    A number that determines how properties of the StyleRule apply relative to the same properties in other StyleRules. Higher priority values take precedence over lower.

  • Read Parallel

    A string specifying which instances the StyleRule should affect.

  • Read Only
    Not Replicated
    Read Parallel

    A read-only string that displays errors from the Selector property.

Methods

Methods inherited from StyleBase

Events

Events inherited from StyleBase

Properties

Priority

Read Parallel

A number that determines how properties of the StyleRule apply relative to the same properties in other StyleRules. Higher priority values take precedence over lower. For example, if a StyleRule with a priority of 10 has an AnchorPoint property of 1, 0, it will take precedence over lower-priority StyleRules with AnchorPoint properties.

Selector

Read Parallel

A string specifying which instances the StyleRule should affect. This can be a mix of selectors and combinators to match characteristics such as the class name, instance name, and hierarchy relationships.

For example, ".Container > ImageLabel.BlueOnHover:Hover" effectively means the style rule overrides every ImageLabel that's a child of an instance tagged with Container (.Container > ImageLabel) and is tagged with BlueOnHover (.BlueOnHover) and is in the Enum.GuiState.Hover state (:Hover).

Selectors
SelectorDescriptionExamples
[class]Matches instances of a GuiObject or UIComponent class."Frame"
"ImageButton"
"UICorner"
.[tag]Matches instances tagged with a CollectionService tag.".Container"
".BlueOnHover"
#[name]Matches instances of a specific Instance.Name."#ModalFrame"
"#CloseButton"
:[state]Matches instances currently in a Enum.GuiState.":Hover"
Combinators
CombinatorDescriptionExamples
>Matches instances that are direct children of the previous filter matches."Frame > .Inventory"
>>Matches instances that are descendants of the previous filter matches."ImageButton >> .BlueOnHover"
,Specifies a list of multiple independent selectors for the style rule."Frame.TagA, TextLabel.TagA"
::Creates a phantom UIComponent instance under the previous filter matches and applies the style rule's properties to it."Frame::UICorner"

Code Samples

The following example shows how to define a class selector which targets all TextButton instances and styles them with blue background and light grey text. To test, paste the code into a LocalScript which is a child of a ScreenGui located inside the StarterGui container.

UI Class Selector

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local screenGui = script.Parent
local coreSheet = Instance.new("StyleSheet")
coreSheet.Parent = ReplicatedStorage
local styleLink = Instance.new("StyleLink")
styleLink.StyleSheet = coreSheet
styleLink.Parent = screenGui
local rule = Instance.new("StyleRule")
rule.Parent = coreSheet
-- Class selector
rule.Selector = "TextButton"
-- Set rule properties
rule: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 = "Main Menu"
button.Parent = screenGui

The following example shows how to define a tag selector which utilizes tags applied through CollectionService to target a TextButton tagged with ButtonPrimary. To test, paste the code into a LocalScript which is a child of a ScreenGui located inside the StarterGui container.

UI Tag Selector

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local screenGui = script.Parent
local coreSheet = Instance.new("StyleSheet")
coreSheet.Parent = ReplicatedStorage
local styleLink = Instance.new("StyleLink")
styleLink.StyleSheet = coreSheet
styleLink.Parent = screenGui
local rule = Instance.new("StyleRule")
rule.Parent = coreSheet
-- Tag selector
rule.Selector = ".ButtonPrimary"
-- Set rule properties
rule:SetProperties({
["BackgroundColor3"] = Color3.fromHex("FF0099"),
["TextColor3"] = Color3.fromHex("E1E1E1"),
["Size"] = UDim2.new(0.15, 0, 0, 40),
["BorderSizePixel"] = 0
})
local button = Instance.new("TextButton")
button.Text = "Main Menu"
button.Parent = screenGui
-- Apply tag to button
CollectionService:AddTag(button, "ButtonPrimary")

The following example shows how to define a UI modifier selector which applies a phantom UICorner instance to a Frame tagged with RoundedCorner20. To test, paste the code into a LocalScript which is a child of a ScreenGui located inside the StarterGui container.

UI Modifier Selector

local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local screenGui = script.Parent
local coreSheet = Instance.new("StyleSheet")
coreSheet.Parent = ReplicatedStorage
local styleLink = Instance.new("StyleLink")
styleLink.StyleSheet = coreSheet
styleLink.Parent = screenGui
local rule = Instance.new("StyleRule")
rule.Parent = coreSheet
-- UI component selector
rule.Selector = "Frame.RoundedCorner20::UICorner"
-- Set rule property
rule:SetProperty("CornerRadius", UDim.new(0, 20))
-- Create frame
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0.4, 0, 0.2, 0)
frame.Parent = screenGui
-- Apply tag to frame
CollectionService:AddTag(frame, "RoundedCorner20")

SelectorError

Read Only
Not Replicated
Read Parallel

A read-only string that displays errors from the Selector property such as syntax errors, unsupported class types, etc.

Methods

GetProperties

Returns a dictionary of key-value pairs describing the properties of the StyleRule, for example:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Get reference to style rule
local frameRule = coreSheet.Frame
local props = frameRule:GetProperties()
print(props)
--[[
{
["AnchorPoint"] = 0.5, 0,
["BackgroundColor3"] = 1, 0, 0.25
}
]]

Returns

Dictionary of key-value pairs describing the properties of the StyleRule.

GetProperty

Variant

Returns the value of a specific property in the StyleRule.


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Get reference to style rule
local frameRule = coreSheet.Frame
local prop = frameRule:GetProperty("AnchorPoint")
print(prop) --> 0.5, 0

Parameters

name: string

String name of the property, for example "AnchorPoint" or "BackgroundColor3".

Default Value: ""

Returns

Variant

Value of the property.

SetProperties

()

Similar to SetProperty() but lets you declare and set multiple properties of the StyleRule at once. Each assignment should be a valid property of the affected GuiObject or UIComponent (UICorner, UIGradient, etc.), and each assigned value should match its property's value type, for example Vector2 for AnchorPoint or Color3 for BackgroundColor3.

Attempts to assign invalid property names such as "AnchorPt" or "BkColor" will silently fail. Type mismatches such as CFrame for AnchorPoint or UDim2 for BackgroundColor3 will also fail and an error will appear in the Output window.

To set/update just one property of a StyleRule, see SetProperty().


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Get reference to style rule
local frameRule = coreSheet.Frame
-- Set rule properties
frameRule:SetProperties({
["AnchorPoint"] = Vector2.new(0.5, 0),
["BackgroundColor3"] = Color3.new(1, 0, 0.25)
})

Note that you can assign tokens as property values through the $ prefix:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- Set tokens (attributes) on tokens sheet
tokensSheet:SetAttribute("TopCenterAnchor", Vector2.new(0.5, 0))
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- Get reference to style rule
local frameRule = coreSheet.Frame
-- Set rule properties
frameRule:SetProperties({
["AnchorPoint"] = "$TopCenterAnchor",
["BackgroundColor3"] = "$MainBackgroundColor"
})

Parameters

styleProperties: Dictionary

Dictionary of key-value pairs defining the properties to set.

Default Value: ""

Returns

()

SetProperty

()

Sets a new property (or modifies an existing property) for the StyleRule. The name parameter should be a valid property of the affected GuiObject or UIComponent (UICorner, UIGradient, etc.), and the assigned value should match the property's value type, for example Vector2 for AnchorPoint or Color3 for BackgroundColor3.

Attempts to assign invalid property names such as "AnchorPt" or "BkColor" will silently fail. Type mismatches such as CFrame for AnchorPoint or UDim2 for BackgroundColor3 will also fail and an error will appear in the Output window.

To set multiple properties for a StyleRule at once, see SetProperties().


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Get reference to style rule
local frameRule = coreSheet.Frame
-- Set rule property
frameRule:SetProperty("BackgroundColor3", Color3.new(1, 0, 0.25))

Note that you can assign tokens as property values through the $ prefix:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local tokensSheet = ReplicatedStorage:FindFirstChild("Tokens")
-- Set new token (attribute) on tokens sheet
tokensSheet:SetAttribute("MainBackgroundColor", Color3.new(0.2, 0.2, 0.3))
-- Get reference to style rule
local frameRule = coreSheet.Frame
-- Set rule property to use the token as its value
frameRule:SetProperty("BackgroundColor3, "$MainBackgroundColor")

Parameters

name: string

Property name to set, for example "BackgroundColor3".

Default Value: ""
value: Variant

Property value to set, for example Color3.new(1, 0, 0.25).

Default Value: ""

Returns

()

Events