Home > Web Front-end > HTML Tutorial > How to introduce css style into html? And the difference between link and @import (code example)

How to introduce css style into html? And the difference between link and @import (code example)

青灯夜游
Release: 2022-08-03 17:14:03
Original
4957 people have browsed it

In the development process of front-end websites, we all need to use CSS styles. CSS styles can effectively achieve more precise control over the layout, fonts, colors, backgrounds and other effects of the page. So how are these css style files or codes imported into html? This chapter will show you how to introduce CSS style files into HTML? As well as the difference between link and @import (code example) , let everyone understand how css styles are imported, focusing on the import of css style files. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. How to import CSS styles into HTML

HTML is mainly responsible for the content display of the web page, while the CSS file is responsible for the style of the web page content. The methods of using CSS styles in HTML include: inline, embedded, and external,

  • . The external style is divided into: link (link) and import (@import). ). [Recommended learning: css video tutorial]

Then let me introduce it to you:

1. Inline style --- -Code example using CSS

in HTML tags:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>行内式</title>
	</head>
	<body>
		<p style="color: red;font-size: 20px;">行内式行内式行内式行内式行内式行内式行内式行内式行内式<p>
	</body>
</html>
Copy after login

Rendering:

How to introduce css style into html? And the difference between link and @import (code example)

within the line quoted by css The style can also be called inline style or line-level style. It is introduced directly inside the tag. The obvious advantage is that it is very convenient and efficient; but it also has the disadvantage of not being able to reuse the style. If the number of lines of code reaches a certain length, it will not be possible. Recommended. Inline CSS is often used as a test to find bugs in the code.

Advantages:

  • Without the style sheet file, efficiency can be improved at some point;

  • Use The style attribute has the strongest style effect and will override the same style effect of other introduction methods.

Disadvantages:

  • It is difficult to share styles with multiple elements, which is not conducive to code reuse;

  • HTML and CSS code are mixed, making it difficult for programmers and search engines to read.

2. Embedded --- write the CSS content in the head tag through the style tag

Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>内嵌式</title>
		<style>
			p{
				background-color: #21B4BB;
				color: #fff;
				font-size: 20px;
			}
		</style>
	</head>
	<body>
		<p>内嵌式内嵌式内嵌式内嵌式内嵌式内嵌式内嵌式<p>
	</body>
</html>
Copy after login

Rendering:

How to introduce css style into html? And the difference between link and @import (code example)

The embedded style referenced by css can also be called internal style or page-level style, and the whole thing is placed in the head tag Inside, define the style in the style tag, and the scope is limited to the elements of this page; if the code you write exceeds a few hundred lines, think about how annoying it is to pull the code page to the top every time, so it is maintainable. Sexually inferior.

Advantages: Like interline style sheets, no additional requests are generated, and it initially realizes the separation of structure and style, making it more suitable for single-page website applications.

Disadvantages: Since the internal style sheet is written in the HTML file, the page is impure, the file size is large, it is not conducive to web crawlers to obtain information, it is not conducive to maintenance, and styles cannot be shared between pages

3. External link---introducing external style sheets (css style files) through the link tag

##1) Link type (link)

Grammar:

<link rel="stylesheet"  type="text/css"  href="css的路径" >
Copy after login

Code example:

HTML code:


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>链接式</title>
		<link rel="stylesheet" type="text/css" href="style.css"  >
	</head>
	<body>
		<p>链接式链接式链接式链接式链接式链接式链接式链接式链接式链接式链接式链接式<p>
	</body>
</html>
Copy after login

css style file --style.css code:

p{
	font-size: 20px;
	color: #fff;
	background-color: #70DBDB;
}
Copy after login

Rendering:


How to introduce css style into html? And the difference between link and @import (code example)

The link type will load the CSS file before loading the main body of the web page file, so the displayed web page will have a style effect from the beginning. It will not display the unstyled web page first and then display the styled web page like the import type. This is the advantage of the link type.

2) Import (@import)

Syntax:


<style type="text/css" media="screen">   
@import url("CSS文件");   
</style>
Copy after login

Code example:


HTML code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>导入式</title>
		<style type="text/css" media="screen">   
		@import url("style.css");   
		</style>
	</head>
	<body>
		<p>导入式导入式导入式导入式导入式导入式导入式导入式导入式<p>
	</body>
</html>
Copy after login
Rendering:

How to introduce css style into html? And the difference between link and @import (code example)

The import will load the CSS file after the entire web page is loaded, so this results in One problem is that if the web page is relatively large, the unstyled page will appear for a while, and then after a flash, the style of the web page will appear. This is an inherent flaw of imports.

3) Advantages and disadvantages of external connection

Advantages:

  • Code that implements structure and performance Complete separation

  • Facilitates reuse and maintenance

  • Because it is separated into separate files, the size of the HTML file is greatly reduced, thus allowing The page structure is easier to read by programmers and web crawlers

  • It is friendly to search engines, allowing search engines to rate the page higher, which is beneficial to the page’s search engine ranking

  • The external style sheet is cached on the user's computer after the user's first visit, and does not need to be loaded on the next visit

Disadvantages:

  • If there are many styles, the CSS file will become very large and difficult to find. In addition, one more CSS file means one more HTTP request, which will increase server pressure on a website with a large number of visits.

2. Link and import The difference between the formula (@import)

  • link is an XHTML tag. In addition to loading CSS, it can also define other transactions such as RSS; while @import belongs to the CSS category. Only CSS can be loaded; when

  • #link references CSS, it is loaded at the same time when the page is loaded; while @import requires the page to be loaded completely.

  • link is an XHTML tag and has no compatibility issues; while @import was proposed in CSS2.1 and is not supported by lower version browsers.

  • ink supports using Javascript to control the DOM to change the style; @import does not support it.

  • @import can introduce other style sheets into the CSS file; link does not support it.

(Learning video sharing: Getting started with web front-end)

The above is the detailed content of How to introduce css style into html? And the difference between link and @import (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template