CSS to achieve text on the background image

CSS to achieve text on the background image

Effect:

insert image description here

<div class="imgs">
  <!-- Background image -->
  <div class="background">
    <img :src="item.voteTime ? imgSrc1:imgSrc2" width="100%" height="100%" alt="" />
  </div>
  <!-- Text -->
  <div class="front">
    <div v-if="item.voteTime">
      <p>Thank you very much! </p>
      <p>You have voted: <span>{{item.voteTime}}</span></p>
    </div>
    <p v-else style="color:#999999">Sorry, you have not completed the voting~</p>
  </div>
</div>

data() {
  return {
    imgSrc1:require('@/common/imgs/yitoupiao.png'),
    imgSrc2:require('@/common/imgs/weiwancheng.png'),
  }
},

The large div outside: set the width and height;
Background image: 1) If it fills the entire image, set both width and height to 100%; 2) If it only occupies a portion, set the position. Key point: The z-index must be lower than the text level, otherwise it will be covered;
Text: It can be positioned or not based on the requirements, and the z-index should be set higher than the image.

.imgs {
  background: #fff;
  position: relative;
  width: 100%;
  height: 250px;
  color: #195541;
  .background{
    // width:100%;  
    // height:100%; /**The width and height are 100% so that the image fills the screen*/
    // z-index:-1;
    z-index:1;
    position: absolute;
    width: 250px;
    height: 100%;
    right: 20px;
    bottom: 0px;
  }
  .front{
    z-index:2;
    position: absolute;
    text-align: center;
    top: 39%;
    left: 25%;
    font-weight: normal;
    line-height: 40px;
    font-size: 28px;
  }
}

A bug was encountered during the development process: I initially set the z-index of the background image to -1, which resulted in the background image sometimes being displayed on the h5 and sometimes not. Later, I changed it to a positive number 1 to solve this problem.

This is the end of this article about how to use CSS to put text on a background image. For more information about using CSS to put text on a background image, please search 123WORDPRESS.COM’s previous articles or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Use overflow: hidden to disable page scrollbars

>>:  Docker container data volume named mount and anonymous mount issues

Recommend

How does Vue solve the cross-domain problem of axios request front end

Table of contents Preface 1. Why do cross-domain ...

Install and configure MySQL 5.7 under CentOS 7

This article tests the environment: CentOS 7 64-b...

Analysis of the principle and creation method of Mysql temporary table

This article mainly introduces the principle and ...

The most basic code for web pages

◆Add to favorites illustrate Click to add your we...

An exploration of the JS operator in problem

Here's the thing: Everyone knows about "...

Detailed explanation of how to customize the style of CSS scroll bars

This article introduces the CSS scrollbar selecto...

MySQL data backup and restore sample code

1. Data backup 1. Use mysqldump command to back u...

Bootstrap 3.0 study notes CSS related supplement

The main contents of this article are as follows:...

How to modify the group to which a user belongs in Linux

Modify the group to which a user belongs in Linux...

An article to understand the execution process of MySQL query statements

Preface We need to retrieve certain data that mee...

Let's talk about my understanding and application of React Context

Table of contents Preface First look at React Con...

Database SQL statement optimization

Why optimize: With the launch of the actual proje...

In-depth analysis of Vue's responsive principle and bidirectional data

Understanding object.defineProperty to achieve re...

HTML code text box limit input text box becomes gray limit text box input

Method 1: Set the readonly attribute to true. INPU...