---
title: "size"
description: "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."
type: "modifier"
---

<div class='type'>Compose Modifier</div>

<a id='references'></a>
<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun Modifier.size(size: Dp) =
    this.then(
        SizeElement(
            minWidth = size,
            maxWidth = size,
            minHeight = size,
            maxHeight = size,
            enforceIncoming = true,
            inspectorInfo =
                debugInspectorInfo {
                    name = "size"
                    value = size
                },
        )
    )
```


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.



<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun Modifier.size(width: Dp, height: Dp) =
    this.then(
        SizeElement(
            minWidth = width,
            maxWidth = width,
            minHeight = height,
            maxHeight = height,
            enforceIncoming = true,
            inspectorInfo =
                debugInspectorInfo {
                    name = "size"
                    properties["width"] = width
                    properties["height"] = height
                },
        )
    )
```


Declare the preferred size of the content to be exactly `width`dp by `height`dp. 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.



<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun Modifier.size(size: DpSize) = size(size.width, size.height)
```


Declare the preferred size of the content to be exactly `size`. 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.



## Code Examples
### SimpleSizeModifier
```kotlin
@Composable
fun SimpleSizeModifier() {
    Box { Box(Modifier.size(100.dp, 100.dp).background(Color.Red)) }
}
```
### SimpleSizeModifierWithDpSize
```kotlin
@Composable
fun SimpleSizeModifierWithDpSize() {
    Box { Box(Modifier.size(DpSize(100.dp, 100.dp)).background(Color.Red)) }
}
```

