External CSS is a method of applying CSS styles to an HTML document by linking to an external CSS file. This approach keeps the styles completely separate from the HTML content, promoting better organization, maintainability, and reusability across multiple web pages. The external CSS file contains only CSS rules and is linked to the HTML document using the <link>
tag inside the <head>
section.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph with external CSS.</p>
<button>Click Me!</button>
</body>
</html>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
}
h1 {
color: blue;
text-align: center;
}
p {
font-size: 16px;
color: green;
padding: 10px;
}
button {
background-color: yellow;
border: 2px solid black;
padding: 10px 20px;
cursor: pointer;
}
HTML File (index.html
):
<link>
tag inside the <head>
section links the external CSS file (styles.css
) to the HTML document. The rel
attribute specifies the relationship as a stylesheet, and the href
attribute provides the path to the CSS file.CSS File (styles.css
):
body
, h1
, p
, and button
elements, similar to the previous examples but in a separate file.