How to overlay text on top of an image in HTML without using the image as a background

Here is the code snippet I am currently working with:

    <img src="../../1021.png" alt class="img img-fluid">
  </div>

I am looking to add text and an input field over this image without setting it as a background due to certain reasons. Is there a way to achieve this?

Answer №1

Here is a simple code snippet for text overlapping that I just put together. Feel free to use it! Thank you

.parent-class {
position: relative;
}

.content-class {
background-color: #fff;
position: absolute;
top: 20px;
left: 20px;
padding: 10px;
}

.content-class h1,
.content-class p {
margin: 0;
}
<div class="parent-class">
<div class="content-class">
<h1>Hello World!</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<img src="https://images.pexels.com/photos/1020315/pexels-photo-1020315.jpeg" alt class="img img-fluid">
</div>

Answer №2

If you want to position text absolutely on a page, you can follow this example:

<div class="container">
  <img src="../../1021.png" alt class="image img-fluid">
  <div class="content">Your text goes here</div>
</div>

Here's the accompanying CSS code:

.container {
  position: relative;
  width: 100%;
}

.container img {
  position: relative;
  z-index: 0;
}

.container .content {
  position: absolute;
  z-index: 10;
  bottom: 0;
}

Make adjustments as needed. The concept is to have both your text and image elements within a container with a relative position. This allows you to position the text absolutely in relation to the container. Keep an eye on the z-index property to ensure the text appears above the image.

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

What is the correct way to utilize jquery's getJSON function?

<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#hidebtn").click(function() { ...

Disable page scrolling after making changes to the DOM

When my JavaScript function executes on page load and at set intervals, it cycles through images supplied by another PHP script. However, every time the function manipulates the DOM, the page scrolls back to the top of the containing div, which is quite fr ...

What is the best way to center a form within a jumbotron using CSS?

I'm struggling to replicate a Bootstrap V4 template where I cannot seem to align the form in the center within a jumbotron div. However, all other text is centered as expected. Even though I copied the code from a tutorial, it functions perfectly the ...

What is the best way to display a red cross on a button?

Could you please help me add a red cross (font awesome icon) to the button? I attempted to implement the code on JSFiddle. However, I still need: UPDATE Initially, I found Kiranvj's answer suitable for its simplicity and applicability to all r ...

Is there a way to detect if JavaScript is disabled using a unique CSS selector?

Does anyone know of a CSS selector that can be used when JavaScript is disabled? I'm not referring to the noscript tag, but specifically in CSS. ...

Combine several elements in a single jQuery scrollreveal function

I am currently working on a webpage that utilizes the jQuery library plugin scrollreveal to gradually reveal different html elements when the page loads. The functionality of the code is working correctly at the moment, but I have noticed that there is a d ...

What is the solution for resolving a significant spacing issue within a block of text?

I noticed that there is a significant gap between two of my divs when viewed in a browser. Here is the code: <div id="title-1"> <p>XYZ Corp is excited to introduce our latest project - Discover XYZ</p> </div> <d ...

Issue with displaying dropdown menu icon in Bootstrap 5

Currently utilizing Angular 14 alongside Bootstrap 5. The code snippet below showcases a simple Bootstrap dropdown menu: <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id=&quo ...

Is there a way to make jQuery Validate display errors within the form elements rather than outside of them?

Given the jQuery Validate basic example provided in the link below, what method can be used to display error messages within form elements whenever feasible (excluding checkboxes)? http://docs.jquery.com/Plugins/validation#source ...

Iterate through a directory on the local machine and generate elements dynamically

I am looking to create a jQuery-UI tabs menu that includes 54 images on each tab. I have a total of 880 images stored in a folder, and it would be very time-consuming to manually insert an <img> tag for each one. Is there a way to dynamically cycle t ...

Expanding the width of CSS dropdown menus according to their content

When attempting to create a dynamic dropdown menu, I encountered an issue where the values were skewed in Internet Explorer if they exceeded the width of the dropdown. To fix this problem, I added select:hover{width:auto;position:absolute}. However, now th ...

Are there any reliable sources for a complete list of browser CSS properties?

Is there a way to easily access a comprehensive list of CSS properties supported by specific browsers, particularly IE8? Any suggestions on where I can find this information? ...

Generate an HTML dropdown menu using information retrieved from an AJAX call

My select list is not displaying, even though everything else renders to the view as expected. The issue seems to be with my if-else statement in setting the selected option based on an ajax value. Is there a workaround to achieve this without using if-els ...

Attempting to replicate the flipping motion of a card by applying the transform property to rotate a div element, ultimately revealing a different

I am attempting to create a series of divs that simulate the flipping action of a notecard. Unfortunately, when I hover over the div, it turns white because I have set the display property to none. I am unsure how to make the other div (.back) visible. Y ...

Can you provide me with the validation expression for the email format in ASP.NET?

Currently, I am working on validating a text field where users need to enter an email address following a specific format: [email protected] Here are the rules for the email validation: 1) The email address must end with .com. No numbers are allowed ...

Utilizing duplicate elements (sub-template) within AngularJS

Looking for a way to avoid duplicating HTML content in my template, which currently repeats the same structure with minor changes: <div class="xyz-state0" data-ng-hide="data.error || !data.states[0].name"> <div class="xyz-content"> ...

An assessment consisting of three parts, with the initial section required to match the size of the browser window using jQuery and Javascript

UPDATE: The issue was resolved by adding the Doctype at the top of the page - "<!DOCTYPE html>" (no spaces between tags < , > and other characters) I am experimenting with a webpage that contains four sections. The first section should take up ...

Reposition the checked box to the top of the list

My goal is to click on each item, and the selected item should move to the top of the list and be rendered at the top. However, I encountered an issue where when clicking on an item, it moves to the top but the item that replaces it also gets checked. Bel ...

JavaScript function is not identifiable

I am currently developing a tic-tac-toe game with 9 buttons that will display either an X or O image depending on the boolean value of the variable isX. The main container for these buttons is a div element with the id 'stage' and each button ha ...

Is there a way to make the menu overlap the logo on the website?

Within my Ruby on Rails Spree application that utilizes the Twitter Bootstrap gem, I have the following code snippet for the header section: <body class="<%= body_class %>" id="<%= @body_id || 'default' %>" data-hook="body"> & ...