CSS


This draft deletes the entire topic.

expand all collapse all

Examples

  • 20

    An external CSS stylesheet can be applied to any number of HTML documents by placing a <link> element in each HTML document.

    The attribute rel and href of the <link> tag have to be set to "stylesheet" and the relative or absolute path to the stylesheet, respectively. While using relative URL paths is generally considered good practice, absolute paths can be used, too. In HTML5 the type attribute can be omitted.

    It is recommended that the <link> tag be placed in the HTML file's <head> tag so that the styles are loaded before the elements they style. Otherwise, users will see a flash of unstyled content.

    Example

    hello-world.html

    <html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <h1>Hello world!</h1>
        <p>I ♥ CSS</p>
    </body>
    </html>
    

    style.css

    h1 {
        color: green;
        text-decoration: underline;
    }
    p {
        font-size: 25px;
        font-family: 'Trebuchet MS';
    }
    
  • 14

    CSS enclosed in <style></style> tags within an HTML document functions like an external stylesheet, except that it lives in the HTML document it styles instead of in a separate file, and therefore can only be applied to the document in which it lives. Note that this element must be inside the <head> element for HTML validation (though it will work in all current browsers if placed in body).

    <head>
        <style>
            h1 {
                color: green;
                text-decoration: underline;
            }
            p {
                font-size: 25px;
                font-family: 'Trebuchet MS', sans-serif;
            }
        </style>
    </head>
    <body>
        <h1>Hello world!</h1>
        <p>I ♥ CSS</p>
    </body>
    
  • 9

    CSS styles can not only be imported into HTML, but also into another CSS file using @import.

    The following line imports a CSS file named additional-styles.css in the root directory into the CSS file in which it appears:

    @import '/additional-styles.css';
    

    Importing external CSS is also possible. A common use case are font files.

    @import 'https://fonts.googleapis.com/css?family=Lato';
    

    An optional second argument to @import rule is a list of media queries:

    @import '/print-styles.css' print;
    
Please consider making a request to improve this example.

Remarks

Styles can be authored in several ways, allowing for varying degrees of reuse and scope when they are specified in a source HTML document. External stylesheets can be reused across HTML documents. Embedded stylesheets apply to the entire document in which they are specified. Inline styles apply only to the individual HTML element on which they are specified.

Versions

VersionRelease Date
11996-12-17
21998-05-12
32015-10-13
Still have a question about Getting started with CSS? Ask Question

Topic Outline