---
title: "Dp"
description: "Dimension value representing device-independent pixels (dp)."
type: "class"
lastmod: "2026-07-30T07:35:58.218680Z"
---
## API Reference

> Source set: Common

```kotlin
value class Dp(val value: Float) : Comparable<Dp>
```

Dimension value representing device-independent pixels (dp). Component APIs specify their
dimensions such as line thickness in DP with Dp objects. Hairline (1 pixel) thickness may be
specified with `Hairline`, a dimension that take up no space. Dp are normally defined using [dp](/jetpack-compose/androidx.compose.ui/ui-unit/properties/dp),
which can be applied to `Int`, `Double`, and `Float`.

Drawing and Layout are done in pixels. To retrieve the pixel size of a Dp, use [Density.toPx](/jetpack-compose/androidx.compose.ui/ui-unit/interfaces/Density):

## Functions

### plus

```kotlin
inline operator fun plus(other: Dp) = Dp(this.value + other.value)
```

Add two [Dp](/jetpack-compose/androidx.compose.ui/ui-unit/classes/Dp)s together.

### minus

```kotlin
inline operator fun minus(other: Dp) = Dp(this.value - other.value)
```

Subtract a Dp from another one.

### unaryMinus

```kotlin
inline operator fun unaryMinus() = Dp(-value)
```

This is the same as multiplying the Dp by -1.0.

### div

```kotlin
inline operator fun div(other: Float): Dp
```

Divide a Dp by a scalar.

### div

```kotlin
inline operator fun div(other: Int): Dp
```

### div

```kotlin
inline operator fun div(other: Dp): Float
```

Divide by another Dp to get a scalar.

### times

```kotlin
inline operator fun times(other: Float): Dp
```

Multiply a Dp by a scalar.

### times

```kotlin
inline operator fun times(other: Int): Dp
```

## Companion Object

#### Properties

> Source set: Common

```kotlin
val Hairline = Dp(0f)
```

A dimension used to represent a hairline drawing element. Hairline elements take up no
space, but will draw a single pixel, independent of the device's resolution and density.

> Source set: Common

```kotlin
val Infinity = Dp(Float.POSITIVE_INFINITY)
```

Infinite dp dimension.

> Source set: Common

```kotlin
val Unspecified = Dp(Float.NaN)
```

Constant that means unspecified Dp. Instead of comparing a [Dp](/jetpack-compose/androidx.compose.ui/ui-unit/classes/Dp) value to this constant,
consider using [isSpecified](/jetpack-compose/androidx.compose.ui/ui-unit/properties/isSpecified) and [isUnspecified](/jetpack-compose/androidx.compose.ui/ui-unit/properties/isUnspecified) instead.

## Code Examples

### DpSample
```kotlin
@Composable
fun DpSample() {
    Box(
        Modifier.padding(
            10.dp, // Int
            10f.dp, // Float
            20.0.dp, // Double
            10.dp,
        )
    )
}
```

### ToPxSample
```kotlin
@Composable
fun ToPxSample() {
    val lineThickness = 6.dp
    Canvas(Modifier.fillMaxSize()) {
        val lineThicknessPx = lineThickness.toPx()
        inset(lineThicknessPx / 2) { drawRect(Color.Red, style = Stroke(lineThicknessPx)) }
    }
}
```
