


How 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:
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:
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的路径" >
<!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>
p{ font-size: 20px; color: #fff; background-color: #70DBDB; }
2) Import (@import)
Syntax:<style type="text/css" media="screen"> @import url("CSS文件"); </style>
<!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>
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
