What is the best way to position an element on top of a section of a massive image using CSS?

My current project involves creating a demo of my app within the app itself. To give you an idea, check out the images below:

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

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

Let me provide some context.

The large black rectangle represents the phone screen boundary. The green area acts as margin space. The pink section showcases an outline image of a phone, while the blue portion displays a video of my app overlaid on the phone image. Together, they offer a visual representation of my app in action on a phone. These elements are horizontally centered. The bottom of the blue video aligns with the top of the yellow text box. The challenge arises from scaling down the oversized pink phone image and ensuring it is truncated by the yellow section. There should be an 80-pixel margin between the top of the pink image and the screen's top, or alternatively, we can consider resizing the phone image to about 80% of the full screen size for ease of implementation. Since I am using the same iPhone 8 image across multiple screen sizes (iPhone 8, iPhone 8 Plus, iPhone 11), hardcoding values is not ideal.

The code snippet I have so far addresses most elements, but encounters issues with the pink phone image getting cut off at the top (written in React-Native, though comprehensible regardless; HTML version available upon request):

<View style={{ flex: 1, flexDirection: 'column' }}>
    <View style={{ flex: 3, textAlign: 'center', marginTop: 80 }}>
        <View style={{ flex: 1, flexDirection: 'column' }}>
            <View style={{ flex: 1 }}></View>
            <Image style={{ marginTop: 'auto', height: 400, width: 375, position: 'absolute' }} source={require('assets/iphone.png')} resizeMode={'cover'} />
            <Video style={{ marginTop: 'auto', height: 350, width: 340, alignSelf: 'center' }} rate={2} source={require('assets/app.mp4')} shouldPlay={true} />
        </View>
    </View>
    <View style={{ flex: 2, paddingTop: "10%" }}>
        <Text>{item.title}</Text>
        <Text>{item.text}</Text>
    </View>
</View>

I suspect that enclosing the pink image within an image container div might be necessary, but I'm uncertain how to proceed from there. Can someone assist me in resolving this complex CSS issue?

Answer №1

To create this effect, utilize a background image with padding to adjust the visibility of the phone. Implement background-size and background-position properties for precise placement of the phone within the container. Below is a basic example.

.black{
  border: 3px solid black;
  width: 350px;
  height: 600px;
  background: green;
  overflow: hidden;
}
.phone{
  margin: 3rem 2rem 0 2rem;
  padding: 2rem;
  padding-bottom: 0;
    background: url("https://images.unsplash.com/photo-1580910051074-3eb694886505?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1301&q=80");
    background-size: 250%;
    background-position: 48% 40%;
}
.content{
  width:100%;
  height:300px;
  background: blue;
}
.yellow{
  width: 100%;
  height: 300px;
  background: yellow;
}
 <div class="black">
   <div class="phone">
     <div class="content">
     </div>
   </div>
   <div class="yellow">

   </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

Troubleshooting Problems with CSS `transform` in JQuery UI's `Slide` Transition

While implementing JQueryUI's slide transition on an element with a CSS transform, I encountered an issue where the top half of the element gets hidden during the animation. Is there a way to tweak either my JQueryUI animation or CSS to avoid this pro ...

Leveraging $last in Angular for obtaining the final visible element with ng-show

My ng-repeat container div contains multiple images, each with a corresponding div next to it. Using $last, I can make sure only the div of the final image is visible after all images. However, I also have a filter button on the page that hides certain im ...

Tips for implementing resizable Material-UI Dialogs

I'm working on a project that involves creating a dialog box that can be resized and dragged. While I found steps in the Material-UI Dialog documentation on how to make it draggable, I'm still looking for information on how to make it resizable. ...

Interactive webpages with dynamic HTML content, similar to the design of popular platforms such

Explore the source code of Dropbox's homepage or any Soundcloud page. They utilize various scripts and minimal pure HTML content (article, main, p, div). This method of generating pages is referred to as dynamic content/HTML. The main function seems ...

Getting error messages for form validation in CodeIgniter when using Fileupload via Ajax can be tricky. Here are

