DIV content fit inside Mobile Phone Background Image on all screens

1.2k views Asked by At

I am designing a single page which has a mobile phone Image (Android or IOS Phone). I want this image to be centered within the page on Desktop and Tablet. On Mobile Phone (The phone image), it should fit the whole screen. Also, l want to place with some html content on top of the image. No matter the screen size, the background phone image and the content must fit together.

This is what l have done so far

<html>

<head> 
<style>
#con{
    background-image: url('example.png');
    background-size: contain;
    background-repeat: no-repeat;
    width: 100%;
    height: 0;
    padding-top: 66.64%;

}
</style>

</head>

<body>

<div style=" margin: 0 auto;">
  <div id="con"></div>
</div> 

</body>

</html>
3

There are 3 answers

0
Rupesh Jain On

Use 100vw for width & 100vh for height, this will make #con cover the full screen. Remove height & padding-top.

0
zyp On

1 . you can set your parent element style {display:table;margin:0 auto;}

2.you can set your parent element style {position: relative;} child element style{ width: 50%;
height: 50%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}

0
Rachel.C On

You can try using @media to do adaptation