How To Place Text on Image using HTML and CSS? Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To place text on an image using HTML and CSS, you can use different techniques to make the text look good and easy to read. Here, we will explore two approaches for placing text over an image using simple HTML and CSS.1. Using Absolute Positioning This approach uses absolute positioning to place the text directly on top of the image. The container that holds the image is positioned relatively, while the text is positioned absolutely inside it, allowing precise control over the text placement. HTML <!DOCTYPE html> <html lang="en"> <head> <style> .gfg { position: relative; width: 100%; text-align: center; } .text-container { position: absolute; color: rgb(255, 255, 255); left: 50%; top: 50%; transform: translate(-50%, -50%); background-color: rgba(41, 41, 41, 0.8); padding: 0.5rem; text-align: center; font-size: 16px; } </style> </head> <body> <div class="gfg"> <img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png" style="width: 50%; height: 60vh;"> <div class="text-container"> <h3>Sample</h3> <p>Mountains Image with river</p> </div> </div> </body> </html> ExplanationContainer Setup: The .gfg class is set to position: relative so that the .text-container can be positioned absolutely within this container.Text Styling: The .text-container uses position: absolute to overlay the text on the image and is centered using left: 50%, top: 50%, and transform: translate(-50%, -50%) for horizontal and vertical centering.Background and Padding: A semi-transparent background (rgba) is added to the text container for readability, with padding to create space around the text.OutputPlace Text on Image using HTML and CSS2. Using CSS background-image PropertyIn this approach, the image is used as a background for a div, and the text is overlaid within the container. This is useful when you want the image to cover the entire container, and you want to have flexible control over the text overlay. HTML <!DOCTYPE html> <html lang="en"> <head> <style> .container { position: relative; width: 400px; height: 300px; background-image: url("https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190710102234/download3.png"); background-size: cover; background-size: 100% 100%; color: white; text-align: center; line-height: 300px; } .text { background-color: rgba(0, 0, 0, 0.5); font-size: 20px; font-weight: bold; padding: 10px; } </style> </head> <body> <center> <div class="container"> <div class="text">Text on Image : (GeeksforGeeks)</div> </div> </center> </body> </html> ExplanationBackground Setup: .container uses background-image to set the image, with cover to fill the space and center for alignment.Text Styling: The text is styled with a semi-transparent background (rgba) and line-height for vertical centering.Flexibility: This method allows easy control of text and image positioning.OutputUsing CSS background-image Property Comment More infoAdvertise with us Next Article How To Place Text on Image using HTML and CSS? R riyakalra59 Follow Improve Article Tags : Web Technologies HTML CSS Web Templates CSS-Misc HTML-Misc +2 More Similar Reads How to Place Text Over an Image using CSS? Place Text Over an Image means overlaying text on top of an image using HTML and CSS. This effect is commonly achieved by placing the image and text inside a container and then using CSS techniques like absolute positioning, z-index, or flexbox to position the text over the image.Below are the appro 2 min read How to Place Text Blocks Over an Image using CSS? Placing text blocks over an image is a common technique used in web design to create visually appealing layouts. Here, we will explore different approaches to achieve this effect using CSS.Below are the approaches to place text blocks over an image using CSS:Table of Content Using Absolute Positioni 2 min read How to Add Image in Text Background using HTML and CSS ? In this article, we will use HTML and CSS to set the image in the text background. To set the image in the text background, some CSS property is used. To add an image in the text background using HTML and CSS, create a container element (e.g., a `<div>`), set its background to the desired imag 2 min read How to Add a Login Form to an Image using HTML and CSS? The login form on an Image is used on many websites. Like hotel websites that contain pictures of the hotels or some organizations that organize special events holding that event picture and login form. In that case, you can design a login or registration form on that picture. This design will make 2 min read How to create Perspective Text using HTML & CSS ? In this article, we are going to create an illusion of images below the main image. This is a simple article, we can achieve our target only by using HTML & CSS. Approach: Create a main div in which we have added a heading tag and then use the selectors in CSS to create the effects. HTML Code: F 2 min read Like