Three ways to introduce css: 1. Inline introduction, use the style attribute to insert CSS code into a specific HTML tag, the syntax is "
.."; 2. Embedded introduction, put the CSS code in the style tag pair in the head part of the document, the syntax is ""; 3. External introduction, put the CSS code in Into the external CSS file, use the link tag or "@import" rule to introduce it into the html document.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
CSS: Cascading style sheets are a computer language used to express document styles such as HTML or XML. CSS can not only statically modify web pages, but can also cooperate with various scripting languages to dynamically format various elements of web pages. CSS can perform pixel-level precise control of the layout of element positions in web pages, supports almost all font sizes and styles, and has The ability to edit web object and model styles
Basic syntax of CSS:
CSS rules consist of two main parts: selectors, As well as one or more statements, the selector is usually the HTML element whose style needs to be changed. Each statement consists of an attribute and a value.
1. Selector (Selector)
The selector consists of the id, class attribute or element name itself of the HTML element and some Special symbols are used to specify which HTML element to define the style for. For example, the selector p means to define the style for all
tags in the page;
2. Declaration
There can be one or countless declarations. These declarations tell the browser how to render the object specified by the selector. All declarations are placed within a pair of curly braces { }, immediately following the selector.
The statement must include two parts: attributes and attribute values, and use a semicolon to mark the end of a statement. The semicolon can be omitted for the last statement in a style.
Attribute: The style name you want to set for the HTML element, consisting of a series of keywords, such as color, border, font, etc., in CSS Provides many attributes, which you can view through the W3C official website;
Value: consists of a numerical value and a unit or keyword, used to control the display effect of a certain attribute, such as the value of the color attribute It can be red or #F1F1F1 etc.
Colon: is required to separate attributes and values. Each combination of attributes and values can be regarded as a statement. A semicolon is required at the end of each statement. ; At the end, declarations belonging to the same selector need to be wrapped in curly braces { }.
Three forms of CSS introduction
1. Inline style sheet (inline introduction)
Add the style attribute directly to a single HTML element tag to control the presentation style of the HTML tag.
This way of introducing CSS is decentralized, flexible and convenient, but it lacks integrity and planning, which is not conducive to later modification and maintenance. When the style of the website needs to be modified, the same modification may involve multiple places. , high maintenance costs. The style effect using the STYLE attribute is the strongest and will overwrite the same style effects of other introduction methods.
<!DOCTYPE html> <html> <head> </head> <body> <h1 id="PHP中文网">PHP中文网</h1> <p style="color: blue;">https://www.php.cn/</p> </body> </html>
Although inline style (inline style) can easily give CSS styles to HTML tags, its shortcomings are also very obvious, and it is not recommended to use it too much.
Defining inline styles requires defining the style attribute in each HTML tag, which is very inconvenient;
Use double in inline styles Be especially careful when using quotation marks or single quotation marks, because the attributes of HTML tags usually use double quotation marks to wrap the attribute value, such as ;
- ## in The styles defined in inline styles cannot be reused anywhere else;
- Inline styles are very inconvenient in later maintenance, because a website usually consists of many pages. When modifying the page style Pages need to be modified one by one;
- Adding too many inline styles will increase the size of the HTML document.
2. Internal style sheet (embedded introduction)
Write the style code on the page语法:
<style type="text/css"> @import url("css文件路径"); </style>
简单实例:
css外部样式表 style.css
h1{ color:red; } p{ font-size:14px; color:green; }
HTML文档
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link type="text/css" rel="styleSheet" href="style.css" /> <!-- <style> @import url("style.cs"); </style> --> </head> <body> <h1 id="link标签或-import的应用">link标签或@import的应用</h1> <p>外部定义CSS样式表以.CSS为扩展名文件,然后在页面中通过link标签或@import链接到页面中。</p> </body> </html>
实现效果:
link和@import的区别:
1、从属关系区别
@import是 CSS 提供的语法规则,只有导入样式表的作用;link是HTML提供的标签,不仅可以加载 CSS 文件,还可以定义 RSS、rel 连接属性等。
2、加载顺序区别
加载页面时,link标签引入的 CSS 被同时加载;@import引入的 CSS 将在页面加载完毕后被加载。
3、兼容性区别
@import是 CSS2.1 才有的语法,故只可在 IE5+ 才能识别;link标签作为 HTML 元素,不存在兼容性问题。
4、DOM可控性区别
可以通过 JS 操作 DOM ,插入link标签来改变样式;由于 DOM 方法是基于文档的,无法使用@import的方式插入样式。
(学习视频分享:web前端)
The above is the detailed content of What are the three different ways to introduce css?. For more information, please follow other related articles on the PHP Chinese website!

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

The relationship between HTML and React is the core of front-end development, and they jointly build the user interface of modern web applications. 1) HTML defines the content structure and semantics, and React builds a dynamic interface through componentization. 2) React components use JSX syntax to embed HTML to achieve intelligent rendering. 3) Component life cycle manages HTML rendering and updates dynamically according to state and attributes. 4) Use components to optimize HTML structure and improve maintainability. 5) Performance optimization includes avoiding unnecessary rendering, using key attributes, and keeping the component single responsibility.

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

React Strict Mode is a development tool that highlights potential issues in React applications by activating additional checks and warnings. It helps identify legacy code, unsafe lifecycles, and side effects, encouraging modern React practices.

React Fragments allow grouping children without extra DOM nodes, enhancing structure, performance, and accessibility. They support keys for efficient list rendering.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools