search
HomeWeb Front-endHTML TutorialHow to introduce css style into html? And the difference between link and @import (code example)

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>

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>

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的路径" >

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>

css style file --style.css code:

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

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>

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>

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!

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
What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version