A Modifier in Jetpack Compose is an ordered chain of instructions that decorates or configures a composable: its size, padding, background, click behaviour, alignment and more. You pass it through the modifier parameter and build it by chaining calls such as Modifier.padding(16.dp).background(Color.Gray). The order of the calls changes the result. This guide explains the compose modifier basics with examples and rules for writing your own composables.
Almost every built-in composable accepts a modifier, so learning the common ones pays off quickly.
Modifier is an interface. Modifier itself (the companion object) is the empty starting point, and each function you call returns a new modifier with one more element added. You apply it to a composable through its modifier parameter.
Modifiers are immutable. You can store one in a variable and reuse it, but you cannot change it afterwards.
Each modifier wraps the content that comes after it, from the outside in. Compare these two:
In the first line the background is applied first, so it covers the full area, and the padding then moves the content inward. In the second line the padding creates space first, and the background is drawn only behind the content. The same idea applies to clicks: Modifier.padding(16.dp).clickable { } leaves the padding outside the click area, while Modifier.clickable { }.padding(16.dp) makes the padding clickable too.
A useful habit is to read a modifier chain from left to right as "outer to inner".
| Modifier | What it does |
|---|---|
padding(...) | Adds space around the content, in dp |
size(...), width(...), height(...) | Sets a fixed size |
fillMaxWidth(), fillMaxHeight(), fillMaxSize() | Fills the available space, optionally by a fraction |
background(...) | Draws a colour or brush behind the content |
border(...) | Draws an outline |
clip(...) | Clips content to a shape such as RoundedCornerShape |
clickable { } | Makes the element respond to taps, with a ripple |
weight(...) | Shares remaining space inside a Row or Column |
alpha(...) | Sets transparency |
verticalScroll(...) | Makes content scrollable |
The modifiers weight and align are available only inside a scope such as RowScope, ColumnScope or BoxScope, because they need a parent to cooperate with.
Follow the convention from the official API guidelines: every composable that emits UI should take a modifier: Modifier = Modifier parameter, place it as the first optional parameter, and apply it to the root element of the composable.
The caller supplies layout decisions (padding, size, alignment), and the composable adds its own styling by chaining onto the received modifier. Always start from the parameter, not from a fresh Modifier, or the caller's modifier is lost. Apply it only once, to the outermost element.
Because chains are cheap immutable objects, creating them during composition is fine. For a chain that never changes, you can keep it in a top-level val or remember a computed one.
When you need your own behaviour, build on existing modifiers with Modifier.then(...) or write a composable extension using composed. In current Compose, composed is discouraged for performance reasons. Prefer the Modifier.Node API for custom modifiers, or combine existing modifiers in a plain extension function:
Most apps never need Modifier.Node, because a plain extension function covers common cases. Also note that some modifiers read state, for example Modifier.offset { } with a lambda reads state during layout and avoids recomposition, which is useful for animation.
Parameters configure a specific composable, such as the colour of a Text. A modifier works on any composable and handles general concerns such as size, padding, click handling and background.
Probably the modifier order. A background before padding colours the padded area, and a background after padding does not. Reorder the chain and check the preview.
Every composable that emits UI should, with a default of Modifier and applied to its root. It makes the composable reusable in different layouts.
Yes. Modifiers are immutable, so a stored chain can be applied to many composables safely.
A Modifier is an immutable, ordered chain that adds layout, drawing and input behaviour to a composable. Order matters because each element wraps what follows it. Learn padding, size, fillMaxWidth, background, clip, clickable and weight first, and give your own composables a modifier: Modifier = Modifier parameter applied to their root element.