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

What to do if you forget the initial password of MySQL on MAC

The method to solve the problem of forgetting the...

Node.js implements breakpoint resume

Table of contents Solution Analysis slice Resume ...

How to set PATH environment variable in Linux system (3 methods)

1. In Windows system, many software installations...

HTML basic summary recommendation (title)

HTML: Title Heading is defined by tags such as &l...

How to prevent users from copying web page content using pure CSS

Preface When I was typing my own personal blog, I...

How to change password in MySQL 5.7.18

How to change the password in MySQL 5.7.18: 1. Fi...

How to add automatic completion commands for docker and kubectl on Mac

Introduction to kubectl kubectl is a command line...

Solution to nginx not jumping to the upstream address

Preface Today I encountered a very strange proble...

Install MySQL 5.7.17 in win10 system

Operating system win10 MySQL is the 64-bit zip de...

WeChat applet realizes left-right linkage

This article shares the specific code for WeChat ...

Detailed tutorial on installing mysql on centos 6.9

1. Confirm whether MySQL has been installed. You ...