Home > Web Front-end > CSS Tutorial > 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)

奋力向前
Release: 2021-08-15 15:04:02
Original
7942 people have browsed it

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

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

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

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

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

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; }
时间也抛弃他
Copy after login

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

Related labels:
css
source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template