Placing an image on top of a text box and ensuring it adjusts to different screen sizes

I'm looking to create a design element with a gray text box and a transparent image overlay in a single row using HTML and CSS. The gray box should only cover part of the row, while the image overlays on top.

For mobile responsiveness, I want the text box to fill the first row and have the image drop below it.

Your assistance in achieving this layout would be highly appreciated.

View Desired Outcome

If you need more context, check out this detailed example reflected in an image.

Updated Image Reference

Below is a sample code snippet I tried, but it didn't produce the desired result:

    body {
    margin: 0;
    font-family: Arial, sans-serif;
}

.container {
    position: relative;
    display: flex;
    align-items: center;
    height: 300px; /* Adjust height as needed */
}

.text-box {
    background-color: #ccc; /* Gray color */
    padding: 20px;
    width: 50%; /* Adjust width as needed */
    box-sizing: border-box;
    z-index: 1;
}

.image-overlay {
    position: absolute;
    right: 0;
    top: 0;
    width: 50%; /* Adjust width as needed */
    height: 100%;
    overflow: hidden;
}

.image-overlay img {
    width: 100%;
    height: 100%;
    object-fit: cover; /* Ensures the image covers the container */
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Text and Image Overlay</title>
</head>
<body>
    <div class="container">
        <div class="text-box">
            <p>Your text goes here. This box is gray and contains some content.</p>
        </div>
        <div class="image-overlay">
            <img src="https://picsum.photos/200/300" alt="Overlay Image">
        </div>
    </div>
</body>
</html>

Answer №1

If you're looking to create a layout using position:absolute, here's one way to go about it. It seems like there may be some confusion regarding the desired outcome of your layout. To achieve the overlapping effect, divs e and f were created as fillers, though this is not the most optimal solution.

div{
margin:0;
padding:0;
}

#a{
width:100%;
height:100vh;
background-color:white;
position:relative;
}


#b{
position:absolute;
width:80%;
height:35%;
background-color:white;
z-index:13;

}

#c{
position:absolute;

width:70%;
height:50%;
left:0;
top:35%;
background-color:grey;
color:white;
z-index:11;

}



#d{
position:absolute;
background-color:red;
height:100%;
width:40%;
right:0;
z-index:12;
}

#e{
position:absolute;
width:10%;
height:10%;
background-color:white;
z-index:12;
left:70%;
top:35%;
}


#f{
position:absolute;
width:10%;
height:10%;
background-color:grey;
z-index:15;
left:60%;
top:35%;
}

body{
margin:0;
padding:0;
}
<div id='a'>
  <div id = 'b'>
  </div>
  
  <div id = 'c'>
  </div>
  
  <div id='d'>
  </div>
  
  <div id='e'>
  </div>
  
  <div id='f'>
  </div>
</div>

Answer №2

Check out the code snippet below and feel free to test it here. Interestingly, when I attempt to convert it into a runnable example, the text fails to wrap around the defined shape using shape-outside.

