Home Web Front-end CSS Tutorial How to add text on image using HTML and CSS? (code example)

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

Nov 08, 2018 pm 02:51 PM
Add text

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/imghw/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>
Copy after login

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%;
  }
Copy after login

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%;}
Copy after login

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%;}
Copy after login

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%;
  }
Copy after login

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles