Home  >  Article  >  Web Front-end  >  One trick to teach you how to use css to add background images to HTML fonts (code sharing)

One trick to teach you how to use css to add background images to HTML fonts (code sharing)

奋力向前
奋力向前Original
2021-08-13 16:56:197918browse

In the previous article "Teach you step by step how to use css to add border effects to HTML fonts (code sharing)", I introduced you how to use css to add border effects to HTML fonts. The following article will introduce to you how to use css to add a background image to HTML fonts. Let's see how to do it together.

One trick to teach you how to use css to add background images to HTML fonts (code sharing)

Let’s first look at the final effect

One trick to teach you how to use css to add background images to HTML fonts (code sharing)

How to add in the font Background image?

1. When opening the html, first write the div tag. Between

and , enter the code
time will also abandon him
.

Code example

<!DOCTYPE html>
<html>
<head>
<title>给字体添加图片</title>
</head>
<body>
<div>时间也抛弃他</1div>
</body>
</html>

Code effect

One trick to teach you how to use css to add background images to HTML fonts (code sharing)

The code output result, the font is too small, I want to enlarge the font, how to enlarge it? Use the font-family attribute to set the font style of text. Don’t forget to write Code example

<style type="text/css">
    * {
        font-family: fantasy;
        font-size: 2em;
    }

Code effect

One trick to teach you how to use css to add background images to HTML fonts (code sharing)

The code output result is now visible. How to add a background image to the font? Try using background: url() to see how it works.

div {
   background: url(3.jpg);
   }

Code renderings

One trick to teach you how to use css to add background images to HTML fonts (code sharing) As a result, resizing does not work. What should I do as the size of the text changes? We can use the size (width and height) of the div box code example

 div {
        height:180px;
        width:710px;
     }

Code effect

One trick to teach you how to use css to add background images to HTML fonts (code sharing)

With the effect of the size of the text, we found that this is just Adding a background image to the div box does not add a background image to the font. It lacks an attribute background-origin to set the style, code example.

background-origin: border-box;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;

Effect picture

One trick to teach you how to use css to add background images to HTML fonts (code sharing)

##ok, you’re done~

Complete code




给字体添加图片

<style type="text/css">
    * {
        font-family: fantasy;
        font-size: 2em;
    }
    div {
        height:180px;
        width:710px;
        background: url(3.jpg) no-repeat; 
        background-origin: 
        border-box;
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;         
    }


时间也抛弃他

background-originwhat does it mean? By explaining it to everyone. The background-origin

attribute in

css refers to the positioning background image of the content box. There are three values. What are the three values?

padding-box padding box

border-box border box
content-box content box

Grammar code

background-origin: padding-box;
background-origin: border-box;
background-origin: content-box;

Recommended learning:

CSS video tutorial

The above is the detailed content of One trick to teach you how to use css to add background images to HTML fonts (code sharing). 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