How to let a component occupy 100% of the remaining height

Asked

Viewed 1,333 times

-1

I’m trying to get this pink rectangle to occupy the rest of the screen, more specifically... responsive height, tried some shapes, but unsuccessfully. How to proceed?

OBS: I’m using the Styled-Components

inserir a descrição da imagem aqui

index js.

return(
    <Container showsVerticalScrollIndicator={false}>
      <ContainerValue>

      </ContainerValue>

        <ContainerRun>
          <Output>

          </Output>

          <Output>
            
          </Output>

          <ContainerButton>
            <ButtonInit>

            </ButtonInit>
          </ContainerButton>
          
        </ContainerRun>
    </Container>
  );

Styles.js

export const Container = styled.ScrollView`
  flex: 100%;
  background-color: ${colors.greyRegular};
  padding-left: 15px;
  padding-right: 15px;
`;

export const ContainerValue = styled.View`
  width: 55%;
  height: 140px;
  border-radius: 10px;
  align-self: center;
  margin-top: 25px;
  background-color: ${colors.white};
`;

export const ContainerRun = styled.View`
  width: 100%;
  border-radius: 10px;
  align-self: center;
  margin-top: 50px;
  margin-bottom: 15px;
  background-color: pink;
`;

export const Output = styled.View`
  width: 90%;
  height: 50px;
  border-radius: 10px;
  align-self: center;
  margin-top: 50px;
  background-color: ${colors.greyRegular};
`;
export const ContainerButton = styled.View`
  flex: 100%;
  justify-content: flex-end; 
`;

export const ButtonInit = styled.TouchableOpacity`
  width: 250px;
  height: 80px;
  border-radius: 10px;
  align-self: center;
  margin-bottom: 15px;
  background-color: ${colors.yellowRegular};

`;

1 answer

1


For the rectangle to occupy a space defined by height: 100% or any other value, it is necessary that the block containing it also has a defined height, whatever it is. Would that be:

<content-wrapper>
  <box></box>
</content-wrapper>

And the style

content-wrapper { height: 400px; }
box { height: 100% }

You can also use Flex to make sure the height works well.

.container {
    display: flex;
    flex-direction: column;
    background-color: #ddd;
    padding-left: 15px;
    padding-right: 15px;
    height:100vh;
}

.containerValue {
    width: 55%;
    height: 140px;
    border-radius: 10px;
    align-self: center;
    margin-top: 25px;
    background-color: white;
}

.containerRun {
    flex-grow: 1;
    width: 100%;
    border-radius: 10px;
    align-self: center;
    margin-top: 50px;
    margin-bottom: 15px;
    background-color: pink;
}
    <div class="container">
        <div class="containerValue">
  
        </div>
  
        <div class="containerRun">
            
        </div>
    </div>

I converted it to pure html/css and only touched the part that modifies the height of the boxes.

  • First of all thank you for dedicating a little of your time to answer me, I made the changes and worked in parts, the block is responsive but does not occupy the rest of the screen. =(

  • I found that the problem was in the container base, because I am using Scrollview and it has a different functioning

  • 1

    Ah, come on! : ) I don’t know about React, but that part of adjusting the height of a block is a bit boring. You can use vh which corresponds to 1% of the height of the display window. In case the device viewport. This way, you will set a height that will cover the entire available visible area, as used in .container. So you will adjust. These links can help you: https://stackoverflow.com/questions/21224411/css-calc-viewport-units-workaround or https://developer.mozilla.org/en-US/docs/Luilding_blocks/Values_and_units

Browser other questions tagged

You are not signed in. Login or sign up in order to post.