skip to Main Content

I am making a react native application in which there is a left and right section.

The left section consists of flex:0.7 and right side section consists of flex:0.2.

Inside the left section I have a container inside which there is a ImageBackground which looks like a circuit skeleton

enter image description here

and inside that I am in the need to place sub components in the respective position.

Expected Result:

enter image description here

Things I have tried:

Pure HTML and CSS way: (Working as expected)

.container {
  display: flex;
  flex: 1;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.leftSection {
  flex: 0.7;
}

.rightSection {
  flex: 0.2;
  background-color: #ccc;
}

.bgContainer {
  background-repeat: no-repeat;
  position: relative;
  margin: 0 auto;
}

.bg-img {
  display: block;
  width: 100%;
}

.coil {
  position: absolute;
  top: 49.55%;
  left: 24.3%;
  width: 17.4418605%;
}

.evaporator {
  position: absolute;
  top: 7.25%;
  left: 54.5%;
  width: 11.627907%;
}

.compressor {
  position: absolute;
  top: 53.15%;
  left: 59.2%;
  width: 13.0813953%;
}

.component img {
  display: block;
  width: 100%;
}
<div class="container">
  <div class="leftSection">
    <div class="bgContainer">
      <img src="https://i.stack.imgur.com/AfygH.png" class="bg-img" />
      <div class="component coil">
        <img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
      </div>
      <div class="component evaporator">
        <img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
      </div>
      <div class="component compressor">
        <img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
      </div>
    </div>
  </div>
  <div class="rightSection">
    Right Section
  </div>
</div>

But as I am doing this in a react native application, I have tried changing this into a react native way like,

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

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  leftSection: {
    flex: 0.7,
  },
  rightSection: {
    flex: 0.2,
    backgroundColor: '#ccc',
  },
  bgContainer: {
    position: 'relative',
    margin: 0,
  },
  bgImg: {
    width: '100%',
  },
  coil: {
    position: 'absolute',
    top: '49.55%',
    left: '24.3%',
    width: '17.4418605%',
  },
  evaporator: {
    position: 'absolute',
    top: '7.25%',
    left: '54.5%',
    width: '11.627907%',
  },
  compressor: {
    position: 'absolute',
    top: '53.15%',
    left: '59.2%',
    width: '13.0813953%',
  },
  componentImg: {
    width: '100%',
  },
});

const App = () => {
  return (
    <View style={styles.container}>
      <View style={styles.leftSection}>
        <View style={styles.bgContainer}>
          <ImageBackground
            source={{ uri: 'https://i.stack.imgur.com/AfygH.png' }}
            style={styles.bgImg}
          >
          <View style={styles.coil}>
            <Image
              source={{ uri: 'https://i.stack.imgur.com/SKUms.png' }}
              style={styles.componentImg}
            />
          </View>
          <View style={styles.evaporator}>
            <Image
              source={{ uri: 'https://i.stack.imgur.com/spv58.png' }}
              style={styles.componentImg}
            />
          </View>
          <View style={styles.compressor}>
            <Image
              source={{ uri: 'https://i.stack.imgur.com/fzSaH.png' }}
              style={styles.componentImg}
            />
          </View>
          </ImageBackground>
        </View>
      </View>
      <View style={styles.rightSection}>
        <Text>Right Section</Text>
      </View>
    </View>
  );
};

export default App;

Issue:

After the implementation,

The below screenshot is captured in the screen viewport with height: 844 and width: 1280

enter image description here

The below screenshot is captured in the screen viewport with height: 552 and width: 1024

enter image description here

I am making this mainly for tablet screens of all height and width but in pure HTML and CSS way, this is responsive but in tablet screen’s in react native, its not responsive at all.

Kindly please help me to solve this issue of making the position:absolute elements responsive and located at same position across all the screen’s without distortion.

Note: Edited my question to mention that this implementation is happening in react-native.

6