Hello, I am currently working on retrieving form errors for an input type file in CodeIgniter. I have created a Javascript code that utilizes Ajax to submit the details as shown below: var name = $("#name").val(); var des = $("#des").val(); var img=$("#fi ...

What is the secret behind positioning the tooltip correctly using 'relative' placement?

Check out this example of a CSS tooltip. The author has positioned the tooltip relatively. .tooltip{ display: inline; position: relative; } However, in this guide, it explains, Relative. This type of positioning can be confusing and often misund ...

What are the steps for implementing Ajax in Django templates?

Looking to implement Ajax within Django templates? urls.py from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^home/$', 'views.index'),) views.py from django.template import RequestContext fro ...

Incorporating an HTML5 Theme with Angular 2

Is there a way to effectively set up my current Theme, which is built using bootstrap, CSS, and JS? I have included the following CSS and JS files in my theme, but I don't want to add them directly to index.html like in Angular 1. Can anyone suggest ...

Is there a way to retrieve a value from localStorage and use it to automatically set the selected option in a UL-based dropdown list upon page load

I have implemented a unique UL-based dropdown list that I discovered here (specifically the third model) as the foundation for a role-based selection box for my users. To enhance user experience, I am storing their role selection in localStorage so they do ...

Is there a way to show several elements simultaneously by hovering over another?

Can anyone help me with setting display:block; on all the p tags and the overlay div when I hover over my img tag? Is this achievable with just CSS or do I need to incorporate some JavaScript? Open to suggestions on the best approach. Any assistance woul ...

How to Style CSS specifically for Internet Explorer?

I'm dealing with a simple div that has a 2px thick border and absolute positioning, but it's hidden until its parent element is hovered over. The issue arises in IE due to the box model, causing the position of this div to be somewhat off in IE c ...

What is causing the inconsistency and instability in this jQuery sortable configuration?

I have created a page where users can interact with green blocks by dragging and re-ordering them from one sliding area to another. Sometimes, however, the dropping and sorting functionality is unreliable. The boxes may align incorrectly or the drop area m ...

Unable to configure AngularJS inputs to have a blank default value

I am facing a peculiar issue where I am unable to initialize my input fields as blank/empty. Even after clearing the cache and performing a hard reload, Google Chrome seems to be auto-populating them with older cached values. Here is the current view: ht ...

The React-native application is freezing on the splash screen but functions perfectly when in debug mode

After successfully running my react-native app in debug mode using npx react-native run-android, I encountered an issue when trying to open the release builds. The app would get stuck on the splash screen without crashing, whether I used the command npx re ...

Implementing personalized font styles in HTML on a WordPress platform

I recently followed a tutorial on how to upload custom fonts to my WordPress website, which you can find at the link below. I completed all the necessary steps, such as uploading to the ftp, modifying the header.php file to include the font stylesheet, and ...

Why isn't the ajax table showing up in my HTML?

I am facing an issue while trying to run an application that involves AngularJS, Ajax, and a Java Controller returning JSON. Despite my efforts, the table is not displaying in the HTML. I am new to working with Ajax and AngularJS, and need assistance troub ...

Can someone provide guidance on utilizing parentheses in HTML code?

I am facing an issue in my Angular project while using the WordPress API. The problem lies in the API address structure which includes "" like this: <img src="{{importantvideo.better_featured_image.media_details.sizes.["covernews-med ...

Traversing through Object consisting of dual arrays within VueJS

Currently, I am working on a chat application built in VueJS and encountering an issue when attempting to display messages along with their respective timestamps. The challenge arises from the need to iterate through an object that contains two arrays: one ...

Container element refusing to grow in size while the footer remains fixed on the screen

Description: I recently encountered an issue with the layout of my website. I have a main content div (#main) and a container div (.display), which should expand automatically with content to push the footer down. Initially, I struggled to get this workin ...

The mysterious case of the missing CSS file in Node.js

I recently set up a new Node.js Express Project. However, I am facing an issue with the index.ejs file showing the error: Undefined CSS file ('/stylesheets/style.css'). Here is the content of the index.ejs file: <!DOCTYPE html> <html& ...