Using CSS to overlay multiple text boxes onto an image at different positions

Can you help me solve this challenge? I have an image of a real property that will be randomly loaded into a DIV with a width of 200 pixels. The image needs to be resized to 200 pixels wide while maintaining its proportional height. Additionally, I have four different captions that need to be placed on the image:

  1. Title
  2. Price
  3. Location
  4. Surface

These captions should be positioned respectively at the top center, bottom left, bottom left, and bottom right of the picture. Here's the layout I'm looking for:

--------------------------------
|             Title            |
|                              |
|                              |
|Price                         |
|Location               Surface|
--------------------------------

I've tried overlaying text onto an image by using relative positioning for the div and absolute positioning for the text, but I can't seem to set individual positions for each text div regardless of how many classes I create.

Any advice or suggestions would be greatly appreciated!

UPDATE: After some more digging, I managed to find a solution. Here is the code snippet I used:

<div id="stampRandom">
    <img class="stampImg" src="myphoto.jpg">
    <div class="title"><span>Title</span></div>
    <div class="price"><span>Location</span><span><br><b>Price</b></span></div>
    <div class="surface"><span><b>Surface</span></b></span></div>                  
</div>

And here's the corresponding CSS:

#stampRandom {
    position: relative;
    top: 0px;
    left: 0px;
    width: 200px;
    color: #FFF;
    font-size: 80%;
}

#stampRandom img {
    top:0px;
    left:0px;
    width:200px;
}
#stampRandom div {
    position: absolute; 
    width: 200px; 
}
#stampRandom div span {
   background: rgba(50, 50, 50, 0.7);
}
.title {
    top:2px;
    width:100%;
    text-align:center;
}
.price {
    bottom:0px;
    left:0px;
}
.surface {
    bottom:0px;
    right:0px;
    text-align:right;
}

Thank you to everyone who offered their assistance! Your insights were invaluable in helping me overcome my coding roadblock and giving my project a much-needed refresh.

Answer №1

Visit this link for more information

<style>
.img-cont{position:relative;width:200px;}
.img-cont div{position:absolute; background:red}
.img-title{top:0px;width:100%;text-align:center;}
.img-price{bottom:0px; left:0px;}
.img-surf{bottom:0px; right:0px;text-align:right;}
</style>
<div class="img-cont">
    <img src="/MyImage.jpg" width="200"/>
    <div class="img-title">
        Title
    </div>
    <div class="img-price">
        Price Location 
    </div>
    <div class="img-surf">
        Surface
    </div>
</div>

Explore this code snippet to enhance your understanding

Answer №2

Here is a simple demonstration of how you can achieve this task. Although it may not be flawless, it gets the job done. If you require additional assistance or would like to explore a different method, please feel free to reach out.

http://jsfiddle.net/bj6k1/245/

<div class="imageWrapper">
 <img src="http://placeholder.com/250x200" alt="1" width="250" height="200" />
 <div class="top">TOP</div>
 <div class="bot">BOT</div>
 <div class="botleft">BOTLEFT</div>
 <div class="botright">BOTRIGHT</div>
</div>

Below is a CSS snippet for styling bottom and bottom left text:

.bot{
 position:absolute;
 background:red;
 text-align:center;
 margin:0 auto;
 width:100px;
 left:50%;
 margin-left:-50px;
 bottom:0;
}

.botleft{
 position:absolute;
 background:red;
 text-align:center;
 margin:0 auto;
 width:100px;
 left:0;
 bottom:0;
}

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

Adjust and modify class when resizing the window

I've encountered a challenge with my Bootstrap Modal when trying to set the width to 750px on large desktops. When resizing the window to a smaller size, the modal loses its responsiveness. To tackle this, I added a class to the modal to give it a fix ...

Vuetify's scrolling list feature allows for smooth navigation through a list

Check out my Vuetify code snippet that utilizes a list: <v-list> <v-list-tile v-for="user in users" :key="user.id" avatar @click="" > <v-list-tile-content> < ...

Arrangement of elements in Bootstrap

Is there a way to prevent my frame from overflowing compared to its parent class when using bootstrap's row class? How can I address this issue? https://i.sstatic.net/gR7Ep.png Here is my code: html: <div class="container"> <div cla ...

An alternative to PHP's exec function in Node.js

