Methods to put css in html files: 1. Add CSS directly to the style attribute in the HTML tag; 2. Write CSS under the style tag in the HTML header; 3. Use the head tag to introduce external CSS files; 4. Use CSS rules to introduce external CSS files.
The operating environment of this article: Windows7 system, HTML5&&CSS3 version, DELL G3 computer
How to introduce CSS into HTML
There are 4 ways to introduce CSS in HTML. Two of them are to add CSS code directly in the HTML file, and the other two are to introduce external CSS files. Let’s take a look at these methods and their advantages and disadvantages.
Inline method
The inline method refers to adding CSS directly to the style attribute in the HTML tag.
Example:
<div style="background: red"></div>
This is usually a bad way of writing, it can only change the style of the current tag. If you want multiple
Embedding method
The embedding method refers to writing CSS code under the
Example:
<head> <style> .content { background: red; } </style> </head>
Embedded CSS is only valid for the current web page. Because the CSS code is in the HTML file, it makes the code more concentrated, which is usually advantageous when we write template web pages. Because someone looking at the template code can see the HTML structure and CSS styles at a glance. Because embedded CSS is only valid for the current page, when multiple pages need to introduce the same CSS code, writing this way will lead to code redundancy and is not conducive to maintenance.
Link method
The link method refers to using the
tag in the HTML header to introduce external CSS files.Example:
<head> <link rel="stylesheet" type="text/css" href="style.css"> </head>
This is the most common and recommended way to introduce CSS. Using this approach, all CSS code only exists in a single CSS file, so it has good maintainability. And all CSS code only exists in the CSS file. The CSS file will be introduced when it is loaded for the first time. When switching pages in the future, you only need to load the HTML file.
Import method
Import method refers to using CSS rules to introduce external CSS files.
Example:
<style> @import url(style.css); </style>
Compare the link method and the import method
The link method (replaced by link below) and the import method (replaced by @import below) both introduce external CSS file method, let’s compare these two methods and explain why @import is not recommended.
link belongs to HTML, and external files are introduced through the href attribute in the tag, while @import belongs to CSS, so the import statement should be written in CSS. It should be noted that the import statement should be written in the style at the beginning of the table, otherwise external files cannot be imported correctly;
@import is a concept that only appeared in CSS2.1, so if the browser version is lower, external style files cannot be imported correctly;
When When the HTML file is loaded, the file referenced by link will be loaded at the same time, while the file referenced by @import will not be loaded until the page is completely downloaded;
Summary: We should try to use the tag to import For external CSS files, avoid or use less of the other three methods.
【Recommended learning: css video tutorial】
The above is the detailed content of How to put css in html file. For more information, please follow other related articles on the PHP Chinese website!

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.

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 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.

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.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

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


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

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

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

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment