search
HomeWeb Front-endCSS TutorialHow to reference CSS into HTML

There are four ways to reference css style sheets. Use css styles directly in divs to create div+css web pages, use the style built-in style in html, use @import to reference external CSS files, and use link to reference external CSS files. Each has advantages and disadvantages, so today we will explain in detail the differences between these four methods.

Using different methods to reference css style sheets will ultimately achieve the same effect, but using css files applied in different methods will affect SEO and web page opening speed and efficiency. Next, we will explain the examples of how to reference CSS in HTML one by one

1. Embed CSS styles directly in HTML tag elements, such as


2. Insert the style statement code in the head part of the html header as follows:

<style type="text/css"> 
<!-- 
.ceshi {font-size:14px; color:#FF0000;}/*这里是设置CSS的样式内容*/ 
--> 
</style>


3. Use @import to reference external CSS files

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>css引用方法实例</title> 
<style type="text/css"> 
<!-- 
@import url(wcss.css);/*这里是通过@import引用CSS的样式内容*/ 
--> 
</style> 
</head> 
 
<body> 
<div class="ceshi">我是测试内容</div> 
 
</body> 
</html>

Code in Wcss.css file.ceshi {font-size:14px; color:#FF0000;}

It can be seen that there are similarities between using this method and using the built-in reference css style sheet method. Both need to use the style tag in the head of html to reference external css.


4. Use link to call external css files

Place To call the external wcss.css file to implement html reference css file

This method does not require the style tag, but directly refers to the external style by linking a style

It is generally recommended to use link to reference external css style methods.

Advantages of using link to reference external css

1. It is beneficial to SEO. Using this method to reference external css files will make the source code of the html page much smaller than directly adding css styles. , because search engine spiders do not crawl css files when crawling web pages, which results in very few html source codes, making spiders crawl faster and process less, which increases the weight of this web page and is beneficial to rankings.

2. Saving HTML allows the browser to separate threads when downloading web pages, just like loading a page and having two lines opening a page at the same time, making the web page open extra fast. (When the user browses this webpage, the html source code and css file are downloaded at the same time, making it faster)

3. It is convenient to modify the style of the webpage. You only need to modify the css style to modify the art style of the webpage. If on the website In this method of the project, since the entire site uses a common CSS style, it is quick and convenient to modify the style of the entire site.


These are the four ways to reference css style sheets. Friends who need them can save them. Please also continue to pay attention to other updates on this site.

Related recommendations:

How to learn Div and CSS


How to use CSS border


How to make rounded corners in CSS3

The above is the detailed content of How to reference CSS into HTML. 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
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor