


What are the different ways to include CSS in an HTML document, and what are their pros and cons?
Jun 20, 2025 am 12:38 AMThere are three common ways to introduce CSS into HTML documents, namely inline styles, internal style sheets and external style sheets. 1. Inline styles are written directly in the style attribute of the HTML tag, which is suitable for temporary modification of individual element styles. The advantage is that they are simple and direct, and the disadvantage is that they are poor maintainability and are not conducive to separating concerns; 2. The internal style sheet is written in
through the <style> tag, and the style is centrally managed. It is suitable for single pages or small websites, but is not convenient for multi-page sharing styles; 3. The external style sheet writes CSS to an independent file and introduces it through <link>, which supports multi-page sharing styles, which are easy to maintain and perform performance optimization, and is suitable for medium and large projects. It is the most commonly used method in modern web development. Overall, the larger the project size, the more external style sheets should be used, while inline and internal styles are suitable for debugging or small-scale applications.There are three common ways to introduce CSS into HTML documents: inline styles, internal style sheets, and external style sheets. They each have their own applicable scenarios and have their own advantages and disadvantages.
Inline Styles
This is the most straightforward way to write styles in the style attribute of the HTML tag. For example:<pre class='brush:php;toolbar:false;'> <p style="color: red; font-size: 16px;">This is a red text</p></pre><p> <strong>advantage:</strong></p><ul><li> Simple and direct, suitable for temporary modification of individual element styles.</li><li> No additional files or tags are required and effective quickly.</li></ul><p> <strong>shortcoming:</strong></p><ul><li> Poor maintainability, the style and structure are mixed together, which is not conducive to later maintenance.</li><li> The style cannot be reused, and there are many repetitions of code.</li><li> It is not conducive to separating the focus and violates the best practices of web development.</li></ul><p> This method is more suitable for testing or very small projects, or when an element style must be unique.</p><hr /><h3> Internal Style Sheet</h3><p> Write CSS directly into <code><head>
part of the HTML file through the <style>
tag:
<head> <style> p { color: blue; } </style> </head>
advantage:
- Centralized style management is easier to maintain than inline.
- No external files are required, suitable for single page applications or simple pages.
shortcoming:
- Styles cannot be shared between multiple pages, resulting in duplicate work.
- If there are many pages, the style code will bloat, affecting maintainability.
This approach is suitable for style definitions for small websites or individual pages, but is not recommended for large projects.
External Style Sheet
Write CSS in a separate .css
file and import it into HTML via the <link>
tag:
<link rel="stylesheet" href="styles.css">
advantage: shortcoming: This is the most commonly used method in modern web development, especially suitable for medium and large projects or multi-page websites. In general, which method to choose depends on the size and requirements of the project. For most actual projects, using an external stylesheet is the best choice. If it is a temporary debugging or small page, you can consider inline or internal styles. Basically, this is all, understanding different applicable scenarios can allow you to organize web page styles more flexibly. The above is the detailed content of What are the different ways to include CSS in an HTML document, and what are their pros and cons?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)

Backdrop-filter is used to apply visual effects to the content behind the elements. 1. Use backdrop-filter:blur(10px) and other syntax to achieve the frosted glass effect; 2. Supports multiple filter functions such as blur, brightness, contrast, etc. and can be superimposed; 3. It is often used in glass card design, and it is necessary to ensure that the elements overlap with the background; 4. Modern browsers have good support, and @supports can be used to provide downgrade solutions; 5. Avoid excessive blur values and frequent redrawing to optimize performance. This attribute only takes effect when there is content behind the elements.

User agent stylesheets are the default CSS styles that browsers automatically apply to ensure that HTML elements that have not added custom styles are still basic readable. They affect the initial appearance of the page, but there are differences between browsers, which may lead to inconsistent display. Developers often solve this problem by resetting or standardizing styles. Use the Developer Tools' Compute or Style panel to view the default styles. Common coverage operations include clearing inner and outer margins, modifying link underscores, adjusting title sizes and unifying button styles. Understanding user agent styles can help improve cross-browser consistency and enable precise layout control.

Define@keyframesbouncewith0%,100%attranslateY(0)and50%attranslateY(-20px)tocreateabasicbounce.2.Applytheanimationtoanelementusinganimation:bounce0.6sease-in-outinfiniteforsmooth,continuousmotion.3.Forrealism,use@keyframesrealistic-bouncewithscale(1.1

Usetheelementwithinatagtocreateasemanticsearchfield.2.Includeaforaccessibility,settheform'sactionandmethod="get"attributestosenddatatoasearchendpointwithashareableURL.3.Addname="q"todefinethequeryparameter,useplaceholdertoguideuse

rel="stylesheet"linksCSSfilesforstylingthepage;2.rel="preload"hintstopreloadcriticalresourcesforperformance;3.rel="icon"setsthewebsite’sfavicon;4.rel="alternate"providesalternateversionslikeRSSorprint;5.rel=&qu

Using tags is the easiest and recommended method. The syntax is suitable for modern browsers to embed PDF directly; 2. Using tags can provide better control and backup content support, syntax is, and provides download links in tags as backup solutions when they are not supported; 3. It can be embedded through Google DocsViewer, but it is not recommended to use widely due to privacy and performance issues; 4. In order to improve the user experience, appropriate heights should be set, responsive sizes (such as height: 80vh) and PDF download links should be provided so that users can download and view them themselves.

ThetargetattributeinanHTMLanchortagspecifieswheretoopenthelinkeddocument.1._selfopensthelinkinthesametab(default).2._blankopensthelinkinanewtaborwindow.3._parentopensthelinkintheparentframe.4._topopensthelinkinthefullwindowbody,removingframes.Forexte

Use elements and set the action and method attributes to specify the data submission address and method; 2. Add input fields with name attribute to ensure that the data can be recognized by the server; 3. Use or create a submission button, and after clicking, the browser will send the form data to the specified URL, which will be processed by the backend to complete the data submission.
