I want a single row (using divs) that has a gray text box with a transparent image overlaying the box. The gray box does not go all the way across the div, but the image does overlay part of it.
I want to achieve this with HTML and CSS.
I have attached a crude image of what I am trying to achieve. I also need this to be responsive in mobile where the text box fills the first row, and the image drops below the text box.
Any help would be greatly appreciated.
Here is a more detailed example of what I am trying to achieve using an image.
This was some generic code I came up with, but it did not work as intended.
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>
2
Answers
Here’s a way to do your layout using position:absolute. It’s not entirely clear what you are trying to do with your layout. I needed to create div’s e and f as fillers which is not the best way but you have the grey box basically going over and going under the red box.
Try the code below, which you can test here. For some reason when I make it into a runnable snippet, the text doesn’t flow around the
shape-outside
.