Answers


  1. Try this:

    .container {
        background-image: url('https://i.stack.imgur.com/AfygH.png');
        height: 400px;
        background-position: center;
        background-size: contain;
        width: 700px;
    }
    .coil {
        position: absolute;
        top: 204px;
        left: 180px;
        zoom: 101%;    }
    .evaporator {
        position: absolute;
        top: 26px;
        left: 320px;
        zoom: 121%;
    }
    .compressor {
        position: absolute;
        top: 220px;
        left: 422px;
        zoom: 100%;
    }
    <div class="container">
      <div class="coil">
        <img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
      </div>
      <div class="evaporator">
        <img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
      </div>
      <div class="compressor">
        <img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
      </div>
    </div>;
    Login or Signup to reply.
  2. As an option. But, keep in mind, this is a construction with hard-coded coordinates. When you change the size of the container with the background image, you will also need to change the positions of the nested elements.

    <style>
      .container {
        background-image: url("https://i.stack.imgur.com/AfygH.png");
        width: 500px;
        height: 500px;
        background-position: center;
        background-size: cover;
        border: 2px solid crimson;
        margin: auto;
        position: relative;
      }
      
      .container div {
        border: 2px solid orange;
        position: absolute;
      }
      
      .coil {
        top: 256px;
        left: 36px;
      }
      
      .evaporator {
        top: 40px;
        left: 296px;
      }
      
      .compressor {
        top: 272px;
        left: 333px;
      }
    </style>
    <div class="container">
      <div class="coil">
        <img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
      </div>
      <div class="evaporator">
        <img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
      </div>
      <div class="compressor">
        <img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
      </div>
    </div>
    Login or Signup to reply.
  3. Maybe so?

    <style>
      .container {
        background-image: url("https://i.stack.imgur.com/AfygH.png");
        width: 500px;
        height: 500px;
        background-position: center;
        background-size: cover;
        border: 2px solid crimson;
        margin: auto;
        display: flex;
        align-items: center;
        /* position: relative; */
      }
      
      .container div {
        border: 2px solid orange;
        /* position: absolute; */
      }
      
      .coil {
        margin-top: 111px;
        margin-left: 33px;
      }
      
      .evaporator {
        margin-top: -343px;
        margin-left: 140px;
      }
      
      .compressor {
        margin-top: 170px;
        margin-left: -49px;
      }
    </style>
    <div class="container">
      <div class="coil">
        <img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
      </div>
      <div class="evaporator">
        <img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
      </div>
      <div class="compressor">
        <img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
      </div>
    </div>
    Login or Signup to reply.
  4. Here is the solution on flexbox instead of making it position:absolute as requested.

    .container {
        background-image: url('https://i.stack.imgur.com/AfygH.png');
        height: 400px;
        background-position: center;
        background-size: contain;
        width: 700px;
        background-repeat: no-repeat;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
    }
    
    .coil, .evaporator, .compressor {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .evaporator {
        width: 80px;
        height: 80px;
        margin-top: 23px;
        margin-left: 140px
    }
    
    .coil {
        width: 150px;
        height: 150px;
        margin-top: 60px;
        margin-right: 231px;
    }
    
    .compressor {
        width: 80px;
        height: 80px;
        margin-bottom: 88px;
        margin-left: 218px;
        margin-top: -90px;
    }
    <div class="container">
      <div class="evaporator">
        <img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
      </div>
      <div class="coil">
        <img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
      </div>
      <div class="compressor">
        <img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
      </div>
    </div>
    Login or Signup to reply.
  5. Because it’s bitmap image, I would suggest to go back to the real size of each element, and to position them regarding their real size. All that in pixels. If really needs resizing outer container, more or less same can be done taking percentage of each element regarding the big one and their position left top same kind of calculation…

    .container {}
    
    .circuit {
      position: relative;
      width: 100%;
      height: 100%;
    }
    
    .circuit>img {
      /* 688 x 401 */
      position: absolute;
      z-index: 1;
      left: 0;
      top: 0;
      width: 688px;
      height: 401px;
    }
    
    .coil {
      /* 120 x 90 */
      position: absolute;
      z-index: 1;
      width: 120px;
      height: 90px;
      left: 150px;
      top: 199px;
    }
    
    .evaporator {
      /* 80 x 70 */
      position: absolute;
      z-index: 1;
      left: 375px;
      top: 25px;
      width: 80px;
      height: 70px;
    }
    
    .compressor {
      /* 90 x 120 */
      position: absolute;
      z-index: 1;
      left: 405px;
      top: 213px;
      width: 90px;
      height: 120px;
    }
    <div class="container">
      <div class="circuit">
        <img src="https://i.stack.imgur.com/AfygH.png" alt="circuit" />
        <div class="evaporator">
          <img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
        </div>
        <div class="coil">
          <img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
        </div>
        <div class="compressor">
          <img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
        </div>
      </div>
    </div>
    Login or Signup to reply.
  6. This is another way.

    .container {
    background-image: url('https://i.stack.imgur.com/AfygH.png');
    height: 400px;
    background-position: center;
    background-size: contain;
    width: 700px;
    background-repeat: no-repeat;
     }
    .coil {
    position: absolute;
    top: 204px;
    left: 180px;
    zoom: 100%;    }
    .evaporator {
    position: absolute;
    top: 26px;
    left: 320px;
    zoom: 115%;
    }
    .compressor {
    position: absolute;
    top: 220px;
    left: 422px;
    zoom: 100%;
    }
    
    
    
    <div class="container">
     <div class="coil">
      <img src="https://i.stack.imgur.com/SKUms.png" alt="coil-image" />
     </div>
     <div class="evaporator">
      <img src="https://i.stack.imgur.com/spv58.png" alt="evaporator-image" />
     </div>
     <div class="compressor">
      <img src="https://i.stack.imgur.com/fzSaH.png" alt="compressor-image" />
     </div>
    </div>;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search