Home Web Front-end CSS Tutorial How to introduce external fonts in css? css font style setting method (example)

How to introduce external fonts in css? css font style setting method (example)

Sep 14, 2018 pm 01:56 PM
css

In browser display, different font styles on the page not only improve the beauty of the page, but also improve the user's browsing interactivity. So how are these different font styles set with css? This chapter will show you how to introduce external fonts in css? CSS font style setting method (example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. What is the function and usage of the font-family attribute?

1. The role of the font-family attribute

The font-family attribute allows you to use css code to set the font style for text display on the page. For example: Song Dynasty, Microsoft Yahei, etc. The font-family attribute sets different fonts to make the page display more coordinated and beautiful.

The font-family attribute can define multiple values ​​(fonts), separated by commas. If the browser does not support the first font, it will try the next one until one is recognized. The browser will use the first value it recognizes.

2. Usage of font-family attribute

font-family:"设置字体名称";
Copy after login

Example:

css code:

.demo{ font-family:"微软雅黑";}
Copy after login

html code:

<div class="demo">我是一段测试文字</div>
Copy after login

In this way, all text in the demo box will be displayed in Microsoft Yahei font style.

2. How to introduce external fonts into CSS?

Commonly used fonts in general browsers include: Song, Hei, Microsoft YaHei, etc. Because when the web page is loaded and the font is selected, the text font in the visitor's system text library is called. If the font is not found, the default web font will be displayed. The operating system installed by everyone has these fonts by default, so these fonts are also called safe fonts. But for some special needs, we can reference some external fonts to display personalized and good-looking text styles.

How to introduce external fonts in CSS (@font-face):

##1.@font-face tag introduction

@font-face allows you to use fonts that are not installed on your computer in web pages, completely getting rid of the restrictions of safe fonts. Just install the font package on the server. When the user loads the web page, the font package will be automatically downloaded to the user's machine to ensure that the fonts can be rendered correctly.

2. Steps for introducing external fonts into CSS

1) Introduce the font into CSS and customize a name for the external font

@font-face {
    /* font-properties */
    font-family: 用户自定义的字体名称;
    src:url(&#39;加载外部字体文件的文件地址&#39;),  
    url(&#39;加载外部字体文件的文件地址&#39;),
    url(&#39;加载外部字体文件的文件地址&#39;); /* IE9 */
}
Copy after login

font- Family defines the name of the font, and the following src is the location where the font file is loaded. The reason why there are multiple URLs is because of browser compatibility issues.

2) Use the font just defined

div {
    font-family: 用户自定义的字体名称;
}
Copy after login

Code example:


@font-face {
	 font-family: &#39;YaHei Consolas Hybrid&#39;;
	src: url(&#39;../fonts/yaheiconsolashybrid.ttf&#39;);
}

body {
	font-family: &#39;YaHei Consolas Hybrid&#39;;
	font-size: 16px;
}
Copy after login
Considering that the formats of fonts in different browsers are different ( Compatible with browsers)


/*字体后缀和浏览器有关,如下所示
* .TTF或.OTF,适用于Firefox 3.5、Safari、Opera 
* .EOT,适用于Internet Explorer 4.0+ 
* .SVG,适用于Chrome、IPhone 
*/
@font-face {
    font-family: &#39;HansHandItalic&#39;;
    src: url(&#39;fonts/hanshand-webfont.eot&#39;);
    src: url(&#39;fonts/hanshand-webfont.eot?#iefix&#39;) format(&#39;embedded-opentype&#39;),
         url(&#39;fonts/hanshand-webfont.woff&#39;) format(&#39;woff&#39;),
         url(&#39;fonts/hanshand-webfont.ttf&#39;) format(&#39;truetype&#39;),
         url(&#39;fonts/hanshand-webfont.svg#webfont34M5alKg&#39;) format(&#39;svg&#39;);
    font-weight: normal;
    font-style: normal;
}
Copy after login

3. Benefits of introducing external fonts into CSS:

With the continued popularity of @font-face , resulting in many new font format icon sets, called web fonts. These web fonts have the following advantages over ordinary safe fonts:

1) They use real text instead of pictures. Zooming in and out will not affect the rendering effect and the user experience is good;

2) Can be recognized by search engines;

3) Unlike images that need to be regenerated every time, it is more convenient to add and delete.

The above is the detailed content of How to introduce external fonts in css? css font style setting method (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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to remove the default style in Bootstrap list? How to remove the default style in Bootstrap list? Apr 07, 2025 am 10:18 AM

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

How to layout bootstrap How to layout bootstrap Apr 07, 2025 pm 02:24 PM

To use Bootstrap to layout a website, you need to use a grid system to divide the page into containers, rows, and columns. First add the container, then add the rows in it, add the columns within the row, and finally add the content in the column. Bootstrap's responsive layout function automatically adjusts the layout according to breakpoints (xs, sm, md, lg, xl). Different layouts under different screen sizes can be achieved by using responsive classes.

How to build a bootstrap framework How to build a bootstrap framework Apr 07, 2025 pm 12:57 PM

To create a Bootstrap framework, follow these steps: Install Bootstrap via CDN or install a local copy. Create an HTML document and link Bootstrap CSS to the &lt;head&gt; section. Add Bootstrap JavaScript file to the &lt;body&gt; section. Use the Bootstrap component and customize the stylesheet to suit your needs.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

Does the image centering support image zooming? Does the image centering support image zooming? Apr 07, 2025 am 07:42 AM

How to achieve image centering and scaling in Bootstrap: Use d-flex justify-content-center to center images horizontally. Use align-items-center and fixed parent element height vertically center the image. Use the width and height attributes to control the image size, or use max-width and max-height to limit the maximum size. Use the img-fluid class or responsive design mechanism, such as media queries, to achieve responsive scaling. Optimize image size, control scaling using the object-fit attribute, and follow best practices to ensure performance and maintainability.

How to upload files on bootstrap How to upload files on bootstrap Apr 07, 2025 pm 01:09 PM

The file upload function can be implemented through Bootstrap. The steps are as follows: introduce Bootstrap CSS and JavaScript files; create file input fields; create file upload buttons; handle file uploads (using FormData to collect data and then send to the server); custom style (optional).

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)

How is the compatibility of Bootstrap image centering How is the compatibility of Bootstrap image centering Apr 07, 2025 am 07:51 AM

Bootstrap image centering faces compatibility issues. The solution is as follows: Use mx-auto to center the image horizontally for display: block. Vertical centering Use Flexbox or Grid layouts to ensure that the parent element is vertically centered to align the child elements. For IE browser compatibility, use tools such as Autoprefixer to automatically add browser prefixes. Optimize image size, format and loading order to improve page performance.

See all articles