Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Methods for Applying CSS Styles to HTML Documents

Tech 4

The Role of CSS in Web Development

HTML provides the structural foundation of a web page, defining the elements and their arrangement. However, HTML alone produces a basic, unstyled layout. CSS (Cascading Style Sheets) is used to enhance and style these HTML elements, controlling their visual presentation including colors, fonts, spacing, and positioning. This separation of content (HTML) from presentation (CSS) imrpoves code maintainability and reduces coupling.

The Relationship Between HTML and CSS

HTML is the primary technology for creating the page structure and content. CSS is then applied to style and format that existing HTML structure. The two technologies are complementary and work in tandem.

Understanding CSS Terminology

CSS stands for Cascading Style Sheets.

  • Cascading: Refers to the way multiple style rules can apply to the same element, with specific rules overriding more general ones.
  • Style Sheets: Collections of style rules that define how elements should be displayed.

Three Methods for Applying CSS

1. Inline Styles

Inline styles are applied directly to an individual HTML element using the style attribute. Multiple style declarations are separated by semicolons.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Inline Style Example</title>
</head>
<body>
    <h1 style="color: crimson; font-family: 'Arial', sans-serif;">This is a styled heading</h1>
</body>
</html>

2. Internal Styles (Embedded Styles)

Internal styles are defined within a <style> element placed inside the document's <head> section. Rules target elements using selectors.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Internal Style Example</title>
    <style>
        .page-heading {
            color: navy;
            font-family: Georgia, serif;
        }
    </style>
</head>
<body>
    <h1 class="page-heading">This heading uses internal styles</h1>
</body>
</html>

3. External Styles

This method involves creating a separate file with a .css extension to hold all style rules. The CSS file is then linked to the HTML document using the <link> element.

external-styles.css

.main-title {
    color: darkgreen;
    font-family: 'Verdana', Geneva, sans-serif;
}

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>External Style Example</title>
    <link rel="stylesheet" type="text/css" href="external-styles.css">
</head>
<body>
    <h1 class="main-title">This heading uses an external stylesheet</h1>
</body>
</html>

Recommended Practice and Priority

In professional develompent, External Styles are the most commonly used and recommended approach. They fully achieve the separation of concerns, making styles reusable across multiple pages and easier to manage.

When multiple styles from different methods target the same element, CSS follows the cascading order and specificity rules. Generally, styles declared "closer" to the element take precedence in a conflict, often summarized as "inline > internal > external," but specificity is the formal rule.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Style Priority Example</title>
    <style>
        h1 { color: goldenrod; }
    </style>
    <link rel="stylesheet" href="styles.css"> <!--假设此文件定义 h1 { color: blue; }-->
</head>
<body>
    <!-- The inline style has the highest specificity here -->
    <h1 style="color: firebrick;">This text will be firebrick red</h1>
</body>
</html>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.