search
HomeWeb Front-endCSS TutorialHow to add text on image using HTML and CSS? (code example)

In this article we will introduce to you how to use HTML and CSS to add text to images. The content is very detailed.

Without further ado, let’s get straight to the point~

1. The benefits of using HTML and CSS to express

are not “writing text into the image itself” ", but "Configuring text on images through HTML and CSS" has the following benefits. (Recommended tutorial: CSS video tutorial)

The characters will not be blurred even when zoomed in

It is also read in search engines (suitable for SEO)

You can choose the letters

Responsive design allows you to adjust the font size (you can do something like lowercase letters in your smartphone display...)

2. How to Placing text on an image

Step 1: We need to prepare an image

As an example, I want to place white text on a dark background.

Step 2: Place the image and letters in a div element

Put the image and letters in a div tag. In the example, place the text "Great Wall" in the p tag. Of course you can use title tags instead of p tags, or you can use span tags.

<div class = "example" >  
<img  src="/static/imghwm/default1.png"  data-src="image/greatwall.jpg"  class="lazy"   alt="How to add text on image using HTML and CSS? (code example)" > 
<p>万里长城</p>
</div>

Step 3: Specify the position attribute

Set the css position attribute for each element.

Specify position:relative for the div as the parent element, and set absolute for the p tag containing the string. The img tag doesn't move.

Set the position of the p tag to top:0; left:0.

In order to place the image in landscape orientation, please specify the width of the img tag as width: 100%.

.example{/*父元素div*/
  position: relative;/*相対定位*/
  }
.example p {
    position: absolute;/*絶対定位*/
  color: white;/*文字设为白色*/
  top: 0;  
  left: 0;
  }
.example img {  
  width: 100%;
  }

The effect is as follows:

How to add text on image using HTML and CSS? (code example)

The text appears in the upper left corner of the image. position:absolute parent element will determine the position relative to the parent element. top:0; left:0 means "the image will be placed in the upper left corner of the parent (div)".

Step 4: Adjust the text style

Let’s adjust the font style now, try to enlarge the font and make it bold. In addition, in order to show a good-looking effect, also change the type of font.

.example{
  position: relative;
  }
.example p{
    position: absolute;
    color: white;
    top: 0;  
    left: 0;
    font-family :楷体,sans-serif;
    font-weight: bold;
    font-size: 2em;   
}
.example img{width: 100%;}

The effect is as follows:

How to add text on image using HTML and CSS? (code example)

This is completed. Please explain: If you want to fix the size of the image to a certain pixel, please follow the width : In 100% of cases, the width of the div of the parent element is specified as a fixed pixel.

3. Place the text in the middle of the image

In some cases, I want to "place the text in the center of the image". In this case, the CSS code should As follows.

.example{
  position: relative;
  }
.example p{
    position: absolute;
    color: white;
   /* top: 0;  
    left: 0;*/
    font-family :楷体,sans-serif;
    font-weight: bold;
    font-size: 2em;
    top: 50%;
  left: 50%;
  -ms-transform: translate(-50%,-50%);
  -webkit-transform: translate(-50%,-50%);
  transform: translate(-50%,-50%);
  margin:0;
  padding:0;
    
}
.example img{width: 100%;}

The effect is as follows:

How to add text on image using HTML and CSS? (code example)

Let’s explain the above centering

top and left are 50%

This is basically placed in the middle. However, this would deviate from the size of the text.

Error in adjusting the text part

Use transform: translate to correct the text difference. In translate(-50%,-50%), the difference between vertical and horizontal text is corrected. ttransform is a CS3 attribute, but it also corresponds to other browsers besides IE8.

To correspond with older browsers, translations are also written with vendor prefixes (-ms and -wabkit-).

If magin and padding are added and there is still space, it may be off-center. Therefore, just to be cautious, let's change its value to 0.

4. Display the category name with background color in the image

The following describes how to display text with background color on the image.

Please look at the following CSS code, the HTML is the same as above

.example {
  position: relative;
  }
.example p {
  position: absolute;  
  top: 0;
  left: 0;
  margin: 0; 
  color: white;
  background: lightblue;
  font-size: 15px;  
  line-height: 1;
  padding: 5px 10px;
  }
.example img {
  width: 100%;
  }

The effect is as follows:

How to add text on image using HTML and CSS? (code example)

When the label is attached to the corner of the image , characters are fixed. In this case, you can see clearly regardless of the brightness of the image. Likewise, if you want to change the size of the image, we recommend specifying the width for the div of the parent element.

The above is the detailed content of How to add text on image using HTML and CSS? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Demystifying Screen Readers: Accessible Forms & Best PracticesDemystifying Screen Readers: Accessible Forms & Best PracticesMar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Create a JavaScript Contact Form With the Smart Forms FrameworkCreate a JavaScript Contact Form With the Smart Forms FrameworkMar 07, 2025 am 11:33 AM

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

Adding Box Shadows to WordPress Blocks and ElementsAdding Box Shadows to WordPress Blocks and ElementsMar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Working With GraphQL CachingWorking With GraphQL CachingMar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Making Your First Custom Svelte TransitionMaking Your First Custom Svelte TransitionMar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Classy and Cool Custom CSS Scrollbars: A ShowcaseClassy and Cool Custom CSS Scrollbars: A ShowcaseMar 10, 2025 am 11:37 AM

In this article we will be diving into the world of scrollbars. I know, it doesn’t sound too glamorous, but trust me, a well-designed page goes hand-in-hand

Show, Don't TellShow, Don't TellMar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

What the Heck Are npm Commands?What the Heck Are npm Commands?Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft