Positioning a component halfway between its top and bottom components

Currently, I am working on creating a profile page UI where the profile data such as name and bio container should remain 50% at the bottom of its top component, which is currently the cover picture.

This is the React Native code I have so far:

<View style={styles.container}>
        <View style={styles.coverContainer}>

        </View>
        <View style={styles.profileDataContainer}>

        </View>
        <View style={styles.intrestsContainer}>

        </View>
        <View style={styles.topicsContainer}>

        </View>
      </View>

Here are the current styles being applied:

const styles = StyleSheet.create({
  container: {

  },
  coverContainer: {
      height : 200,
      backgroundColor : 'blue'
  },
  profileDataContainer: {
      marginHorizontal : 30,
      backgroundColor: 'white',
      height : 200,
      shadowColor: "#e5e5e5",
      shadowOffset: {
        width: 0,
        height: 2
      },
      shadowRadius: 2,
      shadowOpacity: 1.0,
      elevation : 4,
      marginTop : -100,
  }

});

I have set marginTop to -100 in order to achieve the desired structure.

https://i.sstatic.net/urHMV.png

After setting the marginTop to -100, this is how it looks like now. However, it's not responsive as increasing the size of the white block causes it to shift from the center position.

I need assistance on how to make it responsive :(

Answer №1

To achieve this layout, consider using percentage values instead of logical pixels as demonstrated in the following code snippet:

const WindowHeight = Dimensions.get('window').height;

export default class MyApp extends Component {

  render() {
    return (
      <View style={styles.container}>
      <View style={styles.headerBackground}/>
      <View style={styles.header}/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: 'grey',
  },
  headerBackground: {
    height: '30%',
    width: '100%',
    backgroundColor : 'blue'
  },
  header: {
    height: '30%',
    width: '80%',
    marginTop: WindowHeight * -0.15,
    backgroundColor : 'white'
  }
});

The blue background and white header are set to 30% height of the window, with a margin-top of -15%. It's important to use Dimensions since using percentages would refer to the width otherwise.

For a live demo, you can access my example on Snack:

Answer №2

If you want to organize your profileDataContainer within your coverContainer and position it absolutely with top 50%, you can achieve this by adapting your code for React Native...

For example:

.coverContainer{
    height: 200px;
    background-color: blue;
    position: relative;
    width: 100%;
}
.profileDataContainer {
    position: absolute;
    right: 10%;
    left: 10%;
    display: block;
    top: 50%;
    background-color: red;
    height: 200px;
}
<div class="coverContainer"><div class="profileDataContainer"></div></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Launching the date/time selection tool through code in the Material UI framework

I'm currently working on a project that involves React and Material Design. I decided to utilize Material UI for its sleek Dat- and Time Pickers components. In my application, I have to set Start and End Times and Dates in a specific order. To stream ...

Can you explain how to pause the .setInterval() function when hovering over it and clicking on the interior content simultaneously?

I have been working on a slideshow that involves building elements into an array, modifying the DOM, and using items from the array to fade in and out on a timer. While I have made progress, there are still a few things I would like to accomplish but I am ...

What is the method for creating a checkbox in CSS that includes a minus symbol inside of it?

Can you create a checkbox with a minus "-" symbol inside using just html and css? I attempted to achieve this with JavaScript by using: document.getElementsByTagName("input")[0].indeterminate = true; However, the requirement is to accomplish this using ...

Utilizing Bootstrap to create a list inside a <dd> element

Hello Universe! I'm on a quest to discover an elegant solution for presenting multiple lines of information within a tag. Right now, I have the following setup:</p> <pre><code><dl class="row"> <dt class="col-2"> ...

Positioning and resizing elements based on varying screen sizes and levels of zoom

I recently designed a basic webpage using HTML with two main elements - a chat window for user interaction and a chart.js plot. Here is a snippet of the HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

"Repetitive" elements arranged horizontally

My goal is to create a looped row of elements, similar to this design: https://i.sstatic.net/7cC2z.png This row should function like a carousel where clicking the "Next" button changes the current element and positions it in the center of the row. I envi ...

How to pass information to a component when it is clicked in React.js

We currently have 2 distinct components: Test1 and Test2. I'm interested in finding out how we can transfer data from the Test1 component to the Test2 component when a click event occurs. One approach could involve using a function similar to this: ...

How to position JQuery UI tabs at the bottom

Within my page, I have implemented JQuery tabs. The following image illustrates the issue I am encountering: Problem1: The JQuery tab div section is currently centered, but I want it to occupy 100% of the bottom section (highlighted in green in the image) ...

Function anomalies triggered by delayed setState updates with React Hooks

Creating a Quiz app with React involves fetching questions as an array and managing the following states: An array containing all question details - statement, options, chosen answer, status (answered, marked for review, unvisited); An object holding info ...

Nesting divs that soar within a contained div (not spanning the entire page)

I need the div elements to move within another div container instead of flying over the entire page. What changes do I need to make in the code to achieve this? $(document).ready(function() { $('.balloon').each(animateDiv); }); function ma ...

When using npm start, an error related to the babel-eslint version is being displayed

After executing the npm start command to begin the server for my react application established with npx create-react-app my-app, I encountered a persistent error. I have attempted all the recommended solutions without success. Can anyone provide guidance ...

React: Modifying state does not update useState array

The state of the array does not change when the state change method is called : const [arrayOfDocuments, setArrayOfDocuments] = useState([]); I have tried : setArrayOfDocuments(...[]); or setArrayOfDocuments([]); Where I use my method : const pushToArr ...

Having trouble with the find method when trying to use it with the transform

In my code, I have three div elements with different values of the transform property assigned to them. I store these elements in a variable using the getElementsByClassName method and then try to find the element where the value of the transform property ...

GSAP TimelineLite is not being loaded correctly by NPM and Webpack

We are currently in the process of refining and optimizing a production application by cleaning up and organizing dependencies. Within our React component, we utilize GSAP for transitions, specifically only requiring the TimelineLite library. Since our an ...

Unit testing in Jest using Luxon: Is there a way to mock the .setZone('local') function?

I have a straightforward date component that I've been working on: import { DateTime } from 'luxon'; export const DateComponent = ({ item, itemKey }) => { const isoDateString = item[itemKey]; if (!isoDateString) { return ...

Is there a way to automatically generate additional text fields based on the user's input?

Is there a way to dynamically add multiple text fields for color and choose design based on user input? For instance, if the user enters 5 in the quantity field, how can I replicate the color and choose design fields 5 times? Additionally, how should I st ...

What is the best way to split a Bootstrap navbar into two separate rows?

I'm struggling with breaking the navbar to have the navbar-brand in one row and the collapse menu in another. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72101d1d0601060013 ...

Content sliding to the left due to modal prompt

I've searched through various questions and answers but haven't been able to resolve this issue. There's a modal on my page that is causing the content to shift slightly to the left. I've created a sample fiddle, although it doesn&apo ...

Chrome is currently experiencing a problem with embedded fonts, causing the bottom of the customer's font to be

Greetings, I am encountering a font-face issue specifically in Chrome on Windows. Below is the code snippet causing the problem: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta ...

How can I swap a string for a symbol in JavaScript?

Is there a way to convert the text @abc some text here to <a href="some_url">@abc</a> some text here using JavaScript? Are there any libraries that can help with this task? ...