New Compose Multiplatform components arrived on Composables UICheck it out →

Modifier in Compose Foundation Layout

size

Common

Declare the preferred size of the content to be exactly [size]dp square. The incoming measurement [Constraints] may override this value, forcing the content to be either smaller or larger.

For a modifier that sets the size of the content regardless of the incoming constraints, see [Modifier.requiredSize]. See [width] or [height] to set width or height alone. See [widthIn], [heightIn] or [sizeIn] to set a preferred size range.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation-layout:1.7.0-beta04")
}

Overloads

@Stable
fun Modifier.size(size: Dp)
@Stable
fun Modifier.size(width: Dp, height: Dp)
@Stable
fun Modifier.size(size: DpSize)

Code Examples

SimpleSizeModifier

@Composable
@Sampled
fun SimpleSizeModifier() {
    Box {
        Box(Modifier.size(100.dp, 100.dp).background(Color.Red))
    }
}

SimpleSizeModifierWithDpSize

@Composable
@Sampled
fun SimpleSizeModifierWithDpSize() {
    Box {
        Box(Modifier.size(DpSize(100.dp, 100.dp)).background(Color.Red))
    }
}