Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
How to spin text on mouse hover using HTML and CSS?
Next article icon

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> 

Explanation

  • Container 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.

Output

absolute-positioning

Place Text on Image using HTML and CSS

2. Using CSS background-image Property

In 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> 

Explanation

  • Background 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.

Output

add text overlay on image using css

Using CSS background-image Property



Next Article
How to spin text on mouse hover using HTML and CSS?

R

riyakalra59
Improve
Article Tags :
  • CSS
  • HTML
  • Web Technologies
  • Web Templates
  • CSS-Misc
  • HTML-Misc

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 appr
    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 Positio
    2 min read
  • How to Shake Text on hover using HTML and CSS?
    Shaking Text animation is a very cool animation which can be used in websites, this animation can be easily created using some basic HTML and CSS, the below section will guide on how to create the animation. HTML Code: In this section we have a basic div element which contains some text inside of it
    2 min read
  • How to spin text on mouse hover using HTML and CSS?
    The spinning of text on mouse hover is known as the Spin Effect or the Rotation Effect. In this effect, each alphabet of the word is rotated along with any one of the axes (preferably Y-axis). Each word is wrapped inside in <li> tag and then using CSS:hover Selector selector we will rotate eac
    3 min read
  • How to Skew Text on Hover using HTML and CSS?
    Skewed text animation effect can be created using HTML and CSS, this animation looks very cool and can be used in websites to make them look more dynamic, the following sections will guide on how to create the desired animation effect. First Section: In this section we will create a basic div tag wh
    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 indent text in HTML by using CSS ?
    Text indentation in an HTML document sets the length of empty white space before lines of text in a block, typically marking the beginning of a paragraph. Proper indentation can enhance the readability and layout of your web content. Here, we will explore several common approaches for text indentati
    3 min read
  • How to design Meet the Team Page using HTML and CSS ?
    You will learn how to create a simple and responsive “Meet the Team” page using HTML and CSS. We will use HTML to structure the content of the page, such as the headings, paragraphs, images, and links, and then we use CSS to style the elements of the page, such as the colors, fonts, and layout. Here
    5 min read
  • How to Change Image Opacity on Hover using CSS ?
    Changing the opacity of an image on hover can add a more elegant and interactive effect to your website. This simple yet effective technique can be implemented using CSS. When a user hovers over an image, it gradually becomes more transparent, pointing out the interaction and creating an attractive
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences