HTML Styles

The HTML style attribute provides a way to modify the visual characteristics of elements on a web page. You can control the appearance of elements using CSS (Cascading Style Sheets). There are three main ways to apply styles to HTML elements:

  1. Inline Styles
  2. Internal Styles
  3. External Styles

1. Inline Styles

Inline styles are applied directly to HTML elements using the style attribute. This method is useful for quick, one-off style changes.

<!DOCTYPE html>
<html>
<head>
    <title>Inline Styles Example</title>
</head>
<body>
    <h1 style="color: blue; text-align: center;">This is a Blue Heading</h1>
    <p style="color: green;">This is a green paragraph.</p>
</body>
</html>

In this example, the style attribute is used to set the text color and alignment of the heading and paragraph.

2. Internal Styles

Internal styles are defined within a <style> element in the <head> section of the HTML document. This method is useful for styling a single page.

<!DOCTYPE html>
<html>
<head>
    <title>Internal Styles Example</title>
    <style>
        body {
            background-color: lightgray;
        }
        h1 {
            color: blue;
            text-align: center;
        }
        p {
            color: green;
        }
    </style>
</head>
<body>
    <h1>This is a Blue Heading</h1>
    <p>This is a green paragraph.</p>
</body>
</html>

In this example, the <style> element contains CSS rules that apply to the entire document. The body has a light gray background, the heading is blue and centered, and the paragraph is green.

3. External Styles

External styles are defined in a separate CSS file and linked to the HTML document using the element. This method is useful for applying styles across multiple pages.

HTML File:

<!DOCTYPE html>
<html>
<head>
    <title>External Styles Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>This is a Blue Heading</h1>
    <p>This is a green paragraph.</p>
</body>
</html>

CSS File (styles.css):

body {
    background-color: lightgray;
}
h1 {
    color: blue;
    text-align: center;
}
p {
    color: green;
}

In this example, the CSS rules are defined in an external file styles.css. The HTML file links to this CSS file using the element. This approach allows you to maintain a consistent style across multiple HTML documents.

Conclusion

Using styles in HTML allows you to control the appearance of your web pages effectively. Whether you use inline, internal, or external styles depends on the scope and complexity of your project. Experiment with these methods to see which works best for your needs!

Share this Tutorials

HTML Styles

Or copy link

CONTENTS
Scroll to Top