41

I would like the size of my boxes not to be changed by margin and padding.

void42
  • 921
  • 2
  • 10
  • 14

5 Answers5

37

React Native styles all views as if box-sizing: border-box is set. See this code: https://github.com/facebook/react-native/blob/5aa1fb3ff326a429e33a443576da866f2a63c20c/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewBackgroundDrawable.java#L632

Rashi Abramson
  • 1,127
  • 8
  • 16
  • 4
    Although, this behaves very weird, like an `absolute` child with `width: "100%", height: "100%"` will appear after the `padding` values – Biskrem Muhammad May 11 '21 at 12:25
4

As mentioned in another answer, In React Native, everything is treated as if as if box-sizing: border-box is set. A workaround to simulate css content-box is to wrap your component in a container View, and add your border and padding to that View.

ut9081
  • 787
  • 6
  • 11
0

react native don't have an option for

 boxSixing: "border-box"

what you can do in your styling is this

<View style={ {
    flex: 1,
    alignContent: "center",
    justifyContent: "center",
}}>
    <View style={ {
        flex: 1,
        width: "95%" // or width of the box - intended margin
    }}>
        ***chi;dren goes here***
    <View/>
<View/>

Any component that goes into that view will be at the center With a margin of 5% or whats is specified, which solves the issue for now till react-native provides a better solution

Chukwuemeka Maduekwe
  • 6,687
  • 5
  • 44
  • 67
-1

Let O (for outer) = (top, left, bottom, right) be the rectangle that represents the size * and position of a view V. Since the box-sizing of all React Native views is border-box, any * border of V will render inside O.

Naved Khan
  • 1,699
  • 16
  • 13
  • This text is copied from the link that Rashi posted above, but doesn't give any context that this quote is taken from the repo's source code. It may be better as a comment on the previous answer rather than a new one. – Eosis Jun 19 '22 at 08:28
-13

you can use resizeMode: 'cover' or 'stretch' or 'contain'

Kazuki Kimoto
  • 63
  • 1
  • 3
  • 10