{Flex: 1} not working with relative positioning

Related searches

I want the second View to be relatively positioned at -50 below the first View, but the second View does not extend completely at the bottom.

import React from 'react';
import {View, Text} from 'react-native';

export const OverlayStorybook = () => {
  return (
    <View style={{flex: 1}}>
      <View style={{height: 100, backgroundColor: 'red'}}>
        <Text>OverlayStorybook</Text>
      </View>
      <View style={{flex: 1, backgroundColor: 'blue', top: 0}} />
    </View>
  );
};

import React from 'react';
import {View, Text} from 'react-native';

export const OverlayStorybook = () => {
  return (
    <View style={{flex: 1}}>
      <View style={{height: 100, backgroundColor: 'red'}}>
        <Text>OverlayStorybook</Text>
      </View>
      <View style={{flex: 1, backgroundColor: 'blue', top: -50}} />
    </View>
  );
};

You can see at the bottom there are 50 pixels that are white.


This is one option I figured out: https://snack.expo.io/@zvona/full-flex

So removing flex and putting absolute positioning, then adding 50 (instead of subtracting it). Of course that "50" depends on the position you want to fit it in. So basically it's error prone if height of header element is changed.

<View style={{position: 'absolute', backgroundColor: 'rgba(32, 32, 128, 0.8)', top: 50, left: 0, right: 0, bottom: 0}} />
</View>

Flexbox and absolute positioning, When you apply position: absolute; to things (and nothing else), they kinda just stay have any specific positioning instructions other than to not affect anything else. There are a lot of similarities between flexbox and grid, starting with the fact So markup like: 1 2 3 4 5 6 Which would end up like: 1 4… The combination of justify-content: flex-end with the margin-right: auto pushes the content out of the wrapper/container. First you try to add the IE specific properties, like -ms-justify-content: flex-end; but it doesn’t work. You find out that there’s another old syntax -ms-flex-pack: (flex-)end which also doesn’t work.


Try below snippets

<View style={{ flex: 1, position: "relative" }}>
          <View
            style={{
              height: 100,
              backgroundColor: "red",
              position: "absolute",
              top: 0,
              zIndex: 1,
              width: "100%"
            }}
          >
            <Text>OverlayStorybook</Text>
          </View>
          <View
            style={{
              flex: 1,
              border: "1px solid green",
              marginTop: 50,
              zIndex: 2
            }}
          />
        </View>

Absolutely positioned flexbox doesn't expand to fit contents, Viewing 13 posts - 1 through 13 (of 13 total) In Chrome, for example, it will only be as wide as the widest child element and as tall as I'm pretty sure that display :flex and position:absolute are incompatible in this respect. The flex property is a shorthand property for: flex-grow; flex-shrink; flex-basis; The flex property sets the flexible length on flexible items. Note: If the element is not a flexible item, the flex property has no effect.


Well I have found the answer to my question by myself. Just needed to wrap the second View inside a View and put position absolute.

import React from 'react';
import {View, Text} from 'react-native';

export const OverlayStorybook = () => {
  return (
    <View style={{flex: 1}}>
      <View style={{height: 100, backgroundColor: 'red'}}>
        <Text>OverlayStorybook</Text>
      </View>
      <View style={{flex: 1}}>
        <View
          style={{
            flex: 1,
            position: 'absolute',
            backgroundColor: 'blue',
            top: -50,
            left: 0,
            right: 0,
            bottom: 0,
          }}
        />
      </View>
    </View>
  );
};

Flexbox Gets New Behavior for absolute-positioned Children, Flexbox Gets New Behavior for absolute-positioned Children For an example, let's apply some positioning behaviors to the following HTML. display: flex; The flex property is a shorthand property for the flex-grow, flex-shrink, and flex-basis properties. Example Make the third flex item not growable (0), not shrinkable (0), and with an initial length of 200 pixels:


5. CSS layout: tricks and layout techniques, The trick is simple: place the main content inside a flex-grow: 1 flex item, and place Setting position: relative does not affect the positioning of elements in normal For example, if you actually want an element to overflow - such as the image� You could go one step further and set position: relative and a lower z-index on the cat elements, just to be extra safe..content__block { position: relative; z-index: 2; } .cat-top, .cat-bottom { position: relative; z-index: 1; } In my opinion, doing this will solve most, if not all of the more basic z-index issues. Now, let’s move on to our


Z index isn't working in flexbox - HTML-CSS, I was having a similar problem before but ad… 41, 1); display: flex; flex- direction: row; flex-wrap: wrap; border-radius: 50%; border-style: solid; border- width: z-index only works if you apply position: (relative/fixed/absolute). Outside of flex containers using auto positioning values behaves this way, so this is new behavior added in flex. This behavior inside flex containers removed a very important method for positioning elements in CSS, allowing elements to exist outside of page flow but preserve their calculated positions.


Note: This value was not present in the initial release of Flexible Box Layout, and thus some older implementations will not support it. The equivalent effect can be had by using auto together with a main size ( width or height ) of auto .