I am interested in developing a piece of software using C (such as prime number factorization) and hosting it on my web server with Node.js. Following that, I would like to create an HTML document containing a form and a button. Upon clicking the button, ...

Requesting the user to repeatedly input their birth year until it is less than the current year

Can anyone help me figure out how to display a prompt until the user enters a birth year that is less than the current year? I've tried using a loop in my code, but I'm having trouble getting it right. Any assistance would be greatly appreciated. ...

Ways to showcase various data on a single PHP page without relying on the GET method

I'm currently working on a project to create a website where users can set up their own profiles and share their profile link for others to view. Instead of having separate pages for each user, I want to streamline the process by using just one page t ...

The CSS show and hide feature is effective across all browsers except for Safari. What steps can be taken to address this issue

My toggle trigger works perfectly in Chrome, but I'm having trouble getting it to work in Safari. Any suggestions? <a href='#show'class='show'>see more --></a> <a href='#hide'class='hide&apos ...

Removing the 'div' tag using jQuery in CodeIgniter

I need assistance in removing the added item. $(document).ready(function() { $("#appendex0").click(function() { $(".divcls0").append('<div class="col-sm-10 col-sm-offset-1"><div class="col-sm-3 col-sm-offset-1"><div class="form ...

Saving the order of elements dropped in jQuery UI Droppable into an input field

Link to setup: https://jsfiddle.net/7b8bqzsh/1/ You can view a full working example using the link provided above. The objective is to store the order of the links in an input field so that it can be saved to a database. Currently, only the latest ID is ...

React Array Not Behaving Properly When Checkbox Unchecked and Item Removed

When using my React Table, I encountered an issue with checkboxes. Each time I check a box, I aim to add the corresponding Id to an empty array and remove it when unchecked. However, the current implementation is not functioning as expected. On the first c ...

What is the best way to ensure that the width of a div fills its parent container?

I need my divs(.give, .sep, .take) to completely fill the width of its parent container (.trade). This is the HTML code: <div class="trade"> <div class="give"></div> <div class="sep"></div> ...

Locate an element by its TEXT using SeleniumBase

When using SeleniumBase, I have a requirement to click on an element that is identified as shown below: <span class="text"> Contato PF e PJ </span> The specific text inside the element (Contato PF e PJ) needs to be ...

Is it possible to pass a function as an argument to another function in jQuery?

My function accepts a JSON object as a parameter containing User id, password, success callback, and error callback. The goal is to authenticate the user using an AJAX service. If the service response is successful, I want to call the success callback fun ...

What is the best way to ensure that a navbar dropdown appears above all other elements on

I'm having trouble creating a navbar dropdown with material design. The dropdown is working fine, but the issue I'm facing is that other elements are floating above it. https://i.stack.imgur.com/aJ0BH.png What I want is for the dropdown to floa ...

Limiting the size of images within a specific section using CSS

When using CSS, I know how to resize images with the code snippets below: img {width:100%; height: auto; } img {max-width: 600px} While this method works effectively, it applies to every image on the page. What I really need is for some images to be ...

The intended functionality of clicking on an image is exclusively reserved for its immediate parent element

I have a feature on my website that displays an image gallery. When a user clicks on an image, it opens up the image in full screen similar to Facebook's theatre mode. I have written code so that when the user clicks anywhere in the container of the i ...

Ways to expand the dimensions of a Popup while including text

I am using a bootstrap modal popup that contains a Treeview control in the body. The issue arises when the node's text width exceeds the popup's width, causing the text to overflow outside of the popup. Is there a way to dynamically adjust the ...

What is the best way to ensure a div is always scrollable?

I am currently working with Angular 4 and Bootstrap 4 (alpha 6). I have implemented ngx-infinte-scroll to fetch data from the server when the user scrolls to the top or bottom of the div. The issue I am facing is that the user can only scroll when there ar ...

Material-ui v5's DataGrid now includes a new filter icon feature

The current default setting for the new DataGrid is to only display a filter icon when hovering over the column header with an applied filter. In contrast, the previous version had the icon visible at all times. For reference, you can view the Codesandbox ...

Adding <meta> tag to the <head> section of a particular page in WordPress and HTML

I need to insert a meta tag for the site description within the head section of a specific page, before the opening body tag. The website is created using WordPress & Elementor. I prefer not to include it globally in the theme's header option like sh ...