<!doctype html>
<html>
<head>
  <style>
  .d1 {
    background: silver;
    display: flex;
    flex-direction: column;
  }
  .d1 p {
    order: 1;
  }
  .d1 img {
    order: 2;
    width: 75%;
    align-self: center;
  }
  @media (min-width: 750px) {
    .d1 {
      display: block;
      width: 50%;
      margin: 0 auto;
    }
    .d1 p {
      padding: 1em;
    }
    .d1 img {
      float: right;
      width: 250px;
      shape-outside: url(https://donald.au/bugs/so-78914768-3.png);
      shape-margin: 1em;
    }
  }
  </style>
</head>
<body>
  <div class="d1">
    <img src="https://donald.au/bugs/so-78914768-3.png">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas tempor nunc mauris, sit amet placerat tortor lobortis dapibus. Nam lectus eros, maximus ac magna vel, congue consequat eros. Fusce id pretium diam. Cras sit amet pharetra ante. Sed quis commodo quam, vel facilisis ipsum. Vestibulum sodales iaculis arcu, et fringilla nisi ullamcorper sed. Donec interdum sit amet est non accumsan. Donec non augue feugiat, fermentum nunc non, convallis est. Cras vel ligula nec odio faucibus ultricies. Sed vulputate tortor eget pretium convallis. Cras interdum elit eget mi porta suscipit. Morbi ut velit diam. Etiam finibus eros et efficitur rutrum. Quisque viverra metus ac eleifend imperdiet. Quisque pretium ut purus vitae tempus. Duis varius risus congue velit faucibus, sed interdum purus consectetur.</p>
  </div>
</body>
</html>    

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

Is it possible to create an HTML dropdown menu with integer values?

What is the best way to create a dropdown menu in HTML with integer values ranging from 1 to 12? How can I ensure that when a value is selected, it gets saved into a variable? All I need is a simple select field in HTML that displays options from 1 to 12 ...

Adding flair to a object's value in React JS

In my React JS file, I have a map function that I am using to populate a select dropdown with options. const options = response.map(k => ({ value: k.id, label: k.description ? `${k.name} - ${k.description}` : k.name, })); I ...

Attempting to transform a numerical value into CSS syntax

Currently, I am attempting to loop through several DIV elements, extract a numerical value from each DIV, and then based on that value matching a specific value in the JavaScript code, assign a particular CSS Class back to the original DIV. This is the sn ...

Adjust the text placement on the toggle switch to turn it On or Off

I am looking to create a toggle switch with a small size. I found some code on Stack Overflow that I tried but it didn't work as expected. The code snippet I attempted to use is from another helpful post. My goal is to have the "on" text align to th ...

Unusual limitation in jQuery/ASP.NET WebForms for text area character count

Utilizing www.lipsum.com to produce 2,000 characters, verifying the character count within the word and ensuring there are exactly 2,000 characters. Within ASP WebForms, I have the subsequent control: <asp:textbox runat="server" ID="txtComment" rows=" ...

Challenges with Setting up reCaptcha

Issue arises while trying to implement reCaptcha. In the body of my basic HTML page, I have a form with the following content. <form action="/cgi-bin/new_forms/form.pl" method="post" name="pledgeform" onSubmit="return validate(this)"> All form info ...

Animate with ease upon mouse entry into the div

I have a question about hovering. $(".hover li img").mouseenter(function() { $(".overlay").animate({ left: pixels+"px" }); }); The overlay class is a transparent box around the image, with a red border. I want the border to move when hov ...

Identifying bots with Python's Requests and BeautifulSoup in web scraping tasks

Attempting to scrape all the HTML elements from a page using requests & beautifulsoup. Utilizing ASIN (Amazon Standard Identification Number) to extract product details from a page. The code snippet is as shown below: from urllib.request import urlope ...

Is there a way to store image URLs in a fashionable manner?

Hey there! I've been working on creating a HTML page that showcases multiple images. Everything is running smoothly on my localhost, but when I try to access it online, the images take forever to load. Any suggestions on how I can cache image URLs in ...

Tips for creating a dynamically responsive eye icon placement

When the screen width is less than 991px, the content is centered but the eye icon that is absolutely positioned goes off-center. This issue only occurs on devices with a small or medium screen size. The reason for using absolute positioning on the icon i ...

Having trouble implementing button functionality in a web app using JS, jQuery, HTML, and CSS along with an API integration

I am currently developing a web search engine that utilizes the Giphy API... However, I've encountered difficulties with the button function. I have created buttons to modify the page format and adjust the number of search results displayed.... The We ...

Personalized 404 Error Page on Repl.it

Is it possible to create a custom 404-not found page for a website built on Repl.it? I understand that typically you would access the .htaccess file if hosting it yourself, but what is the process when using Repl.it? How can I design my own 404-not found p ...

What is the process for populating the Body placeholder in asp.net Core MVC?

After reviewing a tutorial, I discovered that the RenderBody method is supposed to display views within the designated placeholder of the layout, as illustrated in this diagram: https://i.sstatic.net/nJLUm.png However, in practice, it did not work exactl ...

What could be causing the movement of my main navigation links when hovering over another top-level element?

I am struggling to pinpoint the origin of a particular issue I am encountering with my menu. The problem arises when I hover over a top-level element, causing all elements below it to shift to the right: (Take note of the top-level elements being affected ...

What is the best way to add character limits to an HTML form?

Hello there, I am currently working on creating an HTML form to generate a PDF file. However, I am facing difficulties in figuring out how to create the character placeholders for text input fields (as shown in the screenshot of a sample PDF file below). ...

What is the best way to center the image on a page?

How can I center an image on the screen with a caption? <div id="team-area"> <div class="container"> <div class="row"> <div class="col-12"> <h3 class="main-title">Our Team</h3> < ...

How can I modify the text color of the Navbar-Brand class in Bootstrap NavBar?

I have been experimenting with various options to change the text color of the `navbar` and specifically the `navbar-brand` item. However, my `.navbar-brand {color: purple;}` CSS class doesn't seem to be working. Can someone please assist me in determ ...

Experience Image in an inverted carousel or Virtual Reality/Spherical environment

My goal is to create a virtual reality experience similar to this: https://i.sstatic.net/8pZx0.png However, my background image is 4096x1024 and I want it to have a VR effect where the center appears further away from the screen and the edges are slightl ...

Receiving 'Unexpected end of input' error when using a string as a JavaScript function argument

I am trying to implement an ajax request function in my project. This is the code snippet I have: function Reject(id, scomment) { jQuery.ajax({ type: "POST", url: "reject.php", data: {id: id, Scomment: scom ...

Having Trouble Loading PHP File with Jquery

I've been struggling with using JQuery/Ajax to load the content of my PHP file into a div tag. Below is the code snippet from my page that attempts to load the file: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/